Часто тайм-аут в модернизации - PullRequest
0 голосов
/ 14 ноября 2018

У меня есть бэкэнд PHP для моего приложения для Android, есть странная проблема; С почтальоном веб-сервис работает нормально, но в приложении большинство запросов с модификацией происходит 504 Gateway Timeout. Это также нормально в 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 голосов
/ 14 ноября 2018

Время ожидания истекло Ошибка 504 из-за скорости вашего интернета или сервера int

OkHttpClient client=new OkHttpClient.Builder()
        .readTimeout(120, TimeUnit.SECONDS)
        .connectTimeout(120, TimeUnit.SECONDS)
        .writeTimeout(120, TimeUnit.SECONDS)
        .build();
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...