改造经常超时

时间:2018-11-14 07:22:12

标签: php android http retrofit2

我的Android应用程序有一个PHP后端,这是一个奇怪的问题;使用Postman的websevice可以正常工作,但是在应用程序中,大多数经过改造的请求都会发生504网关超时。在localhost中也可以。有什么问题吗?

G.java

public class G extends Application {

    public static APIService service;

    @Override
    public void onCreate() {
        super.onCreate();
            service= ApiClient.getClient().create(APIService.class);
    }

}

ApiClient.java

public class ApiClient {

  private static final String BASE_URL="http://*.com/caspian/index.php/";

  private static Retrofit retrofit = null;

  public static Retrofit getClient() {

    if (retrofit==null) {
      OkHttpClient client=new OkHttpClient.Builder()
        .readTimeout(60, TimeUnit.SECONDS)
        .connectTimeout(60, TimeUnit.SECONDS)
        .writeTimeout(60, TimeUnit.SECONDS)
        .build();
      retrofit = new Retrofit.Builder()
        .baseUrl(BASE_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .client(client)
        .build();

    }
    return retrofit;
  }

}

APIService.java

public interface APIService {

    @Headers({
            "content-type: application/x-www-form-urlencoded"

    })
    @POST("loginUser")
    @FormUrlEncoded
    Call<MyResponse> loginUser(@Field("action") String action,
                                 @Field("username") String username,
                                 @Field("password") String password);
}

MyResponse.java

public class MyResponse implements Serializable {

  @SerializedName("status")
  private int status;

  @SerializedName("message")
  private String message;

  public MyResponse(int status, String message) {
    this.status = status;
    this.message = message;

  } 
  public int getStatus() {
    return status;
  } 
  public int getLast_id() {return last_id;}

}

LoginActivity.php

void loginUser() {
    Call<MyResponse> call = G.service.loginUser("loginUser",edtUsername.getText().toString(),edtPassword.getText().toString());
    call.enqueue(new RetrofitCallback<>(LoginActivity.this, new Callback<MyResponse>() {
      @Override
      public void onResponse(Call<MyResponse> call, Response<MyResponse> response) {

        try {
          MyResponse token = response.body();
          Log.i("TAG", "status " + token.getStatus());

          Log.i("TAG", "message " + token.getMessage());

          }


        } catch (Exception e) {
          Toast.makeText(LoginActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
        }
      }

      @Override
      public void onFailure(Call<MyResponse> call, Throwable t) {

        Log.i("TAG", "onFailure=" + t.getLocalizedMessage() + " " + call.toString());
      }
    }, false,true
    ));


  }

login.php

<?php

        $response["status"] = 1;
        $response["message"] ='Ok';
        echo json_encode($response);

?>

我使用Retrofit2 V2.4.0

1 个答案:

答案 0 :(得分:0)

超时时间长 错误504是由于您的Internet或服务器int的速度

OkHttpClient client=new OkHttpClient.Builder()
        .readTimeout(120, TimeUnit.SECONDS)
        .connectTimeout(120, TimeUnit.SECONDS)
        .writeTimeout(120, TimeUnit.SECONDS)
        .build();