【问题标题】:HttpURLConnection connect() won't workHttpURLConnection connect() 不起作用
【发布时间】:2017-10-25 08:20:42
【问题描述】:

我尝试在我的 PC 上连接 php,但 Android 代码将停止在 con.connect() 我不知道发生了什么。

 public String doInBackground(Void... arg0){
    HttpURLConnection con = null;
    String urlString = "http://localhost:8081/PHP/Android_SQL.php";
    System.out.println("before try");

    String res = "";
    try {
        URL url = new URL(urlString);
        con = (HttpURLConnection) url.openConnection();
        con.setReadTimeout(15000);
        con.setConnectTimeout(10000);
        con.setRequestMethod("GET");

        //set input and output descript
        con.setDoInput(true);

        con.setDoOutput(false); // in needed?
        System.out.println("connecting");
        con.connect(); // won't work
        System.out.println("connect ");
        System.out.println(con.getURL());

        BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8"));
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
            sb.append(line + "\n");
            System.out.println(line);
        }


        json = sb.toString();
        br.close();
        return json;

    }catch (SocketTimeoutException a){
        a.getMessage();
    }catch (IOException b){
        b.getMessage();
    }catch (Exception e) {
        e.getMessage();
    }
    finally {
        System.out.println("sueecssful");
    }
    return null;
}

我发现其中一个问题是 SSL 认证?

日志猫:

05-24 20:27:44.032 3089-3089/com.example.user.tophp I/System.out: click the button
05-24 20:27:44.033 3089-3237/com.example.user.tophp I/System.out: before try
05-24 20:27:44.033 1524-1595/system_process W/AudioTrack: AUDIO_OUTPUT_FLAG_FAST denied by client
05-24 20:27:44.033 3089-3237/com.example.user.tophp I/System.out: connecting
05-24 20:27:44.036 3089-3237/com.example.user.tophp I/System.out: sueecssful

【问题讨论】:

  • 您遇到的错误是什么?
  • 点击按钮后,logcat只会在connect()之前和finally{ }之后显示tag,也就是说,没有错误响应
  • 你能复制/粘贴相关的logcat吗?
  • @Marc 我编辑答案以显示 logcat
  • 检查以下网址。这可能会对您有所帮助:stackoverflow.com/questions/2151359/…

标签: php android httpurlconnection


【解决方案1】:

您正在尝试连接到 localhost:8081,它本身就是 Android 设备。

使用adb 通过USB 将端口转发/反向连接到您的计算机,或者您可以简单地将localhost 替换为您的计算机在本地网络上的IP。示例:192.168.0.2:8081

其他潜在问题:

catch (SocketTimeoutException a){
        a.getMessage();
    }catch (IOException b){
        b.getMessage();
    }catch (Exception e) {
        e.getMessage();
    }

至少将a.getMessage(); 替换为a.printStackTrace() 以打印错误。但实际上你应该使用Log.e("MyTag", "Error", a); * https://developer.android.com/reference/android/util/Log.html

catch (SocketTimeoutException a){
        Log.e("MyTag", "Timout Error", a);
    }catch (IOException b){
        Log.e("MyTag", "IO Error", b);
    }catch (Exception e) {
        Log.e("MyTag", "Error", e);
    }

还要确保您已将以下内容添加到您的清单中 <uses-permission android:name="android.permission.INTERNET"/>

【讨论】:

  • 谢谢,我已将 <uses-permission android:name="android.permission.INTERNET"/> 添加到清单中,并通过我的主机名使用 url(来自 no-ip)。我将更改catch exceptionstatement。
  • 谢谢,我收到错误信息IO Error java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 8081) after 10000ms: isConnected failed: ECONNREFUSED (Connection refused) 我会努力解决的,非常感谢
  • 你无法连接到localhost,因为你没有在你的安卓设备上运行网络服务器。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多