我正在尝试使用retrofit2方法GET使用参数

时间:2018-01-23 07:50:55

标签: android request retrofit2 okhttp jsonresponse

当我使用用户uid的参数请求GET方法时,响应来(我的包名)例如我的包名是“com.android.gms.sample.Model.time_hour”和响应“com.android.gms。 sample.Model.time_hour@acf38b”。我无法理解发生在哪里和发生了什么。有人可以指出我的错误吗?

使用url

编写userid时的My JSON响应
{
    "hours": 0,
    "status": "success",
    "successMsg": "Time spent today",
    "status_code": 200
}

/ 接口API类 /

 public interface RestInterface {  
     String url2 = "http://beaconites.com/api/v2/";

     @GET ("admin/dailyHours/{uid}")
     Call<time_hour> GetTimeHour(@Path("uid") int uid);
}

请求方法

   public void GetTime1() {

       Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(RestInterface.url2)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
       RestInterface service = retrofit.create(RestInterface.class);

       Call<TimeHour> call = service.GetTimeHour( 13);

       call.enqueue(new Callback<TimeHour>() {
           @Override
           public void onResponse(Call<TimeHour> call, Response<TimeHour> response) {
               progressDialog.dismiss();
               Toast.makeText(MainActivity.this, "Response:" + response.body(), Toast.LENGTH_SHORT).show();

               TimeHour p = response.body();

           }

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

               Toast.makeText(getApplicationContext(), "Invalid Data", Toast.LENGTH_LONG).show();

            }
        });
    }    

/ 模型类 /

public class TimeHour {
@SerializedName("hours")
@Expose
private Integer hours;

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

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

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

public  TimeHour(int hours, String status, String successMsg, String     status_code){
    this.hours=hours;
    this.status=status;
    this.successMsg=successMsg;
    this.status_code=status_code;
}
public TimeHour(){}

public int getHours() {
    return hours;
}

public void setHours(int hours) {
    this.hours = hours;
}

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

public String getSuccessMsg() {
    return successMsg;
}

public void setSuccessMsg(String successMsg) {
    this.successMsg = successMsg;
}

public String getStatus_code() {
    return status_code;
}
public void setStatus_code(String status_code) {
    this.status_code = status_code;
}}

1 个答案:

答案 0 :(得分:0)

您正在记录对象本身,您可以通过以下方式记录详细信息:

TimeHour p =response.body();
Toast.makeText(MainActivity.this, "Response:"+ p.getHours(), Toast.LENGTH_SHORT).show();

//p.getStatus(), p.getgetSuccessMsg(), etc

您还可以覆盖toString()对象上的TimeHour方法,尝试添加此内容:

@Override
public String toString() {
    return "{ \"hours\": " + getHours() + ", \"status\": "+ getStatus() +", \"successMsg\": " + getSuccessMsg() + ", \"status_code\": " + getStatus_code() + "}";
}

然后致电:

Toast.makeText(MainActivity.this, "Response:"+ response.body().toString(), Toast.LENGTH_SHORT).show();