我有带有 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;
}}
电话来了
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();
}
});
}
这是 Retrofit 客户端和基本网址
改造引擎
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 个参数,但始终响应 状态:错误
请检查 postman 回复:
这是我的响应并发送带有 4 个参数的 GET 请求,因此显示结果正确,但改造显示状态:错误
这是 android studio 中的 Retrofit 响应:
最佳答案
您只需将@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);
}
关于java - 使用 Retrofit Android 获取带参数的请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54862999/