简体   繁体   中英

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

I have a problem with Retrofit call. 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. Getting no data in response.

Interface call

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

Retrofit Call

    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

    @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

邮递员回应

What should I change in my code? I welcome every hint. The status im getting is 200 but with empty request on server side.

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. You can try making a request object instead and send that:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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