首页 文章

如何传递参数并从AsyncTask类中获取结果?

提问于
浏览
0

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

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

3 回答

  • 1

    你应该把你的代码

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

    onPostExecute(String result) 方法结束时的某个地方 . 这是执行函数的唯一方法 after 执行异步任务 .

  • 0

    你执行请求基本上是正确的,除了因为 doInBackground() 有一个varargs格式,你传递的 MyVarString 应该在任务中作为 uri[0] 访问,因为 uri 本身就是一个字符串数组 .

    返回值的一个选项是使您的任务成为主Activity的私有内部类 . 这样您就可以直接在 onPostExecute() 中执行所有结果代码,并且您可以访问所有活动数据 .

    如果您希望将任务保持为单独的类,则使用 onPostExecute() 发送Broadcast Intent或通过自定义侦听器接口通知Activity . 回调可以包括结果,或者只是通知Activity它必须在AsyncTask上调用 getResult() 以获得作为参数传递给 onPostExecute() 的相同字符串 .

    HTH

  • 4

    参数像数组一样被访问 . 所以对于你的例子,它将是这样的:

    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..
    }
    

相关问题