我正在使用 WAMP 服务器。我在服务器上有一个 db_connect.php 文件。我从通过 USB 电缆连接到计算机的 android 设备对该文件发出 http 请求。
但是,每次我请求文件时,都会出现以下错误:
连接 org.apache.http.conn.HttpHostConnectException 中的错误:与http://xxxx的连接被拒绝
有谁知道我可能做错了什么?我的桌面在我的家庭宽带上运行 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>