![](/img/trans.png)
[英]fetching data from innermost class using Retrofit. Help me to display data of classes A and B
[英]Not getting any data from Retrofit. Its sending empty request to server
我對 Retrofit 通話有疑問。 我曾調用在服務器上發布數據,調用返回 200 響應,這意味着調用成功,但它沒有將任何數據保存到數據庫並在服務器堆棧跟蹤中返回消息“請求為空”。 沒有得到響應的數據。
接口調用
@Headers({"org-id: vuk"})
@POST("/dmp/user/loginwithotp")
Call<ResponseAPI> signInWithOTP(@Body RequestBody jsonObject);
Retrofit 來電
OkHttpClient client = new OkHttpClient.Builder()
.connectionSpecs(Arrays.asList(ConnectionSpec.MODERN_TLS, ConnectionSpec.CLEARTEXT))
.addInterceptor(loggingInterceptor)
.addNetworkInterceptor(new CacheInterceptor(mContext))
.connectTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
.readTimeout(30, TimeUnit.SECONDS)
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl )
.addConverterFactory(GsonConverterFactory.create(new Gson()))
.client(client)
.build();
return retrofit;
}
public static VuAPIServices geVuAPIServices() {
VuAPIServices vuAPIServices = getRetrofit().create(VuAPIServices.class);
return vuAPIServices;
}
活動中發送調用請求和響應調用的代碼
try {
JSONObject jsonObject = new JSONObject();
jsonObject.put("mobileNumber", mobileNumber);
RequestBody body = RequestBody.create(json.toString(), MediaType.parse("application/json;charset=UTF-8"));
Call<ResponseAPI> responseAPICall = ApiClient.geVuAPIServices().signInWithOTP(body);
responseAPICall.enqueue(new Callback<ResponseAPI>() {
@Override
public void onResponse(Call<ResponseAPI> call, retrofit2.Response<ResponseAPI> response) {
if(!response.isSuccessful()) {
Log.e("TAG", "response: "+new Gson().toJson(response.body()) );
}
}
@Override
public void onFailure(Call<ResponseAPI> call, Throwable t) {
Log.e("TAG", "onFailure: "+t.toString() );
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
響應 POJO
@SerializedName("flag")
private int flag;
@SerializedName("message")
private String message;
@SerializedName("status")
private Boolean status;
@SerializedName("otp")
private String otp;
@SerializedName("locked")
private Boolean locked;
@SerializedName("firstTimeLogin")
private Boolean firstTimeLogin;
@SerializedName("firstLogin")
private Boolean firstLogin;
Getter and Setters...
Postman 圖像
我應該在我的代碼中更改什么? 我歡迎每一個提示。 我得到的狀態是 200,但服務器端的請求為空。
已更新響應結果
E/TAG: response: {"firstLogin":false,"firstTimeLogin":false,"flag":0,"fromInternalApp":false,"locked":false,"mobileNumber":"4455332266","noToken":false,"status":false}
這可能是因為您發送的是 json object。 您可以嘗試發出請求 object 並發送:
public class RequestObject {
private String mobileNumber;
public RequestObject(String mobileNumber) {
this.mobileNumber = mobileNumber;
}
public String getMobileNumber() {
return mobileNumber;
}
public setMobileNumber(String mobileNumber) {
this.mobileNumber = mobileNumber;
}
}
然后在請求中發送:
@Headers({"org-id: vuk"})
@POST("/dmp/user/loginwithotp")
Call<ResponseAPI> signInWithOTP(@Body RequestObject requestObject); // here
我已經解決了我的問題,這是我需要在 okhttp 攔截器的 header 中發送主機的問題
httpClient.addInterceptor(new Interceptor() {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request original = chain.request();
Request request = original.newBuilder()
.addHeader("Content-Type", "application/json")
.addHeader("User-Agent", System.getProperty("http.agent"))
.addHeader("Host", "localhost:8080")
.method(original.method(), original.body())
.build();
return chain.proceed(request);
}
});
在我在攔截器中添加這個之后不久,問題就解決了,並且成功返回了響應。
E/TAG: response: {"firstLogin":false,"firstTimeLogin":false,"flag":1,"fromInternalApp":false,"locked":false,"message":"true OTP sent successfully on mobile number for Initial Login","noToken":false,"otp":"573287","status":false}
感謝@rya 為幫助我所做的寶貴努力。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.