David
David

Reputation: 1709

App shut down during call to a web service

I'm writting an application which has to get some json objects from a web service. It's running well. But, I want to find a way to avert the user in case if those json objects can't be download. So, I use an false URL. And now my app force close without avert the user.

The call :

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

And the ServerCall.GetCategory method :

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 exception :

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)

I precise that this code is working with the correct URL. So it's not an implementation problem. Maybe an exception not catch.

Regards.

Upvotes: 0

Views: 111

Answers (1)

David
David

Reputation: 1709

I followed dj aquell adivies and I change my onPostExecute method by :

    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();
        }
    }

Upvotes: 2

Related Questions