简体   繁体   English

没有从 Retrofit 获得任何数据。 它向服务器发送空请求

[英]Not getting any data from Retrofit. Its sending empty request to server

I have a problem with Retrofit call.我对 Retrofit 通话有疑问。 I had call to post data on server, call is giving me back 200 response that means call is successful, but it's not saving any data to database and returning message "request is empty" in server stacktrace.我曾调用在服务器上发布数据,调用返回 200 响应,这意味着调用成功,但它没有将任何数据保存到数据库并在服务器堆栈跟踪中返回消息“请求为空”。 Getting no data in response.没有得到响应的数据。

Interface call接口调用

  @Headers({"org-id: vuk"})
 @POST("/dmp/user/loginwithotp")
    Call<ResponseAPI> signInWithOTP(@Body RequestBody jsonObject);

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

Code for send call request and response call in activity活动中发送调用请求和响应调用的代码

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

Response POJO响应 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 Image Postman 图像

邮递员回应

What should I change in my code?我应该在我的代码中更改什么? I welcome every hint.我欢迎每一个提示。 The status im getting is 200 but with empty request on server side.我得到的状态是 200,但服务器端的请求为空。

Updated with Response Result已更新响应结果

E/TAG: response: {"firstLogin":false,"firstTimeLogin":false,"flag":0,"fromInternalApp":false,"locked":false,"mobileNumber":"4455332266","noToken":false,"status":false}

It could be because you are sending a json object.这可能是因为您发送的是 json object。 You can try making a request object instead and send that:您可以尝试发出请求 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;
}
}

And then send it in the request:然后在请求中发送:

 @Headers({"org-id: vuk"})
 @POST("/dmp/user/loginwithotp")
 Call<ResponseAPI> signInWithOTP(@Body RequestObject requestObject); // here

I have solved my problem it was a problem that i needed to send a host in header of okhttp interceptor我已经解决了我的问题,这是我需要在 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);
    }
});

Soon after i added this in interceptor the problem solved and it got the response returned successfully.在我在拦截器中添加这个之后不久,问题就解决了,并且成功返回了响应。

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}

Thanks @rya for your valuable efforts with you tried to help me.感谢@rya 为帮助我所做的宝贵努力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM