android – 在Retrofit 2中上传文件

android – 在Retrofit 2中上传文件,第1张

概述我尝试了以下但是在响应时我得到500错误(内部服务器错误) – 帮助我在上面的屏幕截图中设计请求的界面…谢谢 @Multipart@POST("myrecord")Call<ResponseBody> addRecord(@Query("token") String token,@Query("userid") int userId,

我尝试了以下但是在响应时我得到500错误(内部服务器错误) – 帮助我在上面的屏幕截图中设计请求的界面…谢谢

@Multipart@POST("myrecord")Call<ResponseBody> addRecord(@query("token") String token,@query("userID") int userID,@query("name") String name,@Part("file") Requestbody file);file file = new file(getRealPathFromURI(data.getData()));Requestbody requestfile = Requestbody.create(MediaType.parse("image/*"),getRealPathFromURI(data.getData()));` Call<ResponseBody> responseBodyCall = service.addRecord(token,userID,"newfilename",requestfile);        responseBodyCall.enqueue(new Callback<ResponseBody>() {            @OverrIDe            public voID onResponse(Call<ResponseBody> call,Response<ResponseBody> response) {                Log.d("Response","="+response.code());                Log.d("Response","= "+response.message());            }            @OverrIDe            public voID onFailure(Call<ResponseBody> call,Throwable t) {                Log.d("failure","message = " + t.getMessage());                Log.d("failure","cause = " + t.getCause());            }        });`
解决方法 以下代码工作:)
@Multipart@POST("myrecord")Call<ResponseBody> addRecord(@query("token") String token,@Part Multipartbody.Part file); @OverrIDepublic voID onActivityResult(int requestCode,int resultCode,Intent data) {    super.onActivityResult(requestCode,resultCode,data);    if ((requestCode == file_SELECT_CODE) && (resultCode == -1)) {        file file = new file(getRealPathFromURI(data.getData()));        Requestbody requestfile = Requestbody.create(MediaType.parse("multipart/form-data"),getRealPathFromURI(data.getData()));        Multipartbody.Part multipartbody =Multipartbody.Part.createFormData("file",file.getname(),requestfile);        Call<ResponseBody> responseBodyCall = service.addRecord(token,"filename",multipartbody);        responseBodyCall.enqueue(new Callback<ResponseBody>() {            @OverrIDe            public voID onResponse(Call<ResponseBody> call,Response<ResponseBody> response) {                Log.d("Success","success "+response.code());                Log.d("Success","success "+response.message());            }            @OverrIDe            public voID onFailure(Call<ResponseBody> call,"cause = " + t.getCause());            }        });    }}
总结

以上是内存溢出为你收集整理的android – 在Retrofit 2中上传文件全部内容,希望文章能够帮你解决android – 在Retrofit 2中上传文件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1137774.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存