繁体   English   中英

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

[英]Need to get a string from response returned by api using retrofit2

我正在尝试获取成功返回的API返回的字符串值。 我获得了成功,但没有在响应中获得所需的值,但是当我导航至response-> body-> 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();
    }

}

在此处输入图片说明

更改您的代码:

从:

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参数中写入。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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