0

私のonPostExecuteメソッドは結果を返す必要があるonCreate()ので、それを処理することができますが、そこにたどり着くのに苦労しています。

public class ScheduleList extends Activity {

    public String url = "http://www.bbc.co.uk/bbcnews/programmes/schedules/today.json";

    private DownloadJSON mTask = null;


    public static String fetchedJSON = "";
    public static String getFetchedJSON() {
       return fetchedJSON;
    }
  public static void setFetchedJSON(String fetchedJSON) {
     ScheduleList.fetchedJSON = fetchedJSON;
  }


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_schedule_list);

    // Show the Up button in the action bar.
    setupActionBar();

    TextView tvNotice = (TextView) findViewById(R.id.schedulePreamble);
    tvNotice.setText("Well, hello there...");

    System.out.println("Hello");
    downloadPage();
    System.out.println(getFetchedJSON());

}


public class DownloadJSON extends AsyncTask<String, Integer, String> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);

    }

    @Override
    protected String doInBackground(String... urls) {
        String response = "";
        for (String url : urls) {
            DefaultHttpClient client = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);
            try {
                HttpResponse execute = client.execute(httpGet);
                InputStream content = execute.getEntity().getContent();

                BufferedReader buffer = new BufferedReader(new InputStreamReader(
                        content));
                String s = "";
                while ((s = buffer.readLine()) != null) {
                    response += s;
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return response;
    }

    @Override
    public void onPostExecute(String result) {
        setFetchedJSON(result);
    }


}

private void downloadPage() {
    if (mTask != null && mTask.getStatus() != DownloadJSON.Status.FINISHED) {
        mTask.cancel(true);
    }
    mTask = (DownloadJSON) new DownloadJSON().execute(new String[] { url });
}

}

コードを実行するたびにSystem.out.println(getFetchedJSON());何も返されないということonPostExecuteは、実際には変数の内容をまったく変更していないということですよね?

4

2 に答える 2

0

メソッドdownloadPage()はタスクを起動します。このタスクは他のスレッドで実行され、タスクを起動した直後に結果を出力したいのですが、タスクはまだ終わっていません。このコードを試してください:

public class ScheduleList extends Activity {

public String url = "http://www.bbc.co.uk/bbcnews/programmes/schedules/today.json";

private DownloadJSON mTask = null;


public static String fetchedJSON = "";
public static String getFetchedJSON() {
    return fetchedJSON;
}
public static void setFetchedJSON(String fetchedJSON) {
    ScheduleList.fetchedJSON = fetchedJSON;
}


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_schedule_list);

    // Show the Up button in the action bar.
    setupActionBar();

    TextView tvNotice = (TextView) findViewById(R.id.schedulePreamble);
    tvNotice.setText("Well, hello there...");

    System.out.println("Hello");
    downloadPage();

}

public void printFetchedJSON() {
    System.out.println(getFetchedJSON());
}


public class DownloadJSON extends AsyncTask<String, Integer, String> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);

    }

    @Override
    protected String doInBackground(String... urls) {
        String response = "";
        for (String url : urls) {
            DefaultHttpClient client = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);
            try {
                HttpResponse execute = client.execute(httpGet);
                InputStream content = execute.getEntity().getContent();

                BufferedReader buffer = new BufferedReader(new InputStreamReader(
                        content));
                String s = "";
                while ((s = buffer.readLine()) != null) {
                    response += s;
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return response;
    }

    @Override
    public void onPostExecute(String result) {
        setFetchedJSON(result);
        printFetchedJSON();
    }


}

private void downloadPage() {
    if (mTask != null && mTask.getStatus() != DownloadJSON.Status.FINISHED) {
        mTask.cancel(true);
    }
    mTask = (DownloadJSON) new DownloadJSON().execute(new String[] { url });
}

}

于 2013-11-10T12:04:42.930 に答える