错误al conectar con localhost

时间:2012-03-23 13:21:57

标签: android

你能解决我的问题吗?

我正在尝试访问localhost以从表中获取数据,但是当我运行它时出现错误 HttpHostConnectException

COMPLETE ERROR: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");

}

感谢您的关注 阿尔瓦罗

1 个答案:

答案 0 :(得分:0)

Localhost是你真正的Android设备。

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