【问题标题】:GET request with parameters using Retrofit Android使用 Retrofit Android 获取带参数的请求
【发布时间】:2019-07-18 16:01:43
【问题描述】:

我有 4 个参数的 API 并使用改造获取请求。我的 API 没问题。我已经通过 Postman 对其进行了测试。但是当我调用我的 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 响应的屏幕截图。

结果.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;
}}

来电

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

这是改造客户端和基本网址

改造引擎

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 个参数,但总是响应 状态:错误

请查看邮递员回复:

这是我的回复并发送带有 4 个参数的 GET 请求,因此显示结果正确但改装显示状态:错误

这是 android studio 中的改造响应:

【问题讨论】:

  • 在 POSTMAP 中,如果是 GET 请求,为什么要传递 Header 中的所有参数。即使您想通过这种方式,也可以使用标头调用 Retrofit 请求。
  • 解决...服务器端的参数限制

标签: java android retrofit2


【解决方案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

【讨论】:

【解决方案2】:

在 GET 方法中,我们无法从手机发布参数,因此您可以在 url 末尾传递参数

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

例如:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-22
    • 2019-12-04
    • 1970-01-01
    • 1970-01-01
    • 2011-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多