我的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
实际上并没有改变任何变量的内容,是吗?