我正在使用WAMP服务器。我有一个db_connect.php文件驻留在服务器上。我从我的Android设备发出这个文件的http请求,该设备通过usb线连接到计算机。
但是每次我请求文件时都会收到以下错误:
连接错误org.apache.http.conn.HttpHostConnectException:与http://x.x.x.x的连接被拒绝
有谁知道我可能做错了什么?我的桌面在我的家庭宽带上运行WAMP,而我的平板电脑连接到我的家庭宽带。
如果我更改帖子请求以联系www.google.com我建立连接。有什么建议?
MainActivity - 我尝试连接的地方:
public class MainActivity extends Activity {
Button b1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button)findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
new HTTPTask().execute();
}
});
}
private class HTTPTask extends AsyncTask<String, Void, Boolean>{
@Override
protected Boolean doInBackground(String... params) {
//location of file to retrieve
String url = "http://192.168.x.xx/emenu/db_connect.php";
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
return true;
}catch(Exception e){
Log.e("log_tag", "Error in Connection " + e.toString());
return false;
}
}
@Override
protected void onPostExecute(Boolean result) {
super.onPostExecute(result);
if(result){
Toast.makeText(getApplicationContext(), "Connection Established!", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "Connection Failed!", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onPreExecute() {
super.onPreExecute();
Toast.makeText(getApplicationContext(), "Connection initiating...", Toast.LENGTH_SHORT).show();
}
}
}
清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dbconnectiontest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>