我正在尝试从我的android模拟器连接到localhost WAMP Apache服务器。但是连接被拒绝了。所有细节如下。请帮帮我。
1)Android Logcat:
错误:
12-11 01:56:05.880 10346-32019/com.revu.revuappforcollege W/System.err﹕ org.apache.http.conn.HttpHostConnectException: Connection to http://10.0.2.2:80 refused
完成错误消息:
12-11 01:56:04.845 10346-10353/com.revu.revuappforcollege W/art﹕ Suspending all threads took: 178.509ms
12-11 01:56:04.947 10346-10369/com.revu.revuappforcollege W/EGL_emulation﹕ eglSurfaceAttrib not implemented
12-11 01:56:04.947 10346-10369/com.revu.revuappforcollege W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xad921ae0, error=EGL_SUCCESS
12-11 01:56:05.010 10346-10357/com.revu.revuappforcollege I/art﹕ Background partial concurrent mark sweep GC freed 2091(146KB) AllocSpace objects, 0(0B) LOS objects, 26% free, 1410KB/1922KB, paused 3.545ms total 623.566ms
12-11 01:56:05.015 10346-10346/com.revu.revuappforcollege I/Choreographer﹕ Skipped 44 frames! The application may be doing too much work on its main thread.
12-11 01:56:05.143 10346-10353/com.revu.revuappforcollege W/art﹕ Suspending all threads took: 74.530ms
12-11 01:56:05.840 10346-10346/com.revu.revuappforcollege I/Choreographer﹕ Skipped 49 frames! The application may be doing too much work on its main thread.
12-11 01:56:05.880 10346-32019/com.revu.revuappforcollege W/System.err﹕ org.apache.http.conn.HttpHostConnectException: Connection to http://10.0.2.2:80 refused
12-11 01:56:05.880 10346-32019/com.revu.revuappforcollege W/System.err﹕ at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:193)
12-11 01:56:05.880 10346-32019/com.revu.revuappforcollege W/System.err﹕ at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:169)
12-11 01:56:05.947 10346-10369/com.revu.revuappforcollege E/Surface﹕ getSlotFromBufferLocked: unknown buffer: 0xabfb9990
12-11 01:56:06.161 10346-10353/com.revu.revuappforcollege W/art﹕ Suspending all threads took: 82.626ms
12-11 01:56:06.493 10346-32019/com.revu.revuappforcollege W/System.err﹕ at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:124)
12-11 01:56:06.493 10346-32019/com.revu.revuappforcollege W/System.err﹕ at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:366)
12-11 01:56:06.493 10346-32019/com.revu.revuappforcollege W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:560)
12-11 01:56:06.493 10346-32019/com.revu.revuappforcollege W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:492)
12-11 01:56:06.646 10346-32019/com.revu.revuappforcollege W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:470)
12-11 01:56:06.646 10346-32019/com.revu.revuappforcollege W/System.err﹕ at com.revu.revuappforcollege.Activity1$readNFCTag.doInBackground(Activity1.java:336)
12-11 01:56:06.718 10346-32019/com.revu.revuappforcollege W/System.err﹕ at com.revu.revuappforcollege.Activity1$readNFCTag.doInBackground(Activity1.java:292)
12-11 01:56:06.718 10346-32019/com.revu.revuappforcollege W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:295)
12-11 01:56:06.718 10346-32019/com.revu.revuappforcollege W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
12-11 01:56:07.136 10346-10353/com.revu.revuappforcollege W/art﹕ Suspending all threads took: 57.421ms
12-11 01:56:07.230 10346-32019/com.revu.revuappforcollege W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
12-11 01:56:07.230 10346-32019/com.revu.revuappforcollege W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
12-11 01:56:07.230 10346-32019/com.revu.revuappforcollege W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
12-11 01:56:07.230 10346-32019/com.revu.revuappforcollege W/System.err﹕ at java.lang.Thread.run(Thread.java:818)
12-11 01:56:07.230 10346-32019/com.revu.revuappforcollege W/System.err﹕ Caused by: java.net.ConnectException: failed to connect to /10.0.2.2 (port 80): connect failed: ENETUNREACH (Network is unreachable)
12-11 01:56:07.269 10346-32019/com.revu.revuappforcollege W/System.err﹕ at libcore.io.IoBridge.connect(IoBridge.java:124)
12-11 01:56:07.269 10346-32019/com.revu.revuappforcollege W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
12-11 01:56:07.269 10346-32019/com.revu.revuappforcollege W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:452)
12-11 01:56:07.269 10346-32019/com.revu.revuappforcollege W/System.err﹕ at java.net.Socket.connect(Socket.java:884)
12-11 01:56:07.269 10346-32019/com.revu.revuappforcollege W/System.err﹕ at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:124)
12-11 01:56:07.270 10346-32019/com.revu.revuappforcollege W/System.err﹕ at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:149)
12-11 01:56:07.270 10346-32019/com.revu.revuappforcollege W/System.err﹕ ... 14 more
12-11 01:56:07.270 10346-32019/com.revu.revuappforcollege W/System.err﹕ Caused by: android.system.ErrnoException: connect failed: ENETUNREACH (Network is unreachable)
12-11 01:56:07.270 10346-32019/com.revu.revuappforcollege W/System.err﹕ at libcore.io.Posix.connect(Native Method)
12-11 01:56:07.270 10346-32019/com.revu.revuappforcollege W/System.err﹕ at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:111)
12-11 01:56:07.270 10346-32019/com.revu.revuappforcollege W/System.err﹕ at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
12-11 01:56:07.270 10346-32019/com.revu.revuappforcollege W/System.err﹕ at libcore.io.IoBridge.connect(IoBridge.java:122)
12-11 01:56:07.270 10346-32019/com.revu.revuappforcollege W/System.err﹕ ... 19 more
12-11 01:56:07.506 10346-32019/com.revu.revuappforcollege E/AndroidRuntime FATAL EXCEPTION: AsyncTask #1
Process: com.revu.revuappforcollege, PID: 10346
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:200)
at android.os.Handler.<init>(Handler.java:114)
at android.widget.Toast$TN.<init>(Toast.java:345)
at android.widget.Toast.<init>(Toast.java:101)
at android.widget.Toast.makeText(Toast.java:259)
at com.revu.revuappforcollege.Activity1$readNFCTag.doInBackground(Activity1.java:353)
at com.revu.revuappforcollege.Activity1$readNFCTag.doInBackground(Activity1.java:292)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
12-11 01:56:07.546 10346-10353/com.revu.revuappforcollege W/art﹕ Suspending all threads took: 13.659ms
2)我添加了Permissionin Manifest文件
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
3)Android代码:
class readNFCTag extends AsyncTask<String, String, Void> {
private ProgressDialog progressDialog = new ProgressDialog(Activity1.this);
InputStream is;
String result;
protected void onPreExecute() {
Log.e("BP4", "In onPreExecute");
progressDialog.setMessage("Loading ...");
progressDialog.show();
result = "";
is = null;
progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface arg0) {
readNFCTag.this.cancel(true);
}
});
}
@Override
protected Void doInBackground(String... params) {
Log.e("BP5", "In doInBackground");
HttpClient httpClient = new DefaultHttpClient();
// HttpPost httpPost = new
// HttpPost("http://10.0.2.2:8080/SomeName/Details.php");
HttpPost httpPost = new HttpPost("http://10.0.2.2:80/AllainanceCanteen/NFCTagDetails.php");
ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();
try {
Log.e("BP6", "In doInBackground2");
param.add(new BasicNameValuePair("NFCTagNumber", Integer.toString(NFCTagNumber)));
Log.e("BP8", "In doInBackground2");
httpPost.setEntity(new UrlEncodedFormEntity(param));
Log.e("BP9", "In doInBackground2");
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
Log.e("BP19", "In doInBackground: " + NFCTagNumber);
HttpResponse httpResponse = httpClient.execute(httpPost);
Log.e("BP10", "In doInBackground3");
HttpEntity httpEntity = httpResponse.getEntity();
Log.e("BP7", "In doInBackground3");
// read content
is = httpEntity.getContent();
} catch (Exception e) {
// Log.e("log_tag", "Error in http connection " + e.toString());
// Log.e("####log_tag2", "End Here" + e.toString());
// Log.e("!!!!log_tag", "Start Here");
e.printStackTrace();
// Log.e("***log_tag", "END HERE 2");
Toast.makeText(getBaseContext(), "Cannot connect to server", Toast.LENGTH_LONG).show();
this.progressDialog.dismiss();
closeActivity();
}
try {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
while (null != (line = br.readLine())) {
sb.append(line).append("\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag2", "Error converting result " + e.toString());
this.progressDialog.dismiss();
Toast.makeText(getApplicationContext(), "Invalid NFC Communication!!", Toast.LENGTH_LONG).show();
closeActivity();
}
return null;
}
protected void onPostExecute(Void v) {
Log.e("Log_info", "REsults are: " + result + " End HERE");
JSONObject Jarray = new JSONObject();
try {
try {
Jarray = new JSONObject(result);
} catch (Exception e) {
this.progressDialog.dismiss();
Toast.makeText(getApplicationContext(), "Invalid Communication!!", Toast.LENGTH_LONG).show();
closeActivity();
}
if ((result.isEmpty()) || (result == null)) {
// text.setText("Search Results Not found!!");
this.progressDialog.dismiss();
Toast.makeText(getApplicationContext(), "Invalid !!", Toast.LENGTH_LONG).show();
closeActivity();
} else {
Log.e("BP7", "In doInBackground");
for (int i = 0; i < Jarray.length(); i++) {
Log.e("Log_info", "Results2 are: " + Jarray.toString() + " End HERE");
// text_1 = (TextView)findViewById(R.id.txt1);
seekReview = Jarray.getString("ProductName");
addWidgetsToLayout();
this.progressDialog.dismiss();
}
}
} catch (Exception e) {
Log.e("log_tag3", "Error parsing data " + e.toString());
Toast.makeText(getApplicationContext(), "Cannot connect to server!!", Toast.LENGTH_LONG).show();
closeActivity();
}
}
}
4)httpd.conf文件权限为:
*Listen 0.0.0.0:80
Listen [::0]:80
<Directory />
AllowOverride All
Require all granted
</Directory>
<Directory "c:/wamp/bin/apache/apache2.4.9/cgi-bin">
AllowOverride None
Options None
Require all granted
</Directory>
<Files ".ht*">
Require all granted
</Files>*
5)没有其他软件使用端口80
6)我还关闭了Windows防火墙,系统中没有反病毒。
7)我也试过端口8080,但存在同样的问题