2016-04-07 16 views
0

リクエストを実行してレスポンスを非同期タスクとして返そうとしています。 。非同期のAsyncTaskとして実行リクエストでAndroid HttpClientがクラッシュする

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

}

は、残念ながら、それは例外:( を引くことなく、クラッシュしたここで非同期を呼び出すコードクラスです次のテストに合格する: String url = "http://new-rutor.org/search/batman%20v%20superman"; 文字列xpath = "// div [@ id = 'インデックス']/table/tbody/tr/td [2]/a [2]";エミュレータが私を提供

唯一のものは、次のとおりです。

04-07 15:49:21.796 8396-8396/torrentz.com.torrentforme D/OpenGLRenderer﹕ Enabling debug mode 0 
04-07 15:49:33.016 8396-8396/torrentz.com.torrentforme D/AndroidRuntime﹕ Shutting down VM 
04-07 15:49:33.016 8396-8396/torrentz.com.torrentforme W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa4d6eb20) 

私はあなたが非同期タスクのスケジュールを設定した後response権利を読んで取得しようとしている任意のヘルプ/提案

+1

クラッシュレポートを追加します。 –

+0

AsyncTaskの使用を中止してください。それらは単なる苦痛の世界です。 – Bootstrapper

+0

@RohitArya私はそうしたいと思っていますが、Bluestacksエミュレータを使用してこれを私に提供します: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 dalvikvm:threadid = 1:キャッチされていない例外でスレッドが終了する(group = 0xa4d6eb20) – Hunkeone

答えて

3

をいただければと思います。

HttpResponse response = (HttpResponse) new RetrieveAsyncResp().execute(getRequest); // task being ca 
InputStream inputStream = response.getEntity().getContent(); 

非同期タスクのバックグラウンドスレッドでの作業なので、右の次の行に、あなたはあなたの応答を持っていないだろうその時によってexecute、制御の動きを呼び出した後。

responseonPostExecuteasync taskに移動してください。

また、私はこれもに動作しますかを知らない:

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

あなたのIDEがincompatible typesエラーを示すことになります。

+0

また、 'execute()'はasynctask自体を返しますので、キャストしようとする 'ClassCastException' 'HttpResponse'に変換します。 – laalto

+0

、ありがとう! – Hunkeone

関連する問題