2011-11-29 44 views
0

我想要做的就是使用AsyncTask来做一个http请求。这是我的代码到目前为止,但我不知道如何从我的主要活动调用这个类,以及如何获得结果。我有一个String var“uri”,它完成了我的http请求的url,我必须传递给我的类。在调用这个类之后,下一步是将结果(作为字符串)传递给一个新函数。如何传递参数并从AsyncTask Class中获取结果?

import java.io.ByteArrayOutputStream; 
import java.io.IOException; 

import org.apache.http.HttpResponse; 
import org.apache.http.HttpStatus; 
import org.apache.http.StatusLine; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 

import android.os.AsyncTask; 

class RequestTask extends AsyncTask<String, String, String>{ 

    @Override 
    protected String doInBackground(String... uri) { 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpResponse response; 
     String responseString = null; 
     try { 
      response = httpclient.execute(new HttpGet("http://www.mywebsite.com/xml_data/" + uri + "_meeting_info.xml?id=cacheSTOP3")); 
      StatusLine statusLine = response.getStatusLine(); 
      if(statusLine.getStatusCode() == HttpStatus.SC_OK){ 
       ByteArrayOutputStream out = new ByteArrayOutputStream(); 
       response.getEntity().writeTo(out); 
       out.close(); 
       responseString = out.toString(); 
      } else{ 
       //Closes the connection. 
       response.getEntity().getContent().close(); 
       throw new IOException(statusLine.getReasonPhrase()); 
      } 
     } catch (ClientProtocolException e) { 
      //TODO Handle problems.. 
     } catch (IOException e) { 
      //TODO Handle problems.. 
     } 
     return responseString; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
    onPostExecute(result); 
     //Do anything with response.. 
    } 
} 

主要活动:

RequestTask getXML = new RequestTask(); 
getXML.execute(MyVarString); 


//Now send the results from above to a new function 
Document doc = XMLfunctions.XMLfromString(getdata); 
+0

有没有原因为什么这个问题被拒绝投票?请解释一下,以便我确保将来更加清晰。 – Denoteone

+0

** super.onPostExecute(result); **不需要调用。 – NOSTRA

+2

我没有投票,但我会猜测它是因为a)AsyncTasks很常见,有一点研究,你可以找到你的答案或b)已经有一个常见的问题,你可以搜索找到你的回答。我个人不介意,但有些人对这些东西感到敏感 – Joe

回答

4

你应该onPostExecute(字符串结果)方法的最后把你的代码

//Now send the results from above to a new function 
Document doc = XMLfunctions.XMLfromString(getdata); 

地方。只有在执行异步任务后才能执行您的功能

+0

+1并选中了现在的帮助,没有错误,但我原来的问题仍然存在,我希望AsyncTask能够修复。 http://stackoverflow.com/questions/8315928/android-function-is-not-completing/8316185#comment10249311_8316185 – Denoteone

0

参数以数组的形式访问。因此,对于你的例子,它会是这样的:

response = httpclient.execute(new HttpGet("http://www.mywebsite.com/xml_data/" + uri[0] + "_meeting_info.xml?id=cacheSTOP3")); 

任何需要处理结果应进入onPostExecute方法。无论你从doInBackground返回什么,都将是onPostExecute的参数。因此,在你的榜样再次证明应该是这样的:

@Override 
protected void onPostExecute(String result) { 
    super.onPostExecute(result); 

    Document doc = XMLfunctions.XMLfromString(result); 
    //Do anything with response.. 
} 
1

你执行的要求基本上是正确的,只是因为doInBackground()具有可变参数的格式,你通过了MyVarStringuri[0]访问里面的任务,因为uri本身是一个字符串数组。

返回值的一种选择是让您的任务成为主Activity的私有内部类。这样,您可以直接在onPostExecute()中执行所有结果代码,您将可以访问所有活动数据。

如果您希望将任务作为单独的类保留,请使用onPostExecute()发送广播意图或通过自定义侦听器界面通知活动。回调可能包含结果,或者只是通知活动它必须在AsyncTask上调用getResult()以获取传递给的相同字符串作为参数。

HTH

+0

感谢信息非常丰富 – Denoteone

相关问题