簡體   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