5

我正在尝试实现一个 httpClient 类,它是一个 AsyncTask (否则由于我的主线程中有一个连接,我会得到一个异常)。我试过这样的事情:

private class execHttpAsync extends AsyncTask <String, String, HttpResponse>
    {
        public String resultString;

        @Override
        protected HttpResponse doInBackground(String... params) 
        {
            String url = params[0];

            HttpClient httpClient = new DefaultHttpClient();
            HttpGet request = new HttpGet(url);           
            request.setHeader("Content-Type", "text/xml");
            HttpResponse response;
            try {
                response = httpClient.execute(request);
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return response;
        }

        @Override
       protected void onPostExecute(HttpResponse result) 
        {
            StringBuffer returned = new StringBuffer();

            InputStream content = result.getEntity().getContent();
            BufferedReader rd = new BufferedReader(new InputStreamReader(content, "UTF-8"));                
            String line;
            while ((line = rd.readLine()) != null) 
            {
                String endOfLine = "";
                returned.append(line + endOfLine);
            }
            content.close(); 

         String retVal = returned.toString();

         try 
         {
             String header = retVal.substring(0, 1);
             if (!header.equals("<"))
              {
                retVal = retVal.replace(header, "");
              }
         } 
         catch (Exception e) 
         {
            // TODO: handle exception
         }

         resultString = returned.toString();

        }

    }

但我最终需要得到回应。我试图实例化这个类,然后作为成员获得响应,但没有成功。有什么建议么 ?

4

2 回答 2

12

试试这个方法

private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {
      String response = "";
      for (String url : urls) {
        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        try {
          HttpResponse execute = client.execute(httpGet);
          InputStream content = execute.getEntity().getContent();

          BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
          String s = "";
          while ((s = buffer.readLine()) != null) {
            response += s;
          }

        } catch (Exception e) {
          e.printStackTrace();
        }
      }
      return response;
    }

    @Override
    protected void onPostExecute(String result) {
      textView.setText(result);
    }
  }
于 2012-10-27T14:51:35.193 回答
1

这是异步操作,因此类似于:

myRetValue = new myAsyncTask.execute();

不会将返回值分配onPostExecute()给 myRetValue。您的

resultString = returned.toString();

应该导致将任何returned.toString()产生的东西分配给resultString变量(我猜它是在你的 AsyncTask 类之外声明的,所以基本上你的代码乍一看很好。你可能根本没有任何东西returned。尝试在你的 AsyncTask 代码中植入一些断点并检查它是否真的像你想象的那样行事。

于 2012-10-27T14:52:20.573 回答