代码之家  ›  专栏  ›  技术社区  ›  Tousif Irshad

我正在尝试使用改型2使用参数获取方法

  •  0
  • Tousif Irshad  · 技术社区  · 8 年前

    当我使用user uid参数请求GET方法时,会出现响应(我的包名),例如,我的包名是“com.android.gms.sample.Model.time\u hour”,响应是“com.android.gms.sample.Model.time”_hour@acf38b“。我不知道发生了什么,发生了什么。有人能告诉我错误吗?”?

    使用url写入用户ID时我的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 回复  |  直到 7 年前
        1
  •  0
  •   Tenten Ponce    8 年前

    如果要记录对象本身,可以通过以下方式记录详细信息:

    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();