Retrofit 2调用返回500作为响应

时间:2016-12-26 10:25:22

标签: android retrofit2

使用Retrofit2调用Web服务返回500内部服务器错误。但是,当从邮递员和iOS设备调用时,相同的Web服务工作完美。这是对Web服务的调用

@Multipart
@POST("domestic/post_job")
Call<PostJobResponse> postJob(@PartMap HashMap<String, RequestBody> fields, @Part MultipartBody.Part part);`

这是基本网址http://jobshare.xtronlabs.com/job_share/index.php/ 这是调用Web服务的函数

public void post(String userid, String title, String description, int category_id, int sub_cat_id,
                 String skill_id, int postcode_id, String payment_type, double price, double hourly_rate,
                 double hours, java.io.File userfile, String deadline, boolean urgent,
                 boolean allow_comment, int bid_type) {

    HashMap<String, RequestBody> params = new HashMap<>();
    params.put("id", RequestHelper.getRequestBody(userid));
    params.put("title", RequestHelper.getRequestBody(title));
    params.put("description", RequestHelper.getRequestBody(description));
    params.put("category_id", RequestHelper.getRequestBody(category_id + ""));
    params.put("sub_cat_id", RequestHelper.getRequestBody(sub_cat_id + ""));
    params.put("skill_id", RequestHelper.getRequestBody(skill_id));
    params.put("postcode_id", RequestHelper.getRequestBody(postcode_id + ""));
    params.put("payment_type", RequestHelper.getRequestBody(payment_type));
    params.put("price", RequestHelper.getRequestBody(price == 0.0 ? " " : price + ""));
    params.put("hourly_rate", RequestHelper.getRequestBody(hourly_rate == 0.0 ? " " : hourly_rate + ""));
    params.put("hours", RequestHelper.getRequestBody(hours == 0.0 ? " " : hours + ""));
    params.put("deadline", RequestHelper.getRequestBody(deadline));
    params.put("urgent", RequestHelper.getRequestBody(urgent ? "1" : "0"));
    params.put("allow_comment", RequestHelper.getRequestBody(allow_comment ? "1" : "0"));
    params.put("bid_type", RequestHelper.getRequestBody(bid_type + ""));

    Call<PostJobResponse> call = mNetworkInterface.postJob(params, RequestHelper.multiPartBobyPart(userfile, "userfile"));
    call.enqueue(this);


}

有人知道为什么会这样吗?

1 个答案:

答案 0 :(得分:0)

您可以尝试这种方式

@Multipart
@POST("domestic/post_job")
Call<PostJobResponse> postJob(@PartMap HashMap<String, RequestBody> fields, @Part("userfile\"; filename=\"test.png\" ") RequestBody fbody);

并以这种方式创建调用对象

File file = new File(imageUri.getPath());
RequestBody fbody = RequestBody.create(MediaType.parse("image/*"), userfile);
Call<PostJobResponse> call = mNetworkInterface.postJob(params, fbody);