【问题标题】:Not getting any data from Retrofit. Its sending empty request to server没有从 Retrofit 获得任何数据。它向服务器发送空请求
【发布时间】:2021-04-03 22:06:20
【问题描述】:

我对 Retrofit 调用有疑问。我曾调用在服务器上发布数据,调用返回 200 响应,这意味着调用成功,但它没有将任何数据保存到数据库并在服务器堆栈跟踪中返回消息“请求为空”。没有得到响应数据。

接口调用

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

改造电话

    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...

邮递员图片

我应该对我的代码进行哪些更改?我欢迎每一个提示。我得到的状态是 200 但服务器端的请求为空。

已更新响应结果

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

【问题讨论】:

  • postman发送请求时是否保存数据?
  • 是的,它会在邮递员发送请求时保存
  • 谁能帮我解决这个问题?

标签: java android json api postman


【解决方案1】:

我已经解决了我的问题,这是我需要在 okhttp 拦截器的标头中发送主机的问题

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 为帮助我所做的宝贵努力。

【讨论】:

    【解决方案2】:

    这可能是因为您正在发送一个 json 对象。您可以尝试创建一个请求对象并将其发送:

    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
    

    【讨论】:

    • 我已尽一切努力使其工作,但它在服务器中仅返回空请求,而邮递员中具有相同参数的相同请求调用工作正常并将数据保存在数据库中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-20
    • 2023-04-03
    • 1970-01-01
    相关资源
    最近更新 更多