簡體   English   中英

Android Volley 為什么JsonObjectRequest沒有調用onResponse

[英]Android Volley Why does JsonObjectRequest does not call onResponse

我正在嘗試讓我的凌空JsonObjectRequest工作。 調用onResponse() getById() ,不會調用 onResponse()。 我已經有一個工作職位請求。 所以連接參數是正確的。 我在 LogCat 中沒有收到任何錯誤響應或有用的響應。

我創建了一個測試 class 以隔離 getById 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;
    }
}

這是 singleton RequestQueue隊列 class。

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 是一個異步網絡庫,因此您無法像以前那樣從網絡返回結果。 您正在 OnResponse 內部初始化Customer model class 並從外部返回它。 所以當你執行這個操作時會發生什么

創建請求 -> 添加到請求隊列 -> getById() 方法返回 null

最后,return 語句不會等到響應到達。 您每次都會得到 null。 因此,您要做的就是在 API 返回結果或錯誤時實現自定義回調。

為內部測試 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) {

            }
        });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM