我是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文件 - &gt; Checking.Php
<?php
echo "Success! This message is from PHP";
?>
从浏览器调用时,PHP页面工作正常。
答案 0 :(得分:2)
问题看起来像您的Apache配置可能正在侦听错误的IP地址。 Android正在尝试访问IP地址192.168.1.8:8383
,您的Apache服务器需要与您的Android设备在同一网络上。我建议您确保您的Android设备与Apache服务器位于同一网络上,并确保您的服务器设置为侦听Android正在尝试连接的正确IP地址。
答案 1 :(得分:0)
超时问题得到解决,这就是我所做的,我在httpd.conf
文件中将IPV4添加到了Listen命令,并暂时禁用了防病毒网关(阻止请求命中服务器) -