使用Retrofit Android的带有参数的GET请求

时间:2019-02-25 09:25:52

标签: java android retrofit2

我有带有4个参数的API,并使用改型获得请求。我的API没问题。我已经通过邮递员对其进行了测试。但是,当我调用我的API时,并没有给出正确的结果。

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

请检查屏幕快照以获取JSON响应。

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

这是电话

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

以下是翻新客户端和基本网址

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

}

我正在尝试通过翻新发送4个参数,但总是响应 状态:ERROR

请检查邮递员回复:

这是我的回复,并发送带有4个参数的GET请求,因此显示的结果正确,但翻新后显示Status:ERROR

Image 1

这是android studio中的改造响应:

Image 2

2 个答案:

答案 0 :(得分:1)

您只需要将@Query替换为@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);
}

对于@Query,您的要求是: http://global.timetick.ae/MAttendance/api/CheckInOut/CheckInOut?UserID=2&Latitude=111&Longitude=333&CheckType=OUT&CheckTime=2019-02-21%2011:11:11.000

答案 1 :(得分:0)

在GET方法中,我们无法从移动设备发布参数,因此您可以在网址末尾传递参数

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

例如:

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