我试图将意图从PostData的内部类MyAsyncTask的doInBackground方法发送到我的MainActivity的内部类LocationListen中,但是我正在获取

此行上有多个标记:构造函数Intent(PostData.MyAsyncTask,Class)未定义-方法PostActivity(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

将上下文存储为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);

  ask by The Time translate from so

未解决问题?本站智能推荐: