Android 将意向从doInbackground发送到MainActivity

Android 将意向从doInbackground发送到MainActivity,android,Android,我试图将意图从PostData的内部类MyAsyncTask的doInBackground方法发送到MainActivity的内部类LocationListen,但是我得到了 此行有多个标记: 构造函数IntentPostData.MyAsyncTask类未定义-类型PostData.MyAsyncTask的方法StartActivityContent未定义 如何通过故意发送ArrayList并正确接收它 谢谢你的帮助 在PostData类的内部类MyAsyncTask的doInBackgrou

我试图将意图从PostData的内部类MyAsyncTask的doInBackground方法发送到MainActivity的内部类LocationListen,但是我得到了

此行有多个标记: 构造函数IntentPostData.MyAsyncTask类未定义-类型PostData.MyAsyncTask的方法StartActivityContent未定义

如何通过故意发送ArrayList并正确接收它

谢谢你的帮助

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

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;

    }

  }

}
发送意图:

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

    }

  }

}

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

final Context mContext;
public MyAsyncTask(Context context) {
    mContext = context;
    ...
意图需要一个上下文作为第一个参数第一个错误,请参阅:

星状体;是上下文的一种方法,因此您需要重新设置上下文在第二个错误中,请参阅:


可能的副本不是上下文。将上下文传递到MyAsyncTask。@Sebastin:我正在获取无法解析为变量的上下文?我将其设置为Intent=newintentcontext,MainActivity.class;澄清一下,您是否正在尝试从AsyncTask内部启动MainActivity?AsyncTask也在MainActivity内吗?@lemuel:是的,我正在尝试在AsyncTask内启动MainActivity。不,AsyncTask是另一个类PostData的内部类现在我在调用PostData类中的execute方法时遇到了一个错误。此行未定义构造函数PostData.MyAsyncTask new MyAsyncTask.executejSONString;当然,您必须像这样调用PostData.MyAsyncTaskthis。如果它在活动中。或者PostData.MyAsyncTaskgetActivity,如果您在片段中调用它。这是因为您现在将MyAsyncTaskContext作为构造函数,而不仅仅是MyAsyncTask。将上下文传递给它。我已经将整个PosData类添加到问题代码中,因为我尝试这样调用它,并且我得到的方法MyAsyncTaskString对于类型PostData是未定义的?然后将上下文添加到public void post_dataString jSONString的参数,context context。。。。然后调用新的MyAsyncTaskcontext.executejSONString;
Intent intent = new Intent(mContext, MainActivity.class);
mContext.startActivity(intent);