【问题标题】:Server Timeout for Android Retrofit Post RequestsAndroid Retrofit Post 请求的服务器超时
【发布时间】:2018-01-25 11:14:19
【问题描述】:

当用户想要注册时,我会向包含用户详细信息的数据发送一些数据。我尝试使用 Postman 并成功添加记录。我发出的任何发布请求都会返回超时,但数据仍会发送到服务器。可能是什么问题,因为我的 GET 请求没有任何问题。

这是我下面的 API 服务:

@POST("/api/account/usersignup/")
@FormUrlEncoded
Call<LendingResponse> savePost(@Field("phoneNumber") String phoneNumber,
                    @Field("name") String name,
                    @Field("email") String email,
                    @Field("password") String password,
                    @Field("confirmPassword") String confirmPassword);

这是我保存调用以下 API 的方法:

public void sendPost(String userPhone, String userName, String userEmail, String userPassword, String userConfirmPassword) {


    final ProgressDialog loading = ProgressDialog.show(this, "Registering", "Please wait...", false, false);
    loading.show();

    mAPIService.savePost(userPhone, userName, userEmail, userPassword, userConfirmPassword).enqueue(new Callback<LendingAppResponse>() {
        @Override
        public void onResponse(Response<LendingAppResponse> response, Retrofit retrofit) {
            if(response.body().getSuccess().equals("true")) {
                Intent intent = new Intent(SignUpActivity.this, ValidatePhone.class);
                intent.putExtra("phone", userPhone);
                startActivity(intent);
                loading.dismiss();
            }
            else {
                String message = response.body().getMessage();
                Toast.makeText(SignUpActivity.this, message, Toast.LENGTH_LONG).show();
                loading.dismiss();
            }
        }

        @Override
        public void onFailure(Throwable throwable) {
            String error = throwable.getMessage();
            Toast.makeText(SignUpActivity.this, throwable.getMessage() + " Please try again", Toast.LENGTH_SHORT).show();
            loading.dismiss();
        }
    });
}

我已经搜索过,但似乎找不到任何好的解决方案。

【问题讨论】:

  • 你的基本网址对吗?
  • 我的基本网址是正确的,因为 GET 请求可以完美运行
  • 服务器发送响应的时间过长
  • @TimCastelijns 在这种情况下我该怎么办..或者它必须是服务器端问题。
  • 你能发布超时异常吗?您是否从服务器中删除了所有断点?

标签: java android asynchronous retrofit retrofit2


【解决方案1】:

解决方案:通过 Lending Square 延长超时时间

为 httpClient 配置 ReadTimeout 和 ConnectTimeout 一个例子:

   httpClient = new OkHttpClient();
   httpClient.setReadTimeout(60 * 1000, TimeUnit.MILLISECONDS);
   httpClient.setConnectTimeout(60 * 1000, TimeUnit.MILLISECONDS);

【讨论】:

  • 去掉反冲后还是一样
  • 当我使用 HttpLoggingInterceptor 时,这是响应 01-25 00:33:24.414 4272-4305/com.example D/OkHttp: --> POST /api/account/usersignup HTTP/1.1 01 -25 00:33:24.414 4272-4305/com.example D/OkHttp: phoneNumber=6756435287&name=jonathanLa&email=joeyL%40g.com&password=chickenjoe&confirmPassword=chickenjoe 01-25 00:33:24.414 4272-4305/com.example D/ OkHttp: --> END POST (105-byte body)
  • 它是通过邮政。我通过延长超时时间找到了解决方案
猜你喜欢
  • 2016-12-26
  • 2016-12-26
  • 1970-01-01
  • 2016-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-25
  • 2017-05-30
相关资源
最近更新 更多