Search code examples
androidrequestretrofit2okhttpjsonresponse

I'm Trying to Method GET Using Parameter using retrofit2


When I request a GET method using parameter of user uid, response come (my package name) For example my package name is "com.android.gms.sample.Model.time_hour" and response "com.android.gms.sample.Model.time_hour@acf38b". I am not able to understand where and what is happening. Could anyone please point me to the mistake?

My JSON Response when write userid with url

{
    "hours": 0,
    "status": "success",
    "successMsg": "Time spent today",
    "status_code": 200
}

/Interface API Class/

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

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

Request Method

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

            }
        });
    }    

/Model 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;
}}

Solution

  • You're logging the object itself, you can log the details by:

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

    You can also override the toString() method on the TimeHour object, try to add this:

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

    and then call:

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