1

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.

4

1 回答 1

0

我最近开始使用 Ion,所以我不确定这是否是最好的方法(如果您知道,请在此处发表评论!)。

所以,我使用回调将值返回给另一个类。

public interface ResponseCallback {

    public void onSuccess(String result);

    public void onError(String result);

}

我的代码中的一个示例:

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

因此,当我调用 getChats() 时,我实现了 ResponseCallback 方法。我希望它有所帮助,即使我认为有更好的方法可以做到这一点。

于 2015-07-14T02:21:05.950 回答