在主用户界面上执行网络进程
我的Android应用程序需要一些基本数据才能运行。这些数据是使用JSON从服务器下载的。在Xcode中,我简单地使用了发送同步请求,但是我注意到当我在主UI上进行联网时,Eclipse给了我一个错误。 在asynctask上发现了很多东西,但我希望我的应用程序等待下载所需数据(同步?)。我试过使用asynctask .execute()。get()并在onPostExecute中设置变量,但是当我返回变量时,我得到一个NullPointerException异常。有人知道如何使这项工作?在应用程序运行之前,我真的需要这些数据,所以我希望我的应用程序能够等待数据下载。在主用户界面上执行网络进程
MainActivity调用此:
SingletonClass appIDSingleton = SingletonClass.getInstance();
this.ID = appIDSingleton.getAppID();
单例类:
public String getAppID() {
try {
new DownloadAppID().execute(APP_ID_URL).get(5000, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TimeoutException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return AppID; //AppID is still NULL (because the download isnt finished yet?)
}
private class DownloadAppID extends AsyncTask<String, Void, String> {
@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
protected void onPostExecute(String result) {
System.out.println(result);
AppID = result;
}
}
你要明白,你的getAppID
方法不能返回将被异步计算的结果。
你可以例如提供一个监听器,你的异步任务,以便通知时,应用程序号:
SingletonClass appIDSingleton = SingletonClass.getInstance();
appIDSingleton.getAppID(new AppIdDownloadListener() {
@Override
public void appIDAvailable(String appId) {
this.ID = appId;
}
});
public void getAppID(AppIdDownloadListener listener) {
try {
new DownloadAppID(listener).execute(APP_ID_URL).get(5000, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TimeoutException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public interface AppIdDownloadListener {
public void appIDAvailable(String appId);
}
private class DownloadAppID extends AsyncTask<String, Void, String> {
private AppIdDownloadListener listener;
public DownloadAppID(AppIdDownloadListener listener) {
this.listener = listener;
}
@Override
protected String doInBackground(String... params) {
/* Your stuff here */
}
@Override
protected void onPostExecute(String result) {
System.out.println(result);
listener.appIDAvailable(result);
}
}
这对我有效!它有点不同,但你的答案涵盖了一切!谢谢 –
最后一个问题。这是处理来自Web的JSON数据的正确方法吗? –
听起来不错,尽管你可能会摆脱这种'getAppID'方法我猜。 – fiddler
你在哪里得到NullPointerException异常? – Yahor10
编辑我的帖子。我在getAppID方法返回时得到空值 –
您在哪里调用这些行? SingletonClass appIDSingleton = SingletonClass.getInstance(); this.ID = appIDSingleton.getAppID(); :) – Yahor10