0

my application that interacts with a sql server works correctly on my emulator and a HUAWEI tablet version 7.x, but it fails to connect to the server on a Samsung tablet version 10+ and cell phone version 10+. I think it's a permission.INTERNET error or another one if someone can help me.

here are my codes and the log

Manifest

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission-sdk-23 android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission-sdk-23 android:name="android.permission.ACCESS_NETWORK_STATE"/>

ApiClient

public class ApiClient {

public static final String BASE_URL = "http://XXX.XXX.XXX.XXX:XXXX/";
public static Retrofit retrofit = null;

public static Retrofit getRetrofitInstance() {
    if (retrofit ==null){
        return new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    return retrofit;
}

ApiInterface

@FormUrlEncoded
@POST("register.php")
Call<Contact> registerContact(
        @Field("FirstName") String firstname_,
        @Field("LastName") String lastname_,
        @Field("ContactAddress") String adresse_,
        @Field("Mobile") String tel_,
        @Field("BirthDate") String BirthDate_
);

Register method

private void GoRegister() {

    String firstname_ = this.firstname.getText().toString().trim();
    String lastname_ = this.lastname.getText().toString().trim();
    String adresse_ = this.adress.getText().toString().trim();
    String tel_ = this.tel.getText().toString().trim();
    String BirthDate_= this.BirthDate.getText().toString().trim();

    final ProgressDialog dialog = new ProgressDialog(this);
    dialog.setMessage("Please Wait ...");
    dialog.show();

    apiInterface = ApiClient.getRetrofitInstance().create(ApiInterface.class);
    Call<Contact> call = apiInterface.registerContact(firstname_, lastname_, adresse_, tel_, BirthDate_);
   
    call.enqueue(new Callback<Contact>() {
        private Call<Contact> call;
        private Throwable t;
        @Override
        public void onResponse(@NonNull Call<Contact> call, @NonNull Response<Contact> response) {
            if(response.isSuccessful())
            {
                alertSuccess("Enregistrement effectuée");
                dialog.dismiss();

            }else{
                alertEnregistrementFailed("Une erreur est survenue lors de l'enregistrement, veuiller réesayez");
                dialog.dismiss();
            }
        }

        @Override
        public void onFailure(@NonNull Call<Contact> call, @NonNull Throwable t) {
            alertConnexion("Echec de connection aux serveur");
            dialog.dismiss();
        }
    });
}
brombeer
  • 8,716
  • 5
  • 21
  • 27
Enintsoa
  • 1
  • 2
  • so what does `onFailure` say ? what's the exception ? – a_local_nobody May 09 '22 at 13:04
  • Print `Throwable t` you are getting in `onFailure` and add the error stacktrace with question . [a Possible cause](https://stackoverflow.com/questions/45940861/android-8-cleartext-http-traffic-not-permitted). – ADM May 09 '22 at 13:13
  • OnFailure does not show any exception The application connects without error with an emulator and tablet version 7.* but with 10.* and 11.*, it does not show any error and does not connect with the server. – Enintsoa May 09 '22 at 13:19
  • I put this code in OnFailure but no exception try { alertConnexion("Echec de connection aux serveur"); } catch (Exception e) { Log.e("Error", "exception", e); } – Enintsoa May 09 '22 at 13:25
  • fyi, I removed the `PHP` tag since it seems unrelated – brombeer May 09 '22 at 13:31

0 Answers0