0
我想下面的代碼從服務器下載一個json文件。然而,我的應用程序行爲怪異。有時候,json會在1-2秒內下載,有時會永久卡住這個函數。我也嘗試了像HttpUrlConnection一樣的其他下載方式。但是,這也沒有幫助。任何人都可以建議我修復它!無響應的HttpClient
public String getJSONString(String url) {
String json = null;
HttpClient httpclient = null;
try {
Log.d("MARKER","PARSING SE PEHLE");
HttpParams params = new BasicHttpParams();
Log.d("MARKER","1st line");
HttpClient httpclient = new DefaultHttpClient(params); HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpclient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} 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"), 80000);
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());
}
Log.d("MARKER","DOWNLOAD COMPLETE");
HttpClientProvider.safeClose(httpclient);
return json;
}
什麼是'params'參數? – gauravsapiens
只看我新增加的帖子 –
問題存在! – gauravsapiens