我正在尝试使用 retrofit2 方法获取使用参数
I'm Trying to Method GET Using Parameter using retrofit2
当我使用用户 uid 参数请求 GET 方法时,响应来了(我的包名)例如我的包名是 "com.android.gms.sample.Model.time_hour" 和响应 "com.android.gms.sample.Model.time_hour@acf38b"。我不明白发生了什么事。谁能指出我的错误?
我的 JSON 写入用户 ID 时的响应 url
{
"hours": 0,
"status": "success",
"successMsg": "Time spent today",
"status_code": 200
}
/接口APIClass/
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();
}
});
}
/型号Class/
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;
}}
您正在记录对象本身,您可以通过以下方式记录详细信息:
TimeHour p =response.body();
Toast.makeText(MainActivity.this, "Response:"+ p.getHours(), Toast.LENGTH_SHORT).show();
//p.getStatus(), p.getgetSuccessMsg(), etc
你也可以覆盖 TimeHour
对象上的 toString()
方法,尝试添加这个:
@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();
当我使用用户 uid 参数请求 GET 方法时,响应来了(我的包名)例如我的包名是 "com.android.gms.sample.Model.time_hour" 和响应 "com.android.gms.sample.Model.time_hour@acf38b"。我不明白发生了什么事。谁能指出我的错误?
我的 JSON 写入用户 ID 时的响应 url
{
"hours": 0,
"status": "success",
"successMsg": "Time spent today",
"status_code": 200
}
/接口APIClass/
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();
}
});
}
/型号Class/
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;
}}
您正在记录对象本身,您可以通过以下方式记录详细信息:
TimeHour p =response.body();
Toast.makeText(MainActivity.this, "Response:"+ p.getHours(), Toast.LENGTH_SHORT).show();
//p.getStatus(), p.getgetSuccessMsg(), etc
你也可以覆盖 TimeHour
对象上的 toString()
方法,尝试添加这个:
@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();