gpt4 book ai didi

android - 使用 Okhttp 发布和图像到 Django Rest Framework 后端

转载 作者:行者123 更新时间:2023-11-29 23:01:12 25 4
gpt4 key购买 nike

我有一个后端,我可以通过 Pyhton 中的以下请求将文件上传到它:

data = {
"prop_post": 35
}
headers = {
# "Content-Type": "application/json",
"Authorization": "JWT " + t1,
}
if img_path is not None:
with open(img_path, 'rb') as image:
file_data = {
'photo': image
}
r = requests.post(POSTS_ENDPOINT, data=data,files=file_data, headers=headers)

那是我的开发环境。现在在生产中,我将使用 Android 作为客户端来上传图像。我正在使用 Okhttp3。这是我用来上传图片的代码:

final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/jpg");
String file2 = utils.getRealPathFromURI2(obj.getPhotos_uri()[0], context);
File file = new File(file2);
RequestBody req = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("prop-post", Integer.toString(id))
.addFormDataPart("photo", "IMG-20190705-WA0002.jpg",
// RequestBody.create(MEDIA_TYPE_PNG, file.getAbsolutePath())).build();
RequestBody.create(MEDIA_TYPE_PNG, file)).build();

Request request = new Request.Builder()
.url(root + "images/")
.addHeader("Authorization", header)
.post(req)
.build();

OkHttpClient client = new OkHttpClient();
Response response2 = client.newCall(request).execute();

此请求未通过 400(错误请求)响应。我的文件在 Android 中的绝对位置是:/storage/emulated/0/WhatsApp/Media/WhatsApp Images/IMG-20190705-WA0002.jpg

如果有人能告诉我我做错了什么,我将不胜感激。提前致谢

最佳答案

我最终使用了改造:在 gradle 中添加了这些依赖项:

implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'

然后定义了如下接口(interface):

public interface ImageAPI {
@Multipart
@POST("images/")
Call<ResponseBody> createPost( @Part MultipartBody.Part file, @Part("prop_post") RequestBody requestBody);
}

然后我的邮寄电话看起来像这样:

OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder();
okHttpClientBuilder
.addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Request.Builder newRequest = request.newBuilder().header("Authorization", "mytoken");
return chain.proceed(newRequest.build());
}
});
Gson gson = new GsonBuilder()
.setLenient()
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(rootUrl)
.client(okHttpClientBuilder.build())
.addConverterFactory(GsonConverterFactory.create(gson))
.build();


ImageAPI imageAPI = retrofit.create(ImageAPI.class);


String image_path = imagepath;//sets when browsing image
File file = new File(image_path);

RequestBody fileReqBody = RequestBody.create(MediaType.parse("image/*"), file);
MultipartBody.Part part = MultipartBody.Part.createFormData("photo", file.getName(), fileReqBody);


RequestBody id= RequestBody.create(MediaType.parse("text/plain"), "68");



Call<ResponseBody> call = imageAPI.createPost(part, id);
try {
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
Log.d("tag", "onResponse: " + response.message().toString());
}

@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.d("tag", "onResponse: " + t.getMessage());

}
});
} catch (Exception e) {
Log.d("tag", "onCreate: " + e.getMessage());
}

关于android - 使用 Okhttp 发布和图像到 Django Rest Framework 后端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56910769/

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