0

I want to call login api(django) when user click on login button.I have created apiinterface and apiclient also.but when I Click on login button it shows me text: ***Failed to connect to /my ip address:8000.***I am using pgadmin.Server is already runnig and api works fine in postman,I don't know why it is not connecting and gives me message failed to connect??Please help me out!!

  public class ApiClient {
    private static final String BASE_URL="http://192.168.0.102/auth/";
    private static ApiClient mInstance;
    private Retrofit retrofit;

    private ApiClient(){
        retrofit=new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    public static synchronized ApiClient getInstance(){
        if (mInstance==null){
            mInstance=new ApiClient();
        }
        return mInstance;
    }
     public ApiInterface getApi(){
        return retrofit.create(ApiInterface.class);

    }
}

 Call<ResponseBody> call=ApiClient.getInstance().getApi().performUserLogin(username,password);
    call.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            try {
                String s =response.body().string();
                Toast.makeText(MainActivity.this,s,Toast.LENGTH_SHORT).show();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
                Toast.makeText(MainActivity.this,t.getMessage(),Toast.LENGTH_SHORT).show();
        }
    });
}

public interface ApiInterface {

    @FormUrlEncoded
    @POST("registeruser")
    Call<ResponseBody>performUserSignIn(@Field("username") String username, @Field("full_name") String full_name , @Field("password") String password, @Field("confirm_password") String confirm_password);

    @FormUrlEncoded
    @POST("loginuser")
    Call<ResponseBody>performUserLogin( @Field("username") String username ,@Field("password") String password );

}

Here is manifest.xml

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication">
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication"
        android:usesCleartextTraffic="true">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

Here is log

 2021-06-02 01:30:19.564 30836-30836/? I/art: Late-enabling -Xcheck:jni
2021-06-02 01:30:19.574 30836-30836/? D/TidaProvider: TidaProvider()
2021-06-02 01:30:19.579 30836-30836/? V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@2571bb
2021-06-02 01:30:19.668 30836-30836/com.example.myapplication W/System: ClassLoader referenced unknown path: /data/app/com.example.myapplication-1/lib/arm64
2021-06-02 01:30:19.671 30836-30836/com.example.myapplication W/ResourceType: No package identifier when getting name for resource number 0x00000000
2021-06-02 01:30:19.694 30836-30836/com.example.myapplication W/ResourceType: No package identifier when getting name for resource number 0x00000000
2021-06-02 01:30:19.698 30836-30836/com.example.myapplication W/ResourceType: No package identifier when getting name for resource number 0x00000000
2021-06-02 01:30:19.711 30836-30836/com.example.myapplication W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter androidx.vectordrawable.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
2021-06-02 01:30:19.717 30836-30836/com.example.myapplication D/AccessibilityManager: current package=com.example.myapplication, accessibility manager mIsFinalEnabled=false, mOptimizeEnabled=true, mIsUiAutomationEnabled=false, mIsInterestedPackage=false
2021-06-02 01:30:19.765 30836-30836/com.example.myapplication I/TypefaceUtils: Typeface has no familyName field
2021-06-02 01:30:19.832 30836-30836/com.example.myapplication E/HAL: PATH3 /odm/lib64/hw/gralloc.qcom.so
2021-06-02 01:30:19.832 30836-30836/com.example.myapplication E/HAL: PATH2 /vendor/lib64/hw/gralloc.qcom.so
2021-06-02 01:30:19.832 30836-30836/com.example.myapplication E/HAL: PATH1 /system/lib64/hw/gralloc.qcom.so
2021-06-02 01:30:19.832 30836-30836/com.example.myapplication E/HAL: PATH3 /odm/lib64/hw/gralloc.msm8953.so
2021-06-02 01:30:19.832 30836-30836/com.example.myapplication E/HAL: PATH2 /vendor/lib64/hw/gralloc.msm8953.so
2021-06-02 01:30:19.832 30836-30836/com.example.myapplication E/HAL: PATH1 /system/lib64/hw/gralloc.msm8953.so
2021-06-02 01:30:19.855 30836-30853/com.example.myapplication I/Adreno: QUALCOMM build                   : 01d2d27, I3d52eaf367
    Build Date                       : 12/10/16
    OpenGL ES Shader Compiler Version: XE031.09.00.03
    Local Branch                     : 
    Remote Branch                    : 
    Remote Branch                    : 
    Reconstruct Branch               : 
2021-06-02 01:30:19.861 30836-30853/com.example.myapplication I/OpenGLRenderer: Initialized EGL, version 1.4
2021-06-02 01:30:19.861 30836-30853/com.example.myapplication D/OpenGLRenderer: Swap behavior 1
2021-06-02 01:30:22.752 30836-30836/com.example.myapplication V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@535c6a8
2021-06-02 01:30:22.913 30836-30836/com.example.myapplication D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2021-06-02 01:30:22.976 30836-30836/com.example.myapplication I/Toast: Show toast from OpPackageName:com.example.myapplication, PackageName:com.example.myapplication
2021-06-02 01:30:31.946 30836-30836/com.example.myapplication W/IInputConnectionWrapper: reportFullscreenMode on inexistent InputConnection
Pooja
  • 1
  • 4

2 Answers2

0

Your BASE_URL is a localhost, publish your API

  • I have published Apiinterface in which registeruser and loginuser are endpoints of URL .Please help!! – Pooja Jun 01 '21 at 05:18
0

As it's an HTTP URL cleartext traffic permission might be an issue for the same. Try the below solution if it works.

AndroidManifest.xml -

<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        ...
        android:usesCleartextTraffic="true"
        ...>
        ...
    </application>
</manifest>
Kathan Patel
  • 368
  • 3
  • 11
  • ..Thank you But It is already there in Manifest – Pooja Jun 01 '21 at 15:19
  • Share logs to see if I can help. Also, try accessing URL in the mobile browser to check possible router issues. – Kathan Patel Jun 01 '21 at 17:38
  • I have shared logs, please Check. I have tried to access URL in mobile browser but it gives ,this site can't be reached,192.xxx.x.xxx refused to connect. Please suggest !! – Pooja Jun 02 '21 at 05:48
  • If you can't access the URL in the mobile browser means your device can't reach the server. So the application doesn't seems having any issue with the code. Tip: You can check if your server and mobile both in the same router. Also, reconfirm the server's IP in the router and confirm that your router's firewall isn't blocking the calls on your server. – Kathan Patel Jun 02 '21 at 05:55
  • MY server and mobile are in the same router. I have confirmed server's IP. How can I confirm router's firewall is not blocking the calls on my server, If you can give more information about this ,It would be helpful for me. Thank you. – Pooja Jun 02 '21 at 06:28
  • https://stackoverflow.com/questions/22144189/making-django-server-accessible-in-lan Check this answer it might help. And if it doen't then please share your router's model so that i might help. – Kathan Patel Jun 02 '21 at 06:36
  • I have checked that answer it is not helpful.My router's model no is N301 and company name is Tenda. – Pooja Jun 02 '21 at 08:14