Android 改装2.0多部分
我有工作任务httpAndroid 改装2.0多部分,android,multipartform-data,retrofit2,Android,Multipartform Data,Retrofit2,我有工作任务http多部分请求: multi = new MultipartBody.Builder() .setType(MultipartBody.FORM) .addFormDataPart("email", m) .addFormDataPart("password", p) .addFormDataPart("user_name", n)
多部分
请求
:
multi = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("email", m)
.addFormDataPart("password", p)
.addFormDataPart("user_name", n)
.addFormDataPart("user_phone", phone)
.addFormDataPart("photo", "avatar.jpg", RequestBody.create(MediaType.parse("image/jpg"), imgToArray(bmpChosenPic)))
//.addFormDataPart("photo", "1")
.build();
request = new Request.Builder()
.url(Utils.TEST_BASE_URL + "" + REG_URL)
.post(multi)
.build();
client.newCall(request).enqueue(new Callback()...
现在,我尝试使用改装2.0
实现相同的功能。服务内容如下:
@Multipart
@POST("/api/v1/auth/register")
Call<Registration> registration (@Part("email") RequestBody email,
@Part("password") RequestBody password,
@Part("user_name") RequestBody userName,
@Part("photo") RequestBody url,
@Part("phone") RequestBody phone);
但是,不幸的是,我在这方面得到了NPE:
RegistrationData data = response.body().getData();
所以我想,我的代码有问题。我已经阅读了有关SOF和GitHub上的改装分支的相关主题,但没有找到有效的解决方案。请帮忙。我也很难提出这样的工作请求。我最终用它上传了一张图片或一段视频:
@Multipart
@POST(Constants.URL_UPLOAD)
Call<ResponseBody> upload(@PartMap Map<String, RequestBody> params);
@Multipart
@POST(常量.URL\u上传)
调用上载(@PartMap-Map-params);
以及方法API调用
private String uploadFile(String path, final String type) throws IOException, JSONException {
Map<String, RequestBody> map = new HashMap<>();
map.put("Id", Utils.toRequestBody("0"));
map.put("Name", Utils.toRequestBody("example"));
String types = path.substring((path.length() - 3), (path.length()));
if (path != null) {
File file2 = new File(path);
RequestBody fileBody = RequestBody.create(MediaType.parse(type), file2);
map.put("file\"; filename=\"cobalt." + types + "\"", fileBody);
}
Call<ResponseBody> call = cobaltServices.upload(map);
ResponseBody response = call.execute().body();
return RESULT_OK;
}
私有字符串上传文件(字符串路径,最终字符串类型)抛出IOException、JSONException{
Map Map=newhashmap();
地图放置(“Id”,UTIL.toRequestBody(“0”);
map.put(“Name”,Utils.toRequestBody(“示例”);
字符串类型=path.substring((path.length()-3),(path.length());
if(路径!=null){
文件file2=新文件(路径);
RequestBody fileBody=RequestBody.create(MediaType.parse(type),file2);
put(“文件\”文件名=\“cobalt.+types+“\”,文件体);
}
Call Call=cobaltServices.upload(map);
ResponseBody response=call.execute().body();
返回结果_OK;
}
谢谢。刚刚在某个主题中找到了您的答案。
private String uploadFile(String path, final String type) throws IOException, JSONException {
Map<String, RequestBody> map = new HashMap<>();
map.put("Id", Utils.toRequestBody("0"));
map.put("Name", Utils.toRequestBody("example"));
String types = path.substring((path.length() - 3), (path.length()));
if (path != null) {
File file2 = new File(path);
RequestBody fileBody = RequestBody.create(MediaType.parse(type), file2);
map.put("file\"; filename=\"cobalt." + types + "\"", fileBody);
}
Call<ResponseBody> call = cobaltServices.upload(map);
ResponseBody response = call.execute().body();
return RESULT_OK;
}