我使用xampp设置了一个MySql服务器,我正在创建一个连接数据库的Android应用程序。 我可以使用外部的MYSQL Workbench和ODBC连接器毫无问题地连接,并且可以无误地推/拉数据。 当与EMULATED手机连接时,我在DDMS中收到“org.apache.http.conn.httphostconnectexception连接到http:// xxxx:88 / mobile.php denied ”。 知道为什么会这样吗???
允许Internet未启用...现在,只是验证,清理并重新启动,现在我收到“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()); } }I've setup a MySql server using xampp, I am creating an android app to connect to the database. I can connect without a problem using MYSQL Workbench and ODBC connector from off site and ably to push/pull data without errors. when connecting with the EMULATED phone I am getting "org.apache.http.conn.httphostconnectexception connection to http://x.x.x.x:88/mobile.php refused" in the DDMS. Any idea why this is happening???
Allow Internet wasn't turned on... it is now , just verified, cleaned and relaunched, now I am getting "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()); } }最满意答案
使这项工作的步骤:
为您的清单添加INTERNET权限 确保服务器端没有阻止防火墙或代理 添加httpPost.setHeader("Accept", "application/json"); 在httpPost声明之后Steps to make this work:
Add INTERNET permission to your Manifest Make sure no firewall or proxy is blocking from server side add httpPost.setHeader("Accept", "application/json"); after httpPost declaration更多推荐
发布评论