需要使用 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 );

注意:"upload"只是这里的例子,你应该根据你的api参数写在那里。