android - 改造 2.0 多部分

标签 android multipartform-data retrofit2

我有工作 OkHttp MultiPart request :

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()...

现在我尝试使用 Retrofit 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);

所以根据这个接口(interface)我试着打一个Call:

  RequestBody photoR = RequestBody.create(MediaType.parse("image/jpg"), imgToArray(bmp));
    RequestBody nameR = RequestBody.create(MediaType.parse("text/plain"), name);
    RequestBody emailR = RequestBody.create(MediaType.parse("text/plain"), email);
    RequestBody passR = RequestBody.create(MediaType.parse("text/plain"), pass);
    RequestBody phone = RequestBody.create(MediaType.parse("text/plain"), "");
    RegistaionI service = ServiceGenerator.createService(RegistaionI.class);
    retrofit2.Call<Registration> call = service.registration(
            emailR,
            passR,
            nameR,
            photoR,
            phone);
    call.enqueue(new retrofit2.Callback<Registration>() {
        @Override
        public void onResponse(retrofit2.Call<Registration> call, retrofit2.Response<Registration> response) {
            RegistrationData data = response.body().getData();
            System.out.println(data.getId());
        }

        @Override
        public void onFailure(retrofit2.Call<Registration> call, Throwable t) {

        }
    });

但是,不幸的是,我在这一行得到了 NPE:

 RegistrationData data = response.body().getData();

所以我想,我的代码有问题。我已经阅读了关于 SOF 和 GitHub 上 Retrofit 分支的相关主题,但没有找到可行的解决方案。请帮忙。

最佳答案

我也很难让这种请求生效。我最终使用它来上传图片或视频:

@Multipart
@POST(Constants.URL_UPLOAD)
Call<ResponseBody> upload(@PartMap Map<String, RequestBody> 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;
}

关于android - 改造 2.0 多部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37681750/

相关文章:

Android 在 1 小时后执行一个函数

android - 使用 Retrofit 在 Multipart 中将名称/值对作为对象发送

javascript - 请求js重定向

Android Retrofit 2.0 添加带有拦截器的 header 不起作用

java - 如何将参数从主 Activity 传递到@Get API

android - 如何在 Android 应用程序中结合 RxJava Single & Completable Retrofit 调用

android - 如何为图像设置匹配父级、包装内容、水平滚动

Android - 加载后在 Webview 中搜索文本

javascript - Facebook Javascript 表单数据照片上传 : requires upload file error

android - 如何禁用 fragment 中的抽屉并返回正确的 fragment