0

我正在编写一个应用程序,它必须从 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。所以这不是实施问题。也许一个异常没有捕获。

问候。

4

1 回答 1

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();
        }
    }
于 2012-07-09T10:38:52.660 回答