将意图从doInbackground发送到MainActivity

时间:2015-05-17 10:34:21

标签: android

我正在尝试将PostData的内部类MyAsyncTask的doInBackground方法发送到我的MainActivity的内部类LocationListen但我得到了

  

此行有多个标记:       构造函数Intent(PostData.MyAsyncTask,Class)未定义 - 方法startActivity(Intent)未定义类型PostData.MyAsyncTask

我如何通过意图发送ArrayList并正确接收它?

我感谢任何帮助。

在PostData类中的内部类MyAsyncTask-的doInBackground方法中:

发送意图:

 ArrayList<Integer> routes = data.getRoutes(); //contains [7,31]

        Intent intent = new Intent(this, MainActivity.class); //The first error here
        intent.putExtra("stop_route" ,routes);
        startActivity(intent); //The second one here.
在MainActivity类中内部LocationListen的onLocationChanged方法中:

收到意图:

        Bundle b = getIntent().getExtras();
        b.getIntegerArrayList("stop_route");

PostData类:

public class PostData {
String jSONString;

public PostData() {
    super();

}

public String getjSONString() {
    return jSONString;

}

public void setjSONString(String jSONString) {
    this.jSONString = jSONString;
}

public void post_data(String jSONString) {
    this.jSONString = jSONString;
    new MyAsyncTask().execute(jSONString);
}

class MyAsyncTask extends AsyncTask<String, Integer, Void> {
    final Context mContext;

    public MyAsyncTask(Context context){
        mContext = context;
    }

    @Override
    protected Void doInBackground(String... params) {
        // TODO Auto-generated method stub
        BufferedReader reader = null;

        try {
            System.out.println("The output of : doInBackground "
                    + params[0]);

            URL myUrl = new URL(
                    "https://blabla-blabla.rhcloud.com/webapi/data");
/connection.php");
            HttpURLConnection conn = (HttpURLConnection) myUrl
                    .openConnection();
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            conn.setConnectTimeout(10000);
            conn.setReadTimeout(10000);
            conn.setRequestProperty("Content-Type", "application/json");
            conn.connect();
            DataOutputStream wr = new DataOutputStream(
                    conn.getOutputStream());
            // write to the output stream from the string
            wr.writeBytes(params[0]);

            wr.close();

            StringBuilder sb = new StringBuilder();
            reader = new BufferedReader(new InputStreamReader(
                    conn.getInputStream()));
            String line;

            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");

            }

            Gson gson = new Gson();
            StopsJSON data = gson.fromJson(sb.toString(), StopsJSON.class);

            ArrayList<Integer> routes = data.getRoutes();

            Intent intent = new Intent(mContext , MainActivity.class);
            intent.putExtra("stop_route" ,routes);
            mContext.startActivity(intent);

            System.out.println("The output of the StringBulder before "
                    + routes);
            System.out.println("The output of the StringBulder: "
                    + sb.toString());

        } catch (IOException e) {

            e.printStackTrace();
            return null;
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                    return null;
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

        return null;

    }

  }

}

1 个答案:

答案 0 :(得分:0)

将上下文存储为asynTask中的字段:

final Context mContext;
public MyAsyncTask(Context context) {
    mContext = context;
    ...

Intent需要一个上下文作为第一个参数(您的第一个错误,请参阅Intent):

Intent intent = new Intent(mContext, MainActivity.class);

startActivity(意向);是一种Context的方法,所以你需要再次使用你的Context(你的第二个错误,见Context):

mContext.startActivity(intent);