【发布时间】:2013-03-08 02:17:21
【问题描述】:
我遇到了这个问题,导致我把头撞在墙上。我正在编写一个报纸应用程序,它从数据库中解析 JSON 格式的数据并显示它。该应用程序运行良好,可以在 WiFi 和 4G 上传输数据,但在 3G 上会卡住。大多数情况下,在 3G 上抓取数据需要 30 秒到 1 分钟,而在 WiFi 上只需 1 到 2 秒。我经常收到一条警告消息,指出:HttpHostConnectException:连接被拒绝。我知道该站点运行良好并且不会引起问题,因为我可以在 WiFi 和 4G 上正常查询,并且从桌面导航也没有问题。作为另一个测试,我借了我的同事MiFi,它在我们地区只有3G,并将我的设备连接到它,它可以很好地传递数据,虽然它只是3G回到互联网。所以在看了这个,并试图找到一个解决方案之后,我得出的结论是,也许我没有做正确的事情。据我所知,一切都很好,但我不是专家。对此的任何见解将不胜感激。
总结--
- 4G = 工作
- WiFi = 工作
- 3G = 极慢
-
通过 WiFi 的 3G(3G 上的 MiFi)=Works
public JSONObject makeHttpRequest(String url, String method, List params) {
// Making HTTP request try { if(method == "GET"){ // request method is GET DefaultHttpClient httpClient = new DefaultHttpClient(); String paramString = URLEncodedUtils.format(params, "utf-8"); url += "?" + paramString; HttpGet httpGet = new HttpGet(url); HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent(); System.out.println("---GET--- Now grabing GET DATA"); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { BufferedReader reader = new BufferedReader(new InputStreamReader( is, "iso-8859-1"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); json = sb.toString(); } catch (Exception e) { Log.e("Buffer Error", "Error converting result " + e.toString()); } // try parse the string to a JSON object try { jObj = new JSONObject(json); } catch (JSONException e) { Log.e("JSON Parser", "Error parsing data " + e.toString()); } // return JSON String return jObj;}
【问题讨论】: