得到" org.apache.http.conn.httphostconnectexception连接到http://x.x.x.x:88 / mobile.php被拒绝

时间:2014-11-05 22:05:22

标签: php android mysql

我使用xampp设置了一个MySql服务器,我正在创建一个连接数据库的Android应用程序。我可以使用外部的MYSQL Workbench和ODBC连接器顺利连接,并且可以无误地推送/拉取数据。当我与EMULATED手机连接时,我得到了" org.apache.http.conn.httphostconnectexception与http://x.x.x.x:88/mobile.php的连接被拒绝"在DDMS中。知道为什么会这样吗???

允许互联网没有开启......现在,只是验证,清理并重新启动,现在我得到了#34; ClientProtocolException"。

public void getData() {
    String result = "";
    InputStream isr = null;
    try {
        HttpClient httpclient = new DefaultHttpClient();
        ///external ip address not local host or trying inside a local network
        HttpPost httpost = new HttpPost("http://x.x.x.x:88/mobile.php");
        HttpResponse response = httpclient.execute(httpost);
        HttpEntity entity = response.getEntity();
        isr = entity.getContent();
    } catch (Exception e) {
        Log.e("log.tag", "Error in http connection  " + e.toString());
        resultView.setText("Could not connect to Database");
    }
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                isr, "iso=8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        isr.close();

        result = sb.toString();
    } catch (Exception e) {
        Log.e("log.tag", "Error converting result  " + e.toString());
    }
    try {
        String s = "";
        JSONArray jArray = new JSONArray(result);
        for (int i = 0; i < jArray.length(); i++) {
            JSONObject json = jArray.getJSONObject(i);
            s = s + "User :" + json.getString("UserName");
        }
        resultView.setText(s);
    } catch (Exception e) {
        Log.e("log.tag", "Error Parsing Data " + e.toString());
    }

}

1 个答案:

答案 0 :(得分:0)

使这项工作的步骤:

  1. 为您的清单添加INTERNET权限
  2. 确保没有防火墙或代理从服务器端阻止
  3. httpPost.setHeader("Accept", "application/json");声明
  4. 之后添加httpPost