Android: get result from callback (networking KOUSH ION)

烂漫一生 提交于 2019-12-04 03:52:24

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!
        }
}

});

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.

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