gpt4 book ai didi

java - 无法将照片上传到服务器android retrofit 2

转载 作者:行者123 更新时间:2023-11-30 04:55:20 25 4
gpt4 key购买 nike

我正在尝试将照片上传到后端开发人员提供此 json 文件的服务器:

{"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"}]}

如果我使用这种方法,它会以不带照片的字符串形式上传数据:

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
);

但是如果我用这个方法上传数据就失败了:

@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);

然后我得到这个日志:

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 field is required."],"Address":["The address field is required."]}} D/OkHttp: <-- END HTTP (474-byte body)

postman enter image description here

最佳答案

您应该使用 multipart/form-data 而不是 application/json;charset=UTF-8

因为如果您拦截底层 OkHttp 客户端并将内容类型更改为 application/json,您的服务器可能会在反序列化过程中出现问题。

有关更多信息,请参阅 Upload Files With Retrofit 2

关于java - 无法将照片上传到服务器android retrofit 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59288121/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com