我使用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());
}
}
答案 0 :(得分:0)
使这项工作的步骤:
httpPost.setHeader("Accept", "application/json");
声明httpPost
醇>