Android 从另一个活动调用AsyncTask

Android 从另一个活动调用AsyncTask,android,android-activity,android-asynctask,Android,Android Activity,Android Asynctask,我需要你的帮助在过去的两天里,我一直在这个问题上纠缠不清,我试图从另一个活动调用这个AsyncTask并获取返回的字符串,但是在阅读了网上的几篇文章之后,我无法这样做。有没有人能指出我需要做什么,或者厚颜无耻地发布一些代码并解释一下,让我知道发生了什么。我真的很感谢你的帮助 例如 String jsonString=//从AsyncTask返回的字符串 公共类processJSON扩展异步任务{ 私人语境; 公共进程JSON(上下文){ this.context=上下文; } @凌驾 受保护的字

我需要你的帮助在过去的两天里,我一直在这个问题上纠缠不清,我试图从另一个活动调用这个AsyncTask并获取返回的字符串,但是在阅读了网上的几篇文章之后,我无法这样做。有没有人能指出我需要做什么,或者厚颜无耻地发布一些代码并解释一下,让我知道发生了什么。我真的很感谢你的帮助

例如

String jsonString=//从AsyncTask返回的字符串
公共类processJSON扩展异步任务{
私人语境;
公共进程JSON(上下文){
this.context=上下文;
}
@凌驾
受保护的字符串doInBackground(对象…参数){
int location_id=(整数)参数[0];
字符串url=(字符串)参数[1];
InputStream in=null;
字符串结果=”;
JSONObject jArray=null;
字符串newURL=url+“?location\u id=“+location\u id;
HttpClient=new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(),10000);
HttpResponse响应;
JSONObject json=新的JSONObject();
试一试{
HttpPost=newhttppost(newURL);
//put(“location\u id”,location\u id);
StringEntity se=新的StringEntity(json.toString());
setContentType(新的BasicHeader(HTTP.CONTENT_TYPE,“application/json”);
邮政实体(se);
响应=client.execute(post);
if(响应!=null){
in=response.getEntity().getContent();
}
}捕获(例外e){
Log.e(“发送JSON”,“错误:+e”);
e、 printStackTrace();
}
试一试{
BufferedReader读取器=新的BufferedReader(新的InputStreamReader(在“iso-8859-1”中),8);
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
in.close();
结果=sb.toString();
}捕获(例外e){
Log.e(“Log_标记”,“错误转换结果”+e.toString());
}
返回结果;
}
}

您的
AsyncTask
在实例化回调时应该接受回调作为参数。然后,一旦它运行并完成,您就可以返回通过回调生成的任何值。

谢谢,我目前正在谷歌上查看示例,一旦我完成,它将更新。无需。将类processJSON抽象,然后在活动中实例化它的对象,并重写onPostExecute,就是这样。@VladimirLichonos啊,这很好。我必须记住这一点。
String jsonString = //returned string from AsyncTask 



public class processJSON extends AsyncTask<Object, Void, String>{

        private Context context;

        public processJSON (Context context){
            this.context = context;
        }

        @Override
        protected String doInBackground(Object... params) {

            int location_id = (Integer) params[0];
            String url = (String) params[1];

            InputStream in = null;
            String result = "";
            JSONObject jArray = null;
            String newURL = url + "?location_id=" + location_id;

            HttpClient client = new DefaultHttpClient();
            HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); 
            HttpResponse response;
            JSONObject json = new JSONObject();
            try{

                HttpPost post = new HttpPost(newURL);
                //json.put("location_id", location_id);
                StringEntity se = new StringEntity(json.toString());
                se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
                post.setEntity(se);
                response = client.execute(post);

                if (response != null) {
                    in = response.getEntity().getContent(); 
                }
            }catch (Exception e) {
                Log.e("Send JSON", "ERROR: " + e);
                e.printStackTrace();
            }

            try{
                BufferedReader reader = new BufferedReader(new InputStreamReader(in, "iso-8859-1"), 8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                in.close();
                result = sb.toString();
            }catch (Exception e) {
                Log.e("log_tag", "Error converting result " + e.toString());
            }
            return result;
        }
    }