来自Inside Function的JsonRequester Library Access Listener

时间:2016-04-21 12:30:06

标签: android json listener android-volley

我正在使用使用volley的JsonRequester Library来从URL中获取JSON数据。

我的问题是,如何从getJson函数内部的onResponse侦听器返回JSONObject?

这是我的JsonHelpers类:

import android.content.Context;
import android.support.annotation.Nullable;

import com.alirezaafkar.json.requester.interfaces.Methods;
import com.alirezaafkar.json.requester.interfaces.Response;
import com.alirezaafkar.json.requester.requesters.JsonObjectRequester;
import com.alirezaafkar.json.requester.requesters.RequestBuilder;
import com.android.volley.VolleyError;
import org.json.JSONObject;

public class JSONHelper {

    public static int USER_REQUEST = 0;

    public JSONObject getJson(Context context, String url){

        JsonObjectRequester mUserRequester;




        JsonObjectListener objectListener = new JsonObjectListener();

        mUserRequester = new RequestBuilder(context)
                .requestCode(USER_REQUEST)
                .showError(true)
                .buildObjectRequester(objectListener);

        mUserRequester.request(Methods.GET, url);


    }

    private class JsonObjectListener extends Response.SimpleObjectResponse {
        @Override
        public void onResponse(int requestCode, @Nullable JSONObject jsonObject) {

        }

        @Override
        public void onErrorResponse(int requestCode, VolleyError volleyError, @Nullable JSONObject errorObject) {
            //Error (Not server or network error)
        }

        @Override
        public void onFinishResponse(int requestCode, VolleyError volleyError, String message) {
            //Network or Server error
        }

        @Override
        public void onRequestStart(int requestCode) {
            //Show loading or disable button
        }

        @Override
        public void onRequestFinish(int requestCode) {
            //Hide loading or enable button
        }
    }

}

1 个答案:

答案 0 :(得分:1)

<强> 回调

public interface Callback {
    public void onResponse(JsonObject jsonObject);
}

当您需要JsonObject

时,您的班级
getJson(this, url, new Callback(){
    public void onResponse(JsonObject jsonObject){
         // Do what you want here
}
});

<强> 的getJSON

public void getJson(Context context, String url, Callback callBack){
        JsonObjectRequester mUserRequester;
        JsonObjectListener objectListener = new JsonObjectListener();

        mUserRequester = new RequestBuilder(context)
                .requestCode(USER_REQUEST)
                .showError(true)
                .buildObjectRequester(objectListener);

        mUserRequester.request(Methods.GET, url);
    }

<强> JsonObjectListener

private class JsonObjectListener extends Response.SimpleObjectResponse {
    private Callback callback;
    public JsonObjectListener(Callback callBack){
         this.callBack = callBack;
    }

    @Override
    public void onResponse(int requestCode, @Nullable JSONObject jsonObject){
        callback.onResponse(jsonObject);
    }
 }

它应该有效,告诉你是否能解决你的问题。