Failed to connect to /192.168.43.23 (port 8000) from /192.168.43.1 (port 47726) after 10000ms

  android, android-studio, java, laravel, retrofit

I am creating a Login Activity in Android Studio using Laravel REST API. I run the application on a real device (Android version 9). The API worked in Postman but not in the app. My IP address is 192.168.43.23. When I try to login, here’s the error Failed to connect to /192.168.43.23 (port 8000) from /192.168.43.1 (port 47726) after 10000ms. Do you have any idea how to solve it? Thank you. Here’s my code in LoginActivity.java:

private void login(String email, String password) {
    apiInterface = ApiClient.getClient().create(ApiInterface.class);
    Call<Login> loginCall = apiInterface.loginResponse(email, password);
    loginCall.enqueue(new Callback<Login>() {
        @Override
        public void onResponse(Call<Login> call, Response<Login> response) {
            if(response.body() != null && response.isSuccessful() && response.body().isStatus()){

                sessionManager = new SessionManager(LoginActivity.this);
                LoginData loginData = response.body().getLoginData();
                sessionManager.createLoginSession(loginData);

                Toast.makeText(LoginActivity.this, response.body().getLoginData().getEmail(), Toast.LENGTH_SHORT).show();
                Intent intentLogin = new Intent(LoginActivity.this, MainActivity.class);
                startActivity(intentLogin);
                finish();

            }else{
                Toast.makeText(LoginActivity.this, response.body().getMessage(), Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFailure(Call<Login> call, Throwable t) {
            Toast.makeText(LoginActivity.this, t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();

        }
    });

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    sessionManager = new SessionManager(MainActivity.this);
    if(!sessionManager.isLoggedIn()){
        moveToLogin();
    }
}

private void moveToLogin() {
    Intent intent = new Intent(MainActivity.this, LoginActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY);
    startActivity(intent);
    finish();
}

ApiClient.Java

public class ApiClient {
private static final String BASE_URL = "http://192.168.43.23:8000/api/";

private static Retrofit retrofit;

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

ApiInterface.java

import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;

public interface ApiInterface {

   @FormUrlEncoded
   @POST("loginPassenger")
   Call<Login> loginResponse(
        @Field("email") String email,
        @Field("password") String password
   );
 }

Login.java

import com.google.gson.annotations.SerializedName;

public class Login{

   @SerializedName("data")
   private LoginData loginData;

   @SerializedName("message")
   private String message;

   @SerializedName("status")
   private boolean status;

   public void setLoginData(LoginData loginData){
       this.loginData = loginData;
   }

   public LoginData getLoginData(){
       return loginData;
   }

   public void setMessage(String message){

       this.message = message;
   }

   public String getMessage(){
       return message;
   }

   public void setStatus(boolean status){
       this.status = status;
   }

   public boolean isStatus(){
       return status;
   }
   }

LoginData.java

import com.google.gson.annotations.SerializedName;

public class LoginData {

   @SerializedName("password")
   private String password;

   @SerializedName("contact")
   private String contact;

   @SerializedName("name")
   private String name;

   @SerializedName("updated_at")
   private Object updatedAt;

   @SerializedName("id_users")
   private String idUsers;

   @SerializedName("created_at")
   private Object createdAt;

   @SerializedName("gender")
   private int gender;

   @SerializedName("email")
   private String email;

   @SerializedName("username")
   private String username;

   @SerializedName("address")
   private String address;

   public void setPassword(String password){
       this.password = password;
   }

   public String getPassword(){
       return password;
   }

   public void setContact(String contact){
       this.contact = contact;
   }

   public String getContact(){
       return contact;
   }

   public void setName(String name){
       this.name = name;
   }

   public String getName(){
       return name;
   }

   public void setUpdatedAt(Object updatedAt){
       this.updatedAt = updatedAt;
   }

   public Object getUpdatedAt(){
       return updatedAt;
   }

   public void setIdUsers(String idUsers){
       this.idUsers = idUsers;
   }

   public String getIdUsers(){
       return idUsers;
   }

   public void setCreatedAt(Object createdAt){
       this.createdAt = createdAt;
   }

   public Object getCreatedAt(){
       return createdAt;
   }

   public void setGender(int gender){
       this.gender = gender;
   }

   public int getGender(){
       return gender;
   }

   public void setEmail(String email){
       this.email = email;
   }

   public String getEmail(){
       return email;
   }

   public void setUsername(String username){
       this.username = username;
   }

   public String getUsername(){
       return username;
   }

   public void setAddress(String address){
       this.address = address;
   }

   public String getAddress(){
       return address;
   } 
   }

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.passengerApp">

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

   <application
       android:icon="@mipmap/ic_launcher"
       android:label="@string/app_name"
       android:networkSecurityConfig="@xml/network_security_config"
       android:roundIcon="@mipmap/ic_launcher_round"
       android:supportsRtl="true"
       android:theme="@style/AppTheme"
       android:usesCleartextTraffic="true">
       <activity android:name=".AccountActivity"/>
       <activity android:name=".MyOrderActivity" />
       <activity android:name=".RegisterActivity" />
       <activity android:name=".LoginActivity" />
       <activity android:name=".MainActivity">
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />

               <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
       </activity>
   </application>

build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.1"

    defaultConfig {
        applicationId "com.example.passengerApp"
        minSdkVersion 16
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard- rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.2'
    implementation 'com.google.android.material:material:1.2.1'
    implementation 'androidx.annotation:annotation:1.1.0'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
    implementation 'com.android.support:design:30'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

    implementation 'com.squareup.retrofit2:retrofit:2.6.1'
    implementation 'com.squareup.retrofit2:converter-gson:2.6.1'
    implementation 'org.glassfish:javax.annotation:10.0-b28'

    implementation 'com.google.code.gson:gson:2.8.5'
}

network_security_config.xml

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">192.168.43.23:8000</domain>
    </domain-config>
</network-security-config>

Source: Android Questions

LEAVE A COMMENT