Android Volley 为什么 JsonObjectRequest 不调用 onResponse

Android Volley Why does JsonObjectRequest does not call onResponse

我正在努力让我的截击 JsonObjectRequest 发挥作用。调用 getById() 方法时,不会调用 onResponse()。我已经有一个有效的 post 请求。所以连接参数是正确的。我在 LogCat.

中没有收到任何错误响应或有用的响应

我创建了一个测试 class 以隔离 getById() 方法。

public class Test {

    Customer customer;
    public Customer getById(int id) {

        Log.i("CustomerDAO getById", "getById called");
        String urlJsonObj = "http://192.168.0.39:3000/Customers/" + id;
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, urlJsonObj, null, new Response.Listener < JSONObject > () {

            @Override
            public void onResponse(JSONObject response) {
                try {
                    // Parsing json object response
                    // response will be a json object

                    customer = new Customer(response.getInt("customernumber"), response.getString("name"),
                        response.getString("lastname"), response.getString("phonenumber"),
                        response.getInt("addressid"), response.getString("password"));
                } catch (JSONException e) {
                    Log.e("JSON Exception:", e.toString());
                }

            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("GetByIdErrorResponse", error.toString());
            }
        });

        AppController.getInstance().addToRequestQueue(request);
        return customer;
    }
}

这是单例RequestQueueclass.

public class AppController extends Application {

    public static final String TAG = AppController.class
            .getSimpleName();

    private RequestQueue mRequestQueue;

    private static AppController mInstance;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
    }

    public static synchronized AppController getInstance() {
        return mInstance;
    }

    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(getApplicationContext());
            Log.i("RequestQueue= ", mRequestQueue.toString());
        }

        return mRequestQueue;
    }
}

这就是我调用 getById() 方法的方式

Test test = new Test();
Entities.Customer check = test.getById(1);

您的代码中有一些错误。由于 volley 是一个异步网络库,您不能像您所做的那样 return 来自网络的结果。您正在 OnResponse 中初始化您的 Customer 模型 class 并从其外部 return 对其进行初始化。 所以当你执行这个操作时会发生什么是

创建请求 -> 添加到请求队列 -> getById() 方法 returns null

return 语句,终于,不会等到响应到达。你每次都会得到 null 。因此,您要做的是在 API return 为结果或错误时实现自定义回调。

为内部测试创建接口 class 以处理 API 响应。

interface APICallback {
  void onSuccess(Customer customer);
  void onError(String error);
}

将这些接口实现连同id一起传递给getById()方法

public void getById(int id,APICallback callback) {
  ....
}

根据结果调用方法

@Override
            public void onResponse(JSONObject response) {
                try {
                    // Parsing json object response
                    // response will be a json object

                    Customer customer = new Customer(response.getInt("customernumber"), response.getString("name"),
                            response.getString("lastname"), response.getString("phonenumber"),
                            response.getInt("addressid"), response.getString("password"));
                    callback.onSuccess(customer);
                } catch (JSONException e) {
                    Log.e("JSON Exception:", e.toString());
                }

            }
 @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("GetByIdErrorResponse", error.toString());
                callback.onError(error.toString());
            }

现在您可以按如下方式调用 getById()

Test test = new Test();
        test.getById(1, new Test.APICallback() {
            @Override
            public void onSuccess(Customer customer) {

            }

            @Override
            public void onError(String error) {

            }
        });