2012-07-09 148 views
0

期间关闭我书面方式具有从Web服务中获得一些JSON对象的应用程序。 它运行良好。但是,如果这些json对象无法下载,我想找一种避免用户的方法。 所以,我使用了一个错误的URL。 现在我的应用程序强制关闭,避免用户。应用程序调用Web服务

召唤:

/* 
* 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

logcat的输出异常! – AAnkit 2012-07-09 10:04:15

+0

@大卫哪一个是ApPictureActivity.java行号299文件? – 2012-07-09 10:11:18

+0

@djaqeel Log.i( “补spiner”,_ListCategory.toString()); – David 2012-07-09 10:19:14

回答

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(); 
     } 
    } 
相关问题