Java.net.SocketTimeoutException:连接到/192.168.1.8(端口8383)失败,超时时间为10000毫秒。

6

我是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 file -> Checking.Php

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

当通过浏览器调用时,PHP页面正常工作。


你添加权限了吗? - tesla1984
你在使用模拟器进行测试吗?如果没有,你能从设备浏览器访问该页面吗? - ashkhn
在尝试从Android代码访问之前,您能否通过telnet确认该端口是否可达? - liorsolomon
如何添加权限以及在哪里添加?请详细说明。 - shammy narayanan
我同时使用了模拟器和安卓手机,结果是一样的。 - shammy narayanan
你是指从命令提示符中ping它吗?还是不同的Telnet命令? - shammy narayanan
2个回答

3
问题似乎是你的Apache配置可能在监听错误的IP地址。Android正在尝试访问IP地址192.168.1.8:8383,因此您的Apache服务器需要与Android设备在同一个网络中。建议您确保Android设备与Apache服务器在同一网络上,并且您的服务器设置为在Android尝试连接的正确IP地址上进行监听。

两台设备,Android手机和笔记本电脑都连接到同一个WiFi,我还需要做出任何更改吗?请给予建议。 - shammy narayanan
你需要更新Android系统才能连接到Apache服务器正在运行的IP地址。Apache服务器正在监听哪个IP地址? - Ray Hunter
在 httpd.conf 文件中,我添加了这行代码 Listen 192.168.1.8:8383,(这是 URL 中传递的相同 IP 地址和端口)。 - shammy narayanan
监听 192.168.1.8:8383 监听 0.0.0.0:8383 监听 [::0]:8383 - shammy narayanan
超时问题已解决,这是我所做的,我在httpd.conf文件中添加了IPV4到Listen命令,并且暂时禁用了防病毒网关(该网关阻止请求到达服务器)。 - shammy narayanan

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

我按照你说的做了,但对我没有用。我已经在httpd.conf中包含了以下内容:Listen 192.168.1.11:8080 Listen 0.0.0.0:8080 Listen [::0]:8080。我不知道你对防病毒网关做了什么。你能帮我吗? - wasanga7

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接