2017-07-29 48 views
0

我使用Retrofit v2.0.2。我無法得到JSON Response我得到這樣的錯誤[email protected],請任何人在這種情況下幫助我。如何在改進版本2.0.2中獲得Json響應?

這是接口:

public interface IdeaInterface { 

    /*Get zoontype for registeration*/ 
    @GET("api/user/get_zone") 
    Call<ResponseBody> display(); 

} 

調用改造方法在這裏。

public void Get_Zoontype() 
    { 
     reg_progressbar.setVisibility(View.VISIBLE); 

     /* RestAdapter adapter = new RestAdapter.Builder() 
       .setEndpoint(Constandapi.ROOT_URL) //Setting the Root URL 
       .build();*/ 
     // Retrofitnew Retrofit.Builder(); 

     Retrofit adapter = new Retrofit.Builder() 
       .baseUrl(Constandapi.ROOT_URL) 
       .build(); 

     IdeaInterface report = adapter.create(IdeaInterface.class); 

     Call<ResponseBody> call = report.display(); 

     call.enqueue(new Callback<ResponseBody>() { 
      @Override 
      public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { 

        Log.d("reponcevalues","**** "+response.toString()); 
      } 

      @Override 
      public void onFailure(Call<ResponseBody> call, Throwable t) { 

      } 
     }); 

這是我的基本網址:

public class Constandapi { 

    public static final String ROOT_URL = "http://vehiclerescue.in/ideadarpan_beta/"; 
} 

我的build.gradle其中有改裝庫提前

compile 'com.squareup.retrofit2:retrofit:2.0.2' 
compile 'com.squareup.okhttp3:logging-interceptor:3.0.1' 

感謝。

+0

https://stackoverflow.com/questions/37589588/retrofit-get-string-response –

+0

@NaveenKumar你不覺得我的代碼和你的答案一樣嗎 – Mariyappan

+1

使用這段代碼打印日誌response.body()。string()ins tead response.toString() –

回答

-1

改造代碼,它有上傳模型類。

Call<Uploads> uploadsCall = service.profilePicUpload(body, id); 
    uploadsCall.enqueue(new Callback<Uploads>() { 
     @Override 
     public void onResponse(Call<Uploads> call, Response<Uploads> response) { 
      response.body(); 
      if (response.isSuccessful()) { 
       String s = response.body().getMsg(); 
       Communicate.showToast(MyAccountActivity.this, s); 
       ImageHelper.getImage(MyAccountActivity.this, AppController.ProficPic + profpic, profilePic, 
         R.drawable.ic_user, R.drawable.ic_user, new ImageHelper.Callback() { 
          @Override 
          public void Success() { 
           runOnUiThread(new Runnable() { 
            @Override 
            public void run() { 
             profpicProgress.setVisibility(View.GONE); 
            } 
           }); 
          } 
          @Override 
          public void Error() { 
           runOnUiThread(new Runnable() { 
            @Override 
            public void run() { 
             profpicProgress.setVisibility(View.GONE); 
            } 
           }); 
          } 
         }); 
      } else { 
       Communicate.showToast(MyAccountActivity.this, "please try again"); 
       profpicProgress.setVisibility(View.GONE); 
      } 
     } 

     @Override 
     public void onFailure(Call<Uploads> call, Throwable t) { 
      profpicProgress.setVisibility(View.GONE); 
      Communicate.showToast(MyAccountActivity.this, "error"); 
      Log.e(AppController.TAG, "onFailure: " + t.getLocalizedMessage()); 
      Log.e(AppController.TAG, "onFailure: " + t.getMessage()); 
      t.printStackTrace(); 
     } 
    }); 

在這裏,我有一個模型類,我越來越味精stud_id作爲JSON對象

public class Uploads { 
@SerializedName("msg") 
@Expose 
private String msg; 

@SerializedName("stud_id") 
@Expose 
private String stud_id; 

public String getMsg() { 
    return msg; 
} 

public void setMsg(String msg) { 
    this.msg = msg; 
} 

public String getStud_id() { 
    return stud_id; 
} 

public void setStud_id(String stud_id) { 
    this.stud_id = stud_id; 
} 

}

合這將幫助你