Android: get result from callback (networking KOUSH ION)

后端 未结 3 854
旧巷少年郎
旧巷少年郎 2021-01-04 21:28

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

相关标签:
3条回答
  • 2021-01-04 21:38

    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
                }
            }
        });
    }
    
    0 讨论(0)
  • 2021-01-04 21:45

    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.

    0 讨论(0)
  • 2021-01-04 21:55

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

    });

    0 讨论(0)
提交回复
热议问题