我想用下面的代碼Android的MySQL連接
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://127.0.0.1/mytable.php");
// HttpPost httppost = new HttpPost("http://localhost/mytable.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection"+e.toString());
}
PHP-MySQL時,控制繼續httpclient.execute(httppost);
它拋出一個異常連接:
org.apache.http.conn.HttpHostConnectException: Connection to http://127.0.0.1 refused
我還加<uses-permission android:name="android.permission.INTERNET" />
我AndroidManifest。 xml文件。
當我直接從瀏覽器測試我的php文件時,它工作正常。有什麼建議麼?
Jinx - 但你的答案更好。 – Nick
嗨,謝謝。現在我正面臨着java.net.SocketException:從相同的方法超時的操作HttpResponse response = httpclient.execute(httppost);因爲我也設置超時限制public static final int HTTP_TIMEOUT = 90 * 1000; mHttpClient = new DefaultHttpClient(); final HttpParams params = mHttpClient.getParams(); HttpConnectionParams.setConnectionTimeout(params,HTTP_TIMEOUT); HttpConnectionParams.setSoTimeout(params,HTTP_TIMEOUT); ConnManagerParams.setTimeout(params,HTTP_TIMEOUT); – aftab
我使用三星Galaxy GT 190 ....我測試我的上面的代碼 – aftab