Abdelhamid Derouiche
Abdelhamid Derouiche

Reputation: 71

java.net.ConnectException:Failed to connect to /10.0.2.2:443

I am working on an android app that requires user authentication. I am using localhost phpadmin for server and database connexion. The app so far has 1 login activity that imports a php url to connect to localhost. If everything works fine the app is supposed to show an alertDialog saying that login was successful. But whenever i try running the app, when i click login it shows an empty alertDialog and the AndroidStudio debugger shows this error : java.net.ConnectException:Failed to connect to /10.0.2.2:443

This is the code for MainActivity :


import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
EditText UsernameEt , PasswordEt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        UsernameEt= (EditText)findViewById(R.id.editTextTextPersonName);
        PasswordEt= (EditText)findViewById(R.id.editTextTextPassword);

    }
    public void OnLogin(View view) {
        String username= UsernameEt.getText().toString();
        String password= PasswordEt.getText().toString();
        BackroundWorker backroundWorker=new BackroundWorker(this);
        String type= "login";
        backroundWorker.execute(type,username,password);
    }
} 

and this is the code for BackroundWorker class:

package com.android.example.meetmev0;

import android.app.AlertDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

import javax.net.ssl.HttpsURLConnection;

public class BackroundWorker extends AsyncTask<String,Void,String> {
Context context;
AlertDialog alertDialog;
public BackroundWorker ( Context ctx) {
    context=ctx;
}
    @Override
    protected String doInBackground(String... voids) {
    String type = voids[0];
    String login_url= "https://10.0.2.2/MeetLogin.php";
    if(type.equals("login"))
    {
        String test="so far so good";
        try {
            String username = voids[1];
            String password = voids[2];
            URL url= new URL(login_url);
            HttpsURLConnection httpsURLConnection = (HttpsURLConnection)url.openConnection();
            httpsURLConnection.setRequestMethod("POST");
            httpsURLConnection.setDoOutput(true);
            httpsURLConnection.setDoInput(true);
            OutputStream outputStream= httpsURLConnection.getOutputStream();
            test= " still good";
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
            String post_data= URLEncoder.encode("username ", "UTF-8")+"="+URLEncoder.encode(username , "UTF-8")+"&"
                    +URLEncoder.encode("password", "UTF-8")+"="+URLEncoder.encode(password , "UTF-8");
            bufferedWriter.write(post_data);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();
            InputStream inputStream = httpsURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"ISO-8859-1"));
            String result="";
            String line="";
            while((line = bufferedReader.readLine())!=null){
                result+=line;
            }
            bufferedReader.close();
            inputStream.close();
            httpsURLConnection.disconnect();
            Log.v("Activity test: ", result);
            return result;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
        return null;
    }

    @Override
    protected void onPreExecute() {
        alertDialog= new AlertDialog.Builder(context).create();
        alertDialog.setTitle("Login Status");

    }

    @Override
    protected void onPostExecute(String result) {
        alertDialog.setMessage(result);
        alertDialog.show();
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }
}

using breakpoints i found out that this line OutputStream outputStream= httpsURLConnection.getOutputStream(); is what's throwing the exception.

This is my AndroidManifest file :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.example.meetmev0">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <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/AppTheme"
        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>

I tried adding ports 80,8080 and 8888 but it didn't work. Swapping 10.0.2.2to localhostor to my local ip didn't work either.

Upvotes: 3

Views: 3537

Answers (1)

Abdelhamid Derouiche
Abdelhamid Derouiche

Reputation: 71

I changed https to http:

String login_url= "https://10.0.2.2/MeetLogin.php" to String login_url= "http://10.0.2.2/MeetLogin.php"

and

HttpsURLConnection httpsURLConnection = (HttpsURLConnection)url.openConnection(); to HttpURLConnection httpsURLConnection = (HttpURLConnection)url.openConnection();

Now it works perfectly.

Upvotes: 4

Related Questions