2012-03-23 26 views
0

你能帮我解决我的问题吗?错误al conectar con localhost

我试图访问本地主机获取从表中的数据,但我得到一个错误HttpHostConnectException当我运行它

完整的错误:org.apache.http.conn.HttpHostConnectException:连接到http://localhost拒绝

代码使用:

public class MysqlConnectActivity extends Activity { 
private TextView tv; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    tv = (TextView) findViewById(R.id.TextView01); 
} 

private class DownloadPoints extends AsyncTask<String, Void, String> { 
    @Override 
    protected String doInBackground(String... urls) { 
     String response = ""; 

     try { 
      HttpClient client = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost("http://localhost/leerDatosUsuarios.php"); 
      HttpResponse execute = client.execute(httppost); 
      InputStream content = execute.getEntity().getContent(); 

      BufferedReader buffer = new BufferedReader(new InputStreamReader(content)); 
      String s = ""; 
      while ((s = buffer.readLine()) != null) { 
       response += s; 
      } 

     } 
     catch (Exception e) { 
      Log.e("log_tag", "Error en la conexion HTTP: "+e.toString()); 
     } 

     return response; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     tv.append("\nREAD FROM MYSQL:\n" + result); 
    } 
} 

public void readWebpage(View view) { 
    DownloadPoints task = new DownloadPoints(); 
    task.execute("http://localhost/leerDatosUsuarios.php"); 

} 

感谢您的关注 阿尔瓦罗

+0

有您的文章的英文版本? – Andreas 2012-03-23 13:45:26

回答

0

Localhost是您的实际Android设备。

如果您尝试访问开发计算机,则需要从模拟器访问10.0.2.2。

+0

我不使用模拟器,我在我的android手机上运行。我试过192.168.1.4和10.0.2.2相同的错误:( – Alvaito 2012-03-23 16:32:52

+0

我使用mi ip地址(在MS2的ipconfig视图中),我读了“你没有权限访问此服务器上的/leerDatosUsuarios.php”然后, – Alvaito 2012-03-23 16:43:24

+0

可能是您的防火墙的问题 – njzk2 2012-03-23 17:03:37