500,改造2中的内部服务器错误

时间:2018-08-14 11:48:54

标签: java android server http-post retrofit2

每次尝试通过翻新发送POST请求时,我都有500个内部服务器错误。当我发送GET请求时,它发送正确。我敢肯定,在服务器端,一切都可以。我的代码有什么问题?

这是我的请求方法

@POST("/listing/createListing")
Call<ResponseBody> pushData(@Body RequestBody image);

后期实现modelTask​​是我的模型类对象,firebaseToken是firebase身份验证令牌。在我的模型课中,我将图像保存为文件列表。

OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new 
Interceptor() {
        @Override
        public okhttp3.Response intercept(Chain chain) throws 
IOException {
            Request newRequest  = chain.request().newBuilder()
                    .addHeader("Authorization", "Bearer " + 
firebaseToken)
                    .build();
            return chain.proceed(newRequest);
        }
    }).build();
    Retrofit retrofit = new Retrofit.Builder()
            .client(client)
            .baseUrl("https://something.herokuapp.com")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    ApiEndPoint apiService = retrofit.create(ApiEndPoint.class);


    MultipartBody.Builder builder = new MultipartBody.Builder();
    builder.setType(MultipartBody.FORM);


    builder.addFormDataPart("title", modelTask.getTitle());
    builder.addFormDataPart("description", modelTask.getDescription());
    builder.addFormDataPart("user_name", "Josim Uddin");
    builder.addFormDataPart("user_id", "2133323");
    builder.addFormDataPart("price", "30.0");
    builder.addFormDataPart("category_name", "Cleaning");
    builder.addFormDataPart("category_id", "123");
    builder.addFormDataPart("preferred_date", 
modelTask.getPreferred_date());
    builder.addFormDataPart("preferred_time", 
modelTask.getPreferred_time());
    builder.addFormDataPart("hasPet", modelTask.getHasPet());
    builder.addFormDataPart("budget", 
String.valueOf(modelTask.getBudget()));
    builder.addFormDataPart("address", modelTask.getAddress());
    builder.addFormDataPart("longitude", 
String.valueOf(modelTask.getLongitude()));
    builder.addFormDataPart("latitude", 
String.valueOf(modelTask.getLatitude()));
    builder.addFormDataPart("equipment", modelTask.getEquipment());

    for (int i = 0; i < modelTask.getImages().size(); i++) {
        builder.addFormDataPart("image[]", 
modelTask.getImages().get(i).getName(), 
RequestBody.create(MediaType.parse("multipart/form-data"), 
modelTask.getImages().get(i)));
    }


    MultipartBody requestBody = builder.build();
    Call<ResponseBody> call = apiService.pushData(requestBody);
    call.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, 
Response<ResponseBody> response) {
            Log.d(TAG, "onResponse:" + response.code()+", 
"+response.message());

            if (response.isSuccessful()) {
                try {
                    Log.d(TAG, "post submitted code:" + 
response.code()+"body:"+response.body().string());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            Log.e(TAG, "Unable to submit post to API:: 
"+t.getMessage());
        }
    });

邮递员测试成功。

1 个答案:

答案 0 :(得分:1)

尝试

@POST("listing/createListing")
Call<ResponseBody> pushData(@Body RequestBody image);

从链接中删除“ /”,并将其添加到基本网址的末尾。