Tousif Irshad
Tousif Irshad

Reputation: 37

GET request with parameters using Retrofit Android

I have API with 4 parameters & Get request using retrofit. My API is okay. I've tested it via Postman. But when I'm calling my API it's not giving the correct result.

API.JAVA

public interface Api {
   @GET("MAttendance/api/CheckInOut/CheckInOut")
   Call<SignUpResponse> tracking(
        @Query("UserID") int user_id,
        @Query("Latitude") String Latitude,
        @Query("Longitude") String Longitude,
        @Query("CheckType") String CheckType,
        @Query("CheckTime") String CheckTime);}

Please check the Screenshot for JSON response.

Result.java

public class Result1 {
@SerializedName("Status")
@Expose
private String Status;
@SerializedName("ID")
@Expose
private int ID;

public String getStatus() {
    return Status;
}
public void setStatus(String status) {
    Status = status;
}
public int getID() {
    return ID;
}
public void setID(int ID) {
    this.ID = ID;
}}

Here is the Call

Fragment.java

Api userAPICall = RetroFitEngine.getRetrofit().create(Api.class);
    Call<Result1> callEnqueue = userAPICall.tracking(userId, latitude, longitude, checkin, timeDate);
    callEnqueue.enqueue(new Callback<Result1>() {
        @Override
        public void onResponse(Call<Result1> call, Response<Result1> response) {
            result1 = response.body();
            Toast.makeText(getActivity(), "response:" + result1, 
            Toast.LENGTH_SHORT).show();
             if (result1 != null) {
                 if (result1.getSttatus().equals("SUCCESS")) {
                     Toast.makeText(getActivity(), "sucessfully", 
                     Toast.LENGTH_SHORT).show();
                }
                Toast.makeText(getActivity(), result1.getSttatus(), 
                Toast.LENGTH_SHORT).show();
            }
        }
        @Override
        public void onFailure(Call<Result1> call, Throwable t) {

            Toast.makeText(getActivity(), "Failure Response", 
                                Toast.LENGTH_SHORT).show();
        }
    });
}

Here is Retrofit Client & Base Url

RetrofitEngine

public class RetroFitEngine {

public static String baseUrl = "http://global.timetick.ae/";

public static OkHttpClient getClient() {
    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient client = new OkHttpClient.Builder()
            .addInterceptor(interceptor)
            .connectTimeout(10, TimeUnit.SECONDS)
            .readTimeout(60, TimeUnit.SECONDS)
            .retryOnConnectionFailure(true)
            .build();
    client.connectionPool().evictAll();
    return client;
}

public static Retrofit getRetrofit() {
    OkHttpClient client = getClient();
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(baseUrl)
            .addConverterFactory(GsonConverterFactory.create())
            .client(client)
            .build();

    return retrofit;
}

}

I'm trying to send 4 parameters by using retrofit but response always Status: ERROR

Please check Postman Response:

Here is My response and send GET request with 4 parameters so there is showing result correct but retrofit show Status:ERROR

Image 1

Here is Retrofit response in android studio:

Image 2

Upvotes: 0

Views: 5148

Answers (2)

Saravanan
Saravanan

Reputation: 1

In GET method we can't post the parameters from mobile so you can pass the parameter at the end your url

userid=8;Latitude=12.022;Longitude=32.02552;CheckType=2

for example:

http://global.timetick.ae/MAttendance/api/CheckInOut/CheckInOut/8/12.0022/32.0025/2

Upvotes: 0

Taras Parshenko
Taras Parshenko

Reputation: 604

You just need to replace @Query to @Header

public interface Api {
   @GET("MAttendance/api/CheckInOut/CheckInOut")
   Call<SignUpResponse> tracking(
        @Header("UserID") int user_id,
        @Header("Latitude") String Latitude,
        @Header("Longitude") String Longitude,
        @Header("CheckType") String CheckType,
        @Header("CheckTime") String CheckTime);
}

With @Query your request was: http://global.timetick.ae/MAttendance/api/CheckInOut/CheckInOut?UserID=2&Latitude=111&Longitude=333&CheckType=OUT&CheckTime=2019-02-21%2011:11:11.000

Upvotes: 1

Related Questions