gpt4 book ai didi

java - 需要使用retrofit2从api返回的响应中获取字符串

转载 作者:行者123 更新时间:2023-12-02 01:37:36 24 4
gpt4 key购买 nike

我正在尝试获取 API 成功返回的字符串值。我取得了成功,但我没有在响应中获得所需的值,但是当我导航到响应->正文->responseBody时,它会显示该值,但我无法获取该值(请检查屏幕截图以了解详细信息) .

private void uploadImage(String imagePath) {

try{
showProgressDialogue();
File file = new File(imagePath);
RequestBody photoContent = RequestBody.create(MediaType.parse("multipart/form data"), file);
MultipartBody.Part photo = MultipartBody.Part.createFormData("file",file.getName(),photoContent);
//RequestBody description = RequestBody.create(MediaType.parse("description"),"abc");
UploadService uploadService = APIClient.getClient().create(UploadService.class);
Call call1 = uploadService.UploadOMRExamPaper(photo);
call1.enqueue(new Callback<Response>() {
@Override
public void onResponse(Call<Response> call, Response<Response> response) {
progressBar.dismiss();
}

@Override
public void onFailure(Call<Response> call, Throwable t) {
progressBar.dismiss();
Toast.makeText(getApplicationContext(), t.getMessage(),Toast.LENGTH_LONG).show();
}
});

}catch (Exception e){
progressBar.dismiss();
Toast.makeText(this, e.getMessage(),Toast.LENGTH_LONG).show();
}

}

enter image description here

最佳答案

更改您的代码:

来自:

File file = new File(imagePath);
RequestBody photoContent = RequestBody.create(MediaType.parse("multipart/form data"), file);
MultipartBody.Part photo = MultipartBody.Part.createFormData("file",file.getName(),photoContent);

致:

File file = new File(imagePath);
RequestBody photoContent = RequestBody.create(MediaType.parse("image/*"), file);
MultipartBody.Part photo = MultipartBody.Part.createFormData("upload", file.getName(), photoContent );

注意:“上传”只是这里的示例,您应该从您的 api 参数中写入。

关于java - 需要使用retrofit2从api返回的响应中获取字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57509527/

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