How to use the ion library in Android?

pana doll picture pana doll · Jul 11, 2015 · Viewed 7.9k times · Source

I am using the ion library to download the json object response from php web services. I am using it this way :

Ion.with(getActivity())
            .load("http://amd.com/loginService.php")
            .progressBar(progressBar).progress(new ProgressCallback() {
        @Override
        public void onProgress(long downloaded, long total) {
            int mProgress = (int) (100 * downloaded / total);
            progressBar.setProgress(mProgress);
        }
    })
            .setMultipartParameter("fb_id", id)
            .setMultipartParameter("fb_token", AccessToken.getCurrentAccessToken().toString())
            .asJsonObject()
            .setCallback(new FutureCallback<JsonObject>() {
                @Override
                public void onCompleted(Exception e, JsonObject result) {
                    if (result == null) {
                        Toast.makeText(getActivity(), "Error", Toast.LENGTH_SHORT).show();
                        progressBar.setVisibility(View.GONE);
                    } else {


                        Log.v("IonResult", result.toString());
                        progressBar.setVisibility(View.GONE);

                    }
                }
            });

Now I want to ask two questions:

  1. How can I show the "please wait" and the progress bar in a good manner? The progress bar should appear and then disappear when the response of server has been achieved.

  2. How to parse the json which I am getting in response? I have logged my results like this

    Log.v("IonResult", result.toString());

and I can see the response, but how can I use it and parse it and get the items I want?

Please help, I know it is a basic question, but as I am beginner in Android, please help me improve. Thanks.

Answer

Otacilio Oliveira picture Otacilio Oliveira · Jul 14, 2015

I started using Ion recently, so I'm not sure if this is the best way to do that (please, if you know, leave a comment here!).

So, I'm using callbacks to return the value to another class.

public interface ResponseCallback {

    public void onSuccess(String result);

    public void onError(String result);

}

One example in my code:

public void getChats(final Context context, final String url,final ResponseCallback responseCallback) {


 Ion.with(context)

        .load(url)
        .asString()
        .setCallback(new FutureCallback<String>() {
            @Override
            public void onCompleted(Exception e, String result) {
                if (e != null) {
                    if (responseCallback != null) {
                        responseCallback.onError(e.toString());
                    }
                    Toast.makeText(context, "Error loading chat list.", Toast.LENGTH_SHORT);
                    return;
                }

                if (responseCallback != null) {
                    responseCallback.onSuccess(result);
                }
            }
        });
}

So, when I call getChats(), I implement ResponseCallback methods. I hope it helps, even that I think there's a better way to do that.