【问题标题】:java.net.ConnectException:Failed to connect to /10.0.2.2:443java.net.ConnectException:无法连接到 /10.0.2.2:443
【发布时间】:2020-08-25 11:52:16
【问题描述】:

我正在开发一个需要用户身份验证的 Android 应用程序。我正在使用 localhost phpadmin 进行服务器和数据库连接。到目前为止,该应用程序有 1 个登录活动,可导入一个 php url 以连接到 localhost。如果一切正常,应用程序应该会显示一个 alertDialog,说明登录成功。但是每当我尝试运行该应用程序时,当我单击登录时,它会显示一个空的 alertDialog 并且 AndroidStudio 调试器会显示此错误:java.net.ConnectException:Failed to connect to /10.0.2.2:443

这是 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);
    }
} 

这是 BackroundWorker 类的代码:

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);
    }
}

使用断点我发现 OutputStream outputStream= httpsURLConnection.getOutputStream(); 这一行是引发异常的原因。

这是我的 AndroidManifest 文件:

<?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>

我尝试添加端口 80,8080 和 8888,但没有成功。将 10.0.2.2 交换到 localhost 或我的本地 IP 也不起作用。

【问题讨论】:

  • 如果您将 https 更改为 http 会起作用吗?
  • @wowoOt 确实如此,谢谢!

标签: java android localhost httpurlconnection


【解决方案1】:

我把 https 改成了 http:

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

和

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

现在可以完美运行了。

【讨论】:

    猜你喜欢
    • 2012-05-31
    • 1970-01-01
    • 2015-06-02
    • 1970-01-01
    • 2018-11-02
    • 1970-01-01
    • 2018-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多