我正在编写一个必须从Web服务获取一些json对象的应用程序。 它运行良好。但是,我想找到一种方法来避免用户,以防这些json对象无法下载。 所以,我使用了一个虚假的URL。 现在我的应用程序关闭而不会避开用户。
电话:
/*
* Async call to fill the category spinner
*/
private class CallCategory extends AsyncTask<Void,Void,Void>{
private ProgressDialog dialog;
protected void onPreExecute() {
this.dialog = ProgressDialog.show(ApPictureActivity.this, "Chargement", "Récupération des catégories...", true);
}
protected void onPostExecute(Void v) {
dialog.cancel();
Log.i("fill the spiner", _ListCategory.toString());
if(!_ListCategory.isEmpty())
FillSpinner();
else
{
AlertDialog alertDialog = new AlertDialog.Builder(ApPictureActivity.this).create();
alertDialog.setTitle(getResources().getString(R.string.errorCategoryTitle));
alertDialog.setMessage(getResources().getString(R.string.errorCategoryMessage));
//add "Ok button"
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
} });
alertDialog.show();
//log
Log.i("List category empty", "No categories");
//ApPictureActivity.this.finish();
}
}
protected Void doInBackground(Void... params) {
try{
_ListCategory = ServerCall.GetCategory();}
catch (Exception e) {
Log.i("load categories error",e.toString());
}
return null;
}
}
ServerCall.GetCategory方法:
public static List<Category> GetCategory (){
try{
Log.i("Call", "Call categories");
String url = "http://appicture.cloudapp.net/API/yoyo";
RestClient client = new RestClient(url);
//Call
client.Execute(RequestMethod.GET);
if(client.getResponseCode() == 200){
//Get the response
String response = client.getResponse();
//build list of categories
Type listType = new TypeToken<ArrayList<Category>>() {
}.getType();
List<Category> categories = new Gson().fromJson(response, listType);
Log.i("Response", response.toString());
return categories;
}
}
catch (Exception e) {
Log.i("Error", e.toString());
}
return null;
}
Logcat异常:
07-09 09:37:49.446: E/AndroidRuntime(4322): Uncaught handler: thread main exiting due to uncaught exception
07-09 09:37:49.455: E/AndroidRuntime(4322): java.lang.NullPointerException
07-09 09:37:49.455: E/AndroidRuntime(4322): at ApPicture.Android.ApPictureActivity$CallCategory.onPostExecute(ApPictureActivity.java:299)
07-09 09:37:49.455: E/AndroidRuntime(4322): at ApPicture.Android.ApPictureActivity$CallCategory.onPostExecute(ApPictureActivity.java:1)
07-09 09:37:49.455: E/AndroidRuntime(4322): at android.os.AsyncTask.finish(AsyncTask.java:417)
07-09 09:37:49.455: E/AndroidRuntime(4322): at android.os.AsyncTask.access$300(AsyncTask.java:127)
07-09 09:37:49.455: E/AndroidRuntime(4322): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
07-09 09:37:49.455: E/AndroidRuntime(4322): at android.os.Handler.dispatchMessage(Handler.java:99)
07-09 09:37:49.455: E/AndroidRuntime(4322): at android.os.Looper.loop(Looper.java:123)
07-09 09:37:49.455: E/AndroidRuntime(4322): at android.app.ActivityThread.main(ActivityThread.java:4363)
07-09 09:37:49.455: E/AndroidRuntime(4322): at java.lang.reflect.Method.invokeNative(Native Method)
07-09 09:37:49.455: E/AndroidRuntime(4322): at java.lang.reflect.Method.invoke(Method.java:521)
07-09 09:37:49.455: E/AndroidRuntime(4322): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-09 09:37:49.455: E/AndroidRuntime(4322): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-09 09:37:49.455: E/AndroidRuntime(4322): at dalvik.system.NativeStart.main(Native Method)
我确切地知道此代码正在使用正确的URL。所以这不是一个实现问题。也许是一个例外而不是捕获。
问候。
答案 0 :(得分:2)
我跟着dj aquell adivies,我改变了我的onPostExecute方法:
protected void onPostExecute(Void v) {
dialog.cancel();
if(_ListCategory != null){
Log.i("fill the spiner", _ListCategory.toString());
FillSpinner();}
else
{
AlertDialog alertDialog = new AlertDialog.Builder(ApPictureActivity.this).create();
alertDialog.setTitle(getResources().getString(R.string.errorCategoryTitle));
alertDialog.setMessage(getResources().getString(R.string.errorCategoryMessage));
//add "Ok button"
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//log
Log.i("List category empty", "No categories");
ApPictureActivity.this.finish();
} });
alertDialog.show();
}
}