简体   繁体   中英

Can't Upload Photo to server android retrofit 2

I'm trying to upload photo to server where the backend developer provide this json file:

{"name":"update-profilePatient",
 "request":{ 
  "method":"POST",
   "header":[],
   "body":{ 
   "mode":"formdata",
   "formdata":[ 
    {"key":"user_id","value":"","description":"type user id = 8 patient","type":"text"},
    { "key":"image","type":"file","src":[]},
    { "key":"firstname","value":"","type":"text"},
    {"key":"lastname",value":"","type":"text"}]}

If I use this method, it upload data as string without photo:

HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(logging);
Retrofit re = new Retrofit.Builder().baseUrl(ApiUrl).addConverterFactory(GsonConverterFactory.create()).client(httpClient.build()).build();
MyApi api = re.create(MyApi.class);
Map<String, String> sMap = new HashMap<>();
sMap.put("user_id", info.getId() + "");
sMap.put("firstname", info.getFirst_name());
sMap.put("lastname", info.getLast_name());
sMap.put("birthday", info.getBirthday());
sMap.put("phone", info.getPhone());
sMap.put("gender", info.getGender());
sMap.put("city_id", info.getCity_id());
sMap.put("area_id", "1");
sMap.put("Address", info.getAddress());
sMap.put("latitude", info.getLatitude() + "");
sMap.put("longitude", info.getLongitude() + "");
sMap.put("image", photo(info.getPhoto().replace(photoLink, "")));
sMap.put("other", "other");
Call<ResponseBody> request = api.updatePatientsWithePhoto(sMap);
request.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(@NotNull Call<ResponseBody> call, @NotNull Response<ResponseBody> response) {Log.e("EditPaWithPhoto", "onResponse: " + response);
}
@Override
public void onFailure(@NotNull Call<ResponseBody> call, @NotNull Throwable t) {
Log.e("EditPaWithPhoto", "Throwable: " + t.getMessage());
}});

// intrface Code
@POST("update-profilePatient")
@Headers({"Content-Type: application/json;charset=UTF-8"})
Call<ResponseBody> updatePatientsWithePhoto
        (
                @Body Map<String,String> json
        );

But if I use this method to upload data it failed:

@Multipart
@POST("update-profilePatient")
@Headers({"Content-Type: application/json;charset=UTF-8"})
Call<ResponseBody> updatePatientsWithePhoto
(@Part("user_id") RequestBody user_id,
 @Part("firstname") RequestBody firstname,
 @Part("lastname") RequestBody lastname,
 @Part("birthday") RequestBody birthday,
 @Part("phone") RequestBody phone,
 @Part("gender") RequestBody gender,
 @Part("city_id") RequestBody city_id,
 @Part("area_id") RequestBody area_id,
 @Part("Address") RequestBody Address,
 @Part("latitude") RequestBody latitude,
 @Part("longitude") RequestBody longitude,
 @Part("other") RequestBody other,@Part MultipartBody.Part image);

File photoFile = new File(info.getPhoto().replace(photoLink, ""));
RequestBody fileBody = RequestBody.create(MediaType.parse("image/*"), photoFile);
MultipartBody.Part filePart = MultipartBody.Part.createFormData("image", photoName,fileBody );
Call<ResponseBody> request = api.updatePatientsWithePhoto(
toRequestBody(info.getId()+""),
toRequestBody(info.getFirst_name()),
toRequestBody(info.getLast_name()),
toRequestBody(info.getBirthday()),
toRequestBody(info.getPhone()),
toRequestBody(info.getGender()),
toRequestBody(info.getCity_id()),
toRequestBody("1"),
toRequestBody(info.getAddress()),
toRequestBody(info.getLatitude()+""),
toRequestBody(info.getLongitude()+""),
toRequestBody("1"),
filePart);

Then I get this log:

D/OkHttp: --e64d0c37-dddf-4a7e-a5ed-d6104271a7c4-- D/OkHttp: --> END POST (2734-byte body) D/OkHttp: <-- 401 Unauthorized MyUrl/api/update-profilePatient (1325ms) D/OkHttp: Date: Sun, 08 Dec 2019 09:28:21 GMT D/OkHttp: Server: Apache D/OkHttp: Cache-Control: no-cache, private D/OkHttp: X-RateLimit-Limit: 60 D/OkHttp: X-RateLimit-Remaining: 55 D/OkHttp: Upgrade: h2,h2c D/OkHttp: Connection: Upgrade, Keep-Alive D/OkHttp: Vary: Accept-Encoding D/OkHttp: Referrer-Policy: no-referrer-when-downgrade D/OkHttp: Keep-Alive: timeout=5, max=75 D/OkHttp: Content-Type: application/json D/OkHttp:{"status":false,"error":{"user_id":["The user id field is required."],"firstname":["The firstname field is required."],"lastname":["The lastname field is required."],"birthday":["The birthday field is required."],"phone":["The phone field is required."],"gender":["The gender field is required."],"city_id":["The city id field is required."],"area_id":["The area id field is required."],"other":["The other fiel d is required."],"Address":["The address field is required."]}} D/OkHttp: <-- END HTTP (474-byte body)

postman enter image description here

You should use multipart/form-data instead of application/json;charset=UTF-8 .

Because If you intercept the underlying OkHttp client and change the content type to application/json, your server might have issues with the deserialization process.

For more info refer Upload Files With Retrofit 2

this resolve the problem thx

intrface code

@Multipart
@POST("update-profilePatient")
Call<ResponseBody> updatePatientsWithePhoto
(@Part("user_id") RequestBody user_id,
 @Part("firstname") RequestBody firstname,
 @Part("lastname") RequestBody lastname,
 @Part("birthday") RequestBody birthday,
 @Part("phone") RequestBody phone,
 @Part("gender") RequestBody gender,
 @Part("city_id") RequestBody city_id,
 @Part("area_id") RequestBody area_id,
 @Part("Address") RequestBody Address,
 @Part("latitude") RequestBody latitude,
 @Part("longitude") RequestBody longitude,
 @Part("other") RequestBody other,@Part MultipartBody.Part image);

// call like this

Call<ResponseBody> request = api.updatePatientsWithePhoto(
                toRequestBody(info.getId()+""),
                toRequestBody(info.getFirst_name()),
                toRequestBody(info.getLast_name()),
                toRequestBody(info.getBirthday()),
                toRequestBody(info.getPhone()),
                toRequestBody(info.getGender()),
                toRequestBody(info.getCity_id()),
                toRequestBody("1"),
                toRequestBody(info.getAddress()),
                toRequestBody(info.getLatitude()+""),
                toRequestBody(info.getLongitude()+""),
                toRequestBody("1"),
                filePart);
        //Call<ResponseBody> request = api.updatePatientsWithePhoto(sMap,fileBody);
        request.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(@NotNull Call<ResponseBody> call
                    , @NotNull Response<ResponseBody> response) {
                Log.e("EditPaWithPhoto", "onResponse: " + response);
            }
            //
            @Override
            public void onFailure(@NotNull Call<ResponseBody> call, @NotNull Throwable t) {
                Log.e("EditPaWithPhoto", "Throwable: " + t.getMessage());

            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM