HttpClient执行挂起的Android应用程序

时间:2015-04-28 16:17:42

标签: android httpclient

我正在开发一个Android应用程序,它在从Web服务获取和解析数据后填充其ListView。下面是我正在使用的 AsyncTask 代码。

public class AsyncHttpTask extends AsyncTask<String, Void, Integer> {
    @Override
    protected Integer doInBackground(String... params) {
        InputStream inputStream = null;
        Integer result = 0;
        try {
            /* create Apache HttpClient */
            HttpClient httpclient = new DefaultHttpClient();
            /* HttpGet Method */
            HttpGet httpGet = new HttpGet(params[0]);

            /* optional request header */
            httpGet.setHeader("Content-Type", "application/json");

            /* optional request header */
            httpGet.setHeader("Accept", "application/json");
            /* Make http request call */
            HttpResponse httpResponse = httpclient.execute(httpGet);
            int statusCode = httpResponse.getStatusLine().getStatusCode();

            /* 200 represents HTTP OK */
            if (statusCode ==  200) {
                /* receive response as inputStream */
                inputStream = httpResponse.getEntity().getContent();
                String response = convertInputStreamToString(inputStream);
               // parseResult(response);
                result = 1; // Successful
            } else{
                Log.d(Tag, statusCode + " CODE");
                result = 0; //"Failed to fetch data!";
            }
        } catch (Exception e) {
            Log.d(Tag, e.getLocalizedMessage());
        }
        return result; //"Failed to fetch data!";
    }

该应用程序挂在HttpResponse httpResponse = httpclient.execute(httpGet);

有什么问题?提前致谢

1 个答案:

答案 0 :(得分:0)

尝试在进行http请求调用后添加此内容

HttpEntity entity = httpResponse.getEntity();

entity.consumeContent();

消费内容的详细信息可以在here

中找到