Android HttpClient在作为异步任务执行请求时崩溃

Android HttpClient在作为异步任务执行请求时崩溃,android,http,asynchronous,Android,Http,Asynchronous,我正在尝试以异步任务的形式执行请求并返回响应。 但不幸的是,它毫无例外地崩溃了:( 下面是调用async的code.Class: public class HttpUtilz{ HttpGet getRequest; public NodeList executeGetAndReceiveNodeList(String url, String xpath)throws XPathExpressionException,IOException,ParserConfigurationExcept

我正在尝试以异步任务的形式执行请求并返回响应。 但不幸的是,它毫无例外地崩溃了:( 下面是调用async的code.Class:

public class HttpUtilz{

HttpGet getRequest;

public NodeList executeGetAndReceiveNodeList(String url, String xpath)throws XPathExpressionException,IOException,ParserConfigurationException,SAXException{
    getRequest=new HttpGet(url);
    HttpResponse response = (HttpResponse) new RetrieveAsyncResp().execute(getRequest);
    InputStream inputStream = response.getEntity().getContent();
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
    Document doc = builder.parse(inputStream);
    doc.getDocumentElement().normalize();
    XPathFactory xPathFactory = XPathFactory.newInstance();
    XPath xPath=xPathFactory.newXPath();
    XPathExpression xPathExpression = xPath.compile(xpath);
    return (NodeList)xPathExpression.evaluate(doc, XPathConstants.NODESET);
}
}

实现异步:

public class RetrieveAsyncResp extends AsyncTask<HttpGet, String, HttpResponse> {

@Override
protected void onPreExecute(){
    super.onPreExecute();
}


@Override
protected HttpResponse doInBackground(HttpGet... get) {
    HttpClient client = new DefaultHttpClient();
    HttpResponse response;
    try {
        response = client.execute(get[0]);
    }catch (IOException e){
        e.printStackTrace();
        response=null;
    }
    return response;
}
@Override
protected void onPostExecute(HttpResponse response){
    super.onPostExecute(response);
}

我非常感谢您在安排异步任务后立即尝试获得read
响应的任何帮助/建议

HttpResponse response = (HttpResponse) new RetrieveAsyncResp().execute(getRequest); // task being ca
InputStream inputStream = response.getEntity().getContent();
异步任务在后台线程上工作,所以在调用
execute
之后,控件立即移动到下一行,此时您将不会得到响应

尝试在
异步任务
onPostExecute
中移动解析
响应
的代码

我也不知道这是怎么回事

HttpResponse response = (HttpResponse) new RetrieveAsyncResp().execute(getRequest);

您的IDE将显示
不兼容类型
错误。

您正试图在安排异步任务后立即获得读取
响应

HttpResponse response = (HttpResponse) new RetrieveAsyncResp().execute(getRequest); // task being ca
InputStream inputStream = response.getEntity().getContent();
异步任务在后台线程上工作,所以在调用
execute
之后,控件立即移动到下一行,此时您将不会得到响应

尝试在
异步任务
onPostExecute
中移动解析
响应
的代码

我也不知道这是怎么回事

HttpResponse response = (HttpResponse) new RetrieveAsyncResp().execute(getRequest);

您的IDE将显示
不兼容类型
错误。

添加崩溃报告。停止使用AsyncTask的它们只是一个痛苦的世界。@RohitArya我很想这样做,但使用Bluestacks emulator为我提供了:04-07 15:49:21.796 8396-8396/torrentz.com.torrentforme d/OpenGLRenderer﹕ 启用调试模式0 04-07 15:49:33.016 8396-8396/torrentz.com.torrentforme D/AndroidRuntime﹕ 关闭VM 04-07 15:49:33.016 8396-8396/torrentz.com.torrentforme W/dalvikvm﹕ threadid=1:线程以未捕获异常退出(组=0xa4d6eb20)@Bootstrapper我不能,因为我需要在ThreadPool中同时执行许多请求添加崩溃报告。停止使用AsyncTask,它们只是一个痛苦的世界。@RohitArya我很想这样做,但使用Bluestacks emulator为我提供了:04-07 15:49:21.796 8396-8396/torrentz.com.torrentforme d/OpenGLRenderer﹕ 启用调试模式0 04-07 15:49:33.016 8396-8396/torrentz.com.torrentforme D/AndroidRuntime﹕ 关闭VM 04-07 15:49:33.016 8396-8396/torrentz.com.torrentforme W/dalvikvm﹕ threadid=1:thread退出时出现未捕获异常(group=0xa4d6eb20)@Bootstrapper我不能,因为我需要在ThreadPool中同时执行大量请求。此外,
execute()
返回异步任务本身,因此将出现
ClassCastException
试图将其强制转换为
HttpResponse
。此外,
execute()
返回asynctask本身,因此将出现
ClassCastException
试图将其强制转换为
HttpResponse