Android: get result from callback (networking KOUSH ION)

佐手、 提交于 2019-12-05 21:13:40

问题


For my app I need to contact our API from our server which returns some JSON.

While downloading the JSON, it should display a progressbar.

I figured I should use Android's AsyncTask to handle the GUI while doing network operations, so I wrote the following within my Activity:

 class DownloadManager extends AsyncTask<String, Void, Boolean> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mLoadingSpinner.setVisibility(View.VISIBLE);
    }

    @Override
    protected Boolean doInBackground(String... params) {
        String id = params[0];
        downloadUtility.getId(id);

        return true;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);

        mLoadingSpinner.setVisibility(View.INVISIBLE);

    }

}

Basically, onPreExecute shows the loading spinner, the doInBackGround downloads some JSON, and onPostExecute stops the spinner.

The question is, within the downloadUtility.getId(id) I need to either:

  1. Open a new intent if the download succeeded.
  2. Stay on the same activity and display an error toast if the download failed.

The code for the getId:

public Future getId(final String id) {
    // set url
    String url = IPAddress.PRODUCTION + Variables.get_id+ id;
    downloading = Ion.with(context)
            .load("GET", url)
            .asJsonObject()
            .withResponse()
            .setCallback(new FutureCallback<Response<JsonObject>>() {
                @Override
                public void onCompleted(Exception e, Response<JsonObject> response) {
                    //try catch here for null getHeaders
                    if (response != null) {
                        if (response.getHeaders().code() == 200) {

                            //SUCCESS !! Open new intent!


                        } else {
                            //FAIL!! Show TOAST!
                        }
                    }

                }
            });


    return downloading;

}

As you can see, I'm returning a future object. How do I know from the future object if the onCompleted (void) either gave a success or fail, so I can handle the result (success: open new intent, fail: toast) in the asynctask?


回答1:


Here you are running one asynctask inside another asyctask this is not a proper way you can call your getId method directly in your activity it won't be required another asynctask because the following code it self a asynctask.

 downloading = Ion.with(context)
        .load("GET", url)
        .asJsonObject()
        .withResponse()
        .setCallback(new FutureCallback<Response<JsonObject>>() {
            @Override
            public void onCompleted(Exception e, Response<JsonObject> response) {
                //try catch here for null getHeaders
                if (response != null) {
                    if (response.getHeaders().code() == 200) {

                        //SUCCESS !! Open new intent!


                    } else {
                        //FAIL!! Show TOAST!
                    }
                }

            }
        });

//Add new Answer

If you want to separate entire download code from your activity then you can create custom callBack in your download Utility class. It will acts like a communicator between activity and your Download class. I just give a way to do this task on bellow.

DownloadUtility class seams look like bellow

public class DownloadUtility {


//DO Your all other Stuff

/**
 * Custom Callback
 */
public interface customCallBack {
    void onCompleted(Exception e, Response<JsonObject> response);
}


/**
 * Your getID code 
 * 
 * @param context
 * @param id
 * @param mLoadingSpinner
 * @param callBack
 */
public static void getId(Activity context,final String id, Spinner mLoadingSpinner, final customCallBack callBack) {
    // set url
    mLoadingSpinner.setVisibility(View.VISIBLE);
    String url = IPAddress.PRODUCTION + Variables.get_id + id;
    downloading = Ion.with(context)
            .load("GET", url)
            .asJsonObject()
            .withResponse()
            .setCallback(new FutureCallback<Response<JsonObject>>() {
                @Override
                public void onCompleted(Exception e, Response<JsonObject> response) {
                    mLoadingSpinner.setVisibility(View.GONE);
                   if(callBack != null)
                    callBack.onCompleted(e,response);
                    }
                }
            });
}

}

make a call on your Activity

DownloadUtility.getId(this, "ID", spinnerObj, new DownloadUtility.customCallBack() {
@Override
public void onCompleted(Exception e, Response<JsonObject> response) {
    if (response != null) {
        if (response.getHeaders().code() == 200) {
            //SUCCESS !! Open new intent!
        } else {
            //FAIL!! Show TOAST!
        }
}

});




回答2:


I don't think that you need AsyncTask for network operation because your ion library is already using asynctask internally. you can do like this

mLoadingSpinner.setVisibility(View.VISIBLE);
downloading = Ion.with(context)
            .load("GET", url)
            .asJsonObject()
            .withResponse()
            .setCallback(new FutureCallback<Response<JsonObject>>() {
                @Override
                public void onCompleted(Exception e, Response<JsonObject> response) {
                    //try catch here for null getHeaders
                    if (response != null) {
                        if (response.getHeaders().code() == 200) {

                            //SUCCESS !! Open new intent!
                            mLoadingSpinner.setVisibility(View.INVISIBLE);
                        } else {
                            mLoadingSpinner.setVisibility(View.INVISIBLE);
                        }
                    }

                }
            });
    return downloading;

let me know if some issue.




回答3:


In my opinion the cleanest solution is to create a service that handles the dirty download logic and returns a future of your custom response class, that contains the success info and the json object.

// in e.g JsonResponse.java
public class JsonResponse() {
    public boolean ok;
    public JsonObject json;
}

// in Service.java
public Future<JsonResponse> getId(final String id) {
    final SimpleFuture<JsonResponse> jsonFuture = new SimpleFuture<>();

    String url = IPAddress.PRODUCTION + Variables.get_id + id;
    Ion.with(context)
    .load("GET", url)
    .asJsonObject()
    .withResponse()
    .setCallback(new FutureCallback<Response<JsonObject>>() {
        @Override
        public void onCompleted(Exception e, Response<JsonObject> response) {
            JsonResponse jsonResponse = new JsonResponse();
            if (response != null) {
                if (response.getHeaders().code() != 200) {
                    jsonResponse.ok = false;
                } else {
                    jsonResponse.ok = true;
                    jsonResponse.json = response.getResult();   
                }
            }
            jsonFuture.setComplete(jsonResponse);
        }
    });

    return jsonFuture;
}


// in Activity.java
private void loadUser(String userId) {    
    mLoadingSpinner.setVisibility(View.VISIBLE);

    service.getId(userId)
    .setCallback(new FutureCallback<JsonResponse>() {

        // onCompleted is executed on ui thread
        @Override
        public void onCompleted(Exception e, JsonResponse jsonResponse) {
            mLoadingSpinner.setVisibility(View.GONE);
            if (jsonResponse.ok) {
                // Show intent using info from jsonResponse.json
            } else {
                // Show error toast
            }
        }
    });
}


来源:https://stackoverflow.com/questions/34265401/android-get-result-from-callback-networking-koush-ion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!