Android 不负责任的HttpClient

Android 不负责任的HttpClient,android,httpclient,Android,Httpclient,我正试图使用下面的代码从服务器下载一个json文件。然而,我的应用程序表现得很奇怪。有时,json会在1-2秒内下载,有时它会永远停留在这个函数上。我也尝试过其他下载方式,比如HttpUrlConnection。然而,这也无济于事。谁能给我建议一个解决办法 public String getJSONString(String url) { String json = null; HttpClient httpclient = null; try {

我正试图使用下面的代码从服务器下载一个json文件。然而,我的应用程序表现得很奇怪。有时,json会在1-2秒内下载,有时它会永远停留在这个函数上。我也尝试过其他下载方式,比如HttpUrlConnection。然而,这也无济于事。谁能给我建议一个解决办法

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;

    }
试试这个:

public class JSONParser {

   static InputStream is = null;
   static JSONObject jObj = null;
   static String json = "";

   // constructor
   public JSONParser() {

   }

   // function get json from url
   // by making HTTP POST or GET method
   public JSONObject makeHttpRequest(String url, String method, List<NameValuePair> params) {

      // Making HTTP request
      try {

         // check for request method
         if (method == "POST") {
            // request method is POST
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

         } else 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();
         }

      } catch (UnsupportedEncodingException e) {
         e.printStackTrace();
      } catch (ClientProtocolException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }

      if (is == null)
         return new JSONObject();

      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;

   }
}
公共类JSONParser{
静态InputStream为空;
静态JSONObject jObj=null;
静态字符串json=“”;
//建造师
公共JSONParser(){
}
//函数从url获取json
//通过使用HTTP POST或GET方法
公共JSONObject makeHttpRequest(字符串url、字符串方法、列表参数){
//发出HTTP请求
试一试{
//检查请求方法
如果(方法==“POST”){
//请求方法为POST
//defaultHttpClient
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url);
setEntity(新的UrlEncodedFormEntity(参数));
HttpResponse HttpResponse=httpClient.execute(httpPost);
HttpEntity HttpEntity=httpResponse.getEntity();
is=httpEntity.getContent();
}else if(方法==“GET”){
//请求方法是GET
DefaultHttpClient httpClient=新的DefaultHttpClient();
String paramString=URLEncodedUtils.format(params,“utf-8”);
url+=“?”+参数字符串;
HttpGet HttpGet=新的HttpGet(url);
HttpResponse HttpResponse=httpClient.execute(httpGet);
HttpEntity HttpEntity=httpResponse.getEntity();
is=httpEntity.getContent();
}
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
如果(is==null)
返回新的JSONObject();
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is,“iso-8859-1”),8;
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
json=sb.toString();
}捕获(例外e){
Log.e(“缓冲区错误”,“错误转换结果”+e.toString());
}
//尝试将字符串解析为JSON对象
试一试{
jObj=新的JSONObject(json);
}捕获(JSONException e){
Log.e(“JSON解析器”,“错误解析数据”+e.toString());
}
//返回JSON字符串
返回jObj;
}
}
您可以在AsyncTask中填充相关对象以避免另一个问题。玩得开心

String SERVER_URL ="";


List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jsonParser.makeHttpRequest(SERVER_URL, "GET", params);
String SERVER_URL=”“;
List params=new ArrayList();
//从URL获取JSON字符串
JSONObject json=jsonParser.makeHttpRequest(服务器URL,“GET”,参数);

这个“params”参数的作用是什么?只需看看我新添加的postPlease,请尝试更明确一些,以便我可以帮助您。它有时正确运行,有时挂在HTTPResponse行上!我想你的服务器有问题。请试试这个,告诉我发生了什么