【问题标题】:java.net.SocketTimeoutException: failed to connect to /192.168.1.8 (port 8383) after 10000msjava.net.SocketTimeoutException:10000 毫秒后无法连接到 /192.168.1.8(端口 8383)
【发布时间】:2017-07-09 04:45:45
【问题描述】:

我是 Android 编程新手。尝试从我的 Android 客户端 (WAMP) 连接到 PHP 时出现以下错误

java.net.SocketTimeoutException: failed to connect to /192.168.1.8 (port 8383) after 10000ms
'I had searched for this type of error and had made all prescribed changes including Changing localhost to ipv address, changing the Timeout interval, configuring the httpd.conf file, still couldn't resolve.Pls find below the code, conf file changes and error log

Error Log:

java.net.SocketTimeoutException: failed to connect to /192.168.1.8 (port 8383) after 10000ms
02-20 07:42:34.857 3370-3512/com.example.vijayar.phpconnect W/System.err:     at libcore.io.IoBridge.connectErrno(IoBridge.java:169)
02-20 07:42:34.857 3370-3512/com.example.vijayar.phpconnect W/System.err:     at libcore.io.IoBridge.connect(IoBridge.java:122)
02-20 07:42:34.857 3370-3512/com.example.vijayar.phpconnect W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
02-20 07:42:34.857 3370-3512/com.example.vijayar.phpconnect W/System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)
02-20 07:42:34.857 3370-3512/com.example.vijayar.phpconnect W/System.err:     at java.net.Socket.connect(Socket.java:882)
02-20 07:42:34.857 3370-3512/com.example.vijayar.phpconnect W/System.err:     at com.android.okhttp.internal.Platform.connectSocket(Platform.java:139)
02-20 07:42:34.857 3370-3512/com.example.vijayar.phpconnect W/System.err:     at com.android.okhttp.Connection.connect(Connection.java:148)
02-20 07:42:34.857 3370-3512/com.example.vijayar.phpconnect W/System.err:     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:276)
02-20 07:42:34.857 3370-3512/com.example.vijayar.phpconnect W/System.err:     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
02-20 07:42:34.857 3370-3512/com.example.vijayar.phpconnect W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:373)
02-20 07:42:34.857 3370-3512/com.example.vijayar.phpconnect W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:323)
02-20 07:42:34.857 3370-3512/com.example.vijayar.phpconnect W/System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:491)
02-20 07:42:34.857 3370-3512/com.example.vijayar.phpconnect W/System.err:     at com.example.vijayar.phpconnect.MainActivity$AsyncRetrieve.doInBackground(MainActivity.java:80)
02-20 07:42:34.857 3370-3512/com.example.vijayar.phpconnect W/System.err:     at com.example.vijayar.phpconnect.MainActivity$AsyncRetrieve.doInBackground(MainActivity.java:33)
02-20 07:42:34.857 3370-3512/com.example.vijayar.phpconnect W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:288)
02-20 07:42:34.857 3370-3512/com.example.vijayar.phpconnect W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
02-20 07:42:34.857 3370-3512/com.example.vijayar.phpconnect W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
02-20 07:42:34.857 3370-3512/com.example.vijayar.phpconnect W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
02-20 07:42:34.857 3370-3512/com.example.vijayar.phpconnect W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
02-20 07:42:34.857 3370-3512/com.example.vijayar.phpconnect W/System.err:     at java.lang.Thread.run(Thread.java:818)
02-20 07:42:35.007 3370-3510/com.example.vijayar.phpconnect V/RenderScript: 0xb0f4b600 Launching thread(s), CPUs 3

Java 代码

public class MainActivity extends AppCompatActivity {

// CONNECTION_TIMEOUT and READ_TIMEOUT are in milliseconds
public static final int CONNECTION_TIMEOUT = 10000;
public static final int READ_TIMEOUT = 10000;
TextView textPHP;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textPHP = (TextView) findViewById(R.id.textPHP);
    //Make call to AsyncRetrieve
    new AsyncRetrieve().execute();
}

private class AsyncRetrieve extends AsyncTask<String, String, String> {
    ProgressDialog pdLoading = new ProgressDialog(MainActivity.this);
    HttpURLConnection conn;
    URL url = null;

    //this method will interact with UI, here display loading message
    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        pdLoading.setMessage("\tLoading...");
        pdLoading.setCancelable(false);
        pdLoading.show();

    }

    // This method does not interact with UI, You need to pass result to onPostExecute to display
    @Override
    protected String doInBackground(String... params) {
        try {
            // Enter URL address where your php file resides
            url = new URL("http://192.168.1.8:8383/Checking/Checking.php");

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return e.toString();
        }
        try {

            // Setup HttpURLConnection class to send and receive data from php
            conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(READ_TIMEOUT);
            conn.setConnectTimeout(CONNECTION_TIMEOUT);
            conn.setRequestMethod("GET");

            // setDoOutput to true as we recieve data from json file
            conn.setDoOutput(true);

        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
            return e1.toString();
        }

        try {

            int response_code = conn.getResponseCode();

            // Check if successful connection made
            if (response_code == HttpURLConnection.HTTP_OK) {

                // Read data sent from server
                InputStream input = conn.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(input));
                StringBuilder result = new StringBuilder();
                String line;

                while ((line = reader.readLine()) != null) {
                    result.append(line);
                }

                // Pass data to onPostExecute method
                return (result.toString());

            } else {

                return ("unsuccessful");
            }

        } catch (IOException e) {
            e.printStackTrace();
            return e.toString();
        } finally {
            conn.disconnect();
        }


    }

    // this method will interact with UI, display result sent from doInBackground method
    @Override
    protected void onPostExecute(String result) {

        pdLoading.dismiss();
        if(result.equals("Success! This message is from PHP")) {
            textPHP.setText(result.toString());
        }else{
            // you to understand error returned from doInBackground method
            Toast.makeText(MainActivity.this, result.toString(), Toast.LENGTH_LONG).show();
        }

    }

}
}

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

In Httpd.conf the following changes are made
#Listen 12.34.56.78:8383
Listen 0.0.0.0:8383
Listen [::0]:8383

<Directory />
    AllowOverride None
Options None
Allow from All
Require all granted
</Directory>

PHP 文件 -> Checking.Php

<?php 
      echo "Success! This message is from PHP";
?>

从浏览器调用 PHP 页面可以正常工作。

【问题讨论】:

  • 你添加权限了吗?
  • 您是否使用模拟器进行测试?如果不能,您可以从设备浏览器访问该页面吗?
  • 在尝试从 Android 代码访问它之前,您能否确保该端口甚至可以通过 telnet 访问?
  • 如何添加权限以及在哪里添加?请详细说明
  • 模拟器和安卓手机我都用过,结果是一样的

标签: java php android wampserver


【解决方案1】:

超时问题已解决,这就是我所做的,我已将 IPV4 添加到 httpd.conf 文件中的 Listen 命令并暂时禁用防病毒网关(阻止请求访问服务器)-

【讨论】:

  • 我照你说的做了,对我没用。我已包含在httpd.conf Listen 192.168.1.11:8080 Listen 0.0.0.0:8080 Listen [::0]:8080 中。我不知道你对防病毒网关做了什么。你能帮我吗??
【解决方案2】:

您的 Apache 配置可能正在侦听错误的 IP 地址。 Android 正在尝试访问 IP 地址192.168.1.8:8383,并且您的 Apache 服务器需要与您的 Android 设备位于同一网络上。我建议您确保您的 Android 设备与 Apache 服务器位于同一网络上,并且您的服务器已设置为侦听 Android 尝试连接的正确 IP 地址。

【讨论】:

  • Android 和笔记本电脑都连接到同一个 WiFi,如果我还要进行任何更改,请告知
  • 您需要更新 Android 以连接到运行 Apache 服务器的 IP 地址。 Apache 服务器监听的 IP 地址是什么?
  • 在 httpd.conf 中我添加了这一行 Listen 192.168.1.8:8383,(这是相同的 IP 地址:在 URL 中传递的端口)
  • 听 192.168.1.8:8383 听 0.0.0.0:8383 听 [::0]:8383
  • 超时问题已解决,这就是我所做的,我在 httpd.conf 文件中将 IPV4 添加到 Listen 命令并暂时禁用防病毒网关(阻止请求访问服务器)
猜你喜欢
  • 1970-01-01
  • 2017-11-20
  • 1970-01-01
  • 1970-01-01
  • 2018-10-19
  • 2014-11-02
  • 2013-04-27
  • 2016-11-06
  • 2018-01-04
相关资源
最近更新 更多