改造图像上载返回错误请求400

时间:2018-05-08 07:53:33

标签: java android retrofit http-status-code-400

我正在使用改装,我需要上传和成像,但我的状态代码是400.我是初学者。改造,我无法找到问题的解决方案。 那你能帮帮我吗? 这是代码。

这是界面。

public interface SupportInterface {

//Get request for sending photo in chat
@Multipart
@POST("/api/upload-image")
Call<ResponseBody> getChatPhoto(@Header("Content-Type") String json,
                                @Header("Authorization") String token,
                                @Header("Cache-Control") String cache,
                                @Part("type") String type,
                                @Part("user_id") String userId,
                                @Part MultipartBody.Part image_path);

}

我使用标题+我需要发送user_id并输入。所以我使用@Part。我做对了吗?

这是初始化部分的改造。

public class ApiClient {

private static ApiClient instance;

    OkHttpClient.Builder client = new OkHttpClient.Builder()
            .readTimeout(10, TimeUnit.SECONDS)
            .connectTimeout(10, TimeUnit.SECONDS)
            .writeTimeout(10, TimeUnit.SECONDS);

    client.addInterceptor(new Interceptor() {
        @Override
        public Response intercept(@NonNull Chain chain) throws IOException {
            Request request = chain.request();
            request = request.newBuilder()
                    .header("Cache-Control", "public, max-age=0")
                    .build();
            return chain.proceed(request);
        }
    });

    supportopApi = new Retrofit.Builder()
            .baseUrl(endpoint)
            .client(client.build())
            .addConverterFactory(GsonConverterFactory.create())
            .build()
            .create(SupportopApi.class);
}

public Call<ResponseBody> getChatImage(MultipartBody.Part multipartBody) {
    return supportopApi.getChatPhoto("application/json", There is my accessToken,
            "no-cache", "5", This is userID, multipartBody);
   }
}

如果我在这里做错了,请告诉我。

这是主要部分。

public void getChatImage() {

    File file = new File("/storage/emulated/0/Download/s-l640.jpg");
    RequestBody reqFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
    MultipartBody.Part multiPartFile = MultipartBody.Part.createFormData("image", file.getName(), reqFile);

    Call<ResponseBody> chatImageCall = apiClient.getChatImage(multiPartFile);
    chatImageCall.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            if (response.isSuccessful()) {
                try {
                    Log.d(TAG, response.body().string());
                } catch (IOException e) {
                    e.printStackTrace();
                }

            } else {
                Toast.makeText(context, "Response is not successful: " + response.errorBody(), Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            Toast.makeText(getActivity(), "An error occurred", Toast.LENGTH_SHORT).show();
        }
    });
}

我在这里遇到了错误的请求400.所以请帮我解决这个问题。我是初学者,还在改造中)

1 个答案:

答案 0 :(得分:1)

@Header("Content-Type") String json

您声明您的内容类型为JSON,但实际上您将多部分(表单数据)传递给服务器

所以我认为你正在努力做到:

@Header("Accept") String json

(从服务器接受JSON)

相关问题