使用 volley 教程 slidenerd 解析没有标题的 JSON

Parsing JSON with no title with volley tutorial slidenerd

我在 youtube 上观看了 slidenerd 教程。我想得到 json。我无法得到我的 json。我认为问题是当我解析 json 但我看不到它时。这是我的请求方式

    public static JSONObject requestMoviesJSON(RequestQueue requestQueue, String url) {
    JSONObject response = null;
    RequestFuture<JSONObject> requestFuture = RequestFuture.newFuture();

    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,
            url,
            (String)null, requestFuture, requestFuture);

    requestQueue.add(request);
    try {
        response = requestFuture.get(30000, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
        L.m(e + "");
    } catch (ExecutionException e) {
        L.m(e + "");
    } catch (TimeoutException e) {
        L.m(e + "");
    }
    return response;
}

这就是我得到回复的方式

    JSONObject response = Requestor.requestMoviesJSON(requestQueue, "http://my_url");
    ArrayList<Milkbox> listMilkboxes = Parser.parseMilkboxesJSON(response);

这是我的解析函数

parseMilkboxesJSON(JSONObject response) {
    ArrayList<Milkbox> listMilkbox = new ArrayList<>();

    if (response != null && response.length() > 0) {
        try {
            JSONArray arrayMilkboxes = new JSONArray(response);

            for (int i = 0; i < arrayMilkboxes.length(); i++) {
                int id = -1;
                long phone = -1L;
                long imei = -1L;
                String city = Constants.NA;
                String address = Constants.NA;

                JSONObject currentMilkbox = arrayMilkboxes.getJSONObject(i);

                id = currentMilkbox.getInt(KEY_ID);
                phone = currentMilkbox.getLong(KEY_PHONE);
                imei = currentMilkbox.getLong(KEY_IMEI);
                city = currentMilkbox.getString(KEY_CITY);
                address = currentMilkbox.getString(KEY_ADDRESS);


                Milkbox milkbox = new Milkbox();
                milkbox.set_id(id);
                milkbox.set_phone(phone);
                milkbox.set_imei(imei);
                milkbox.set_city(city);
                milkbox.set_address(address);

                listMilkbox.add(milkbox);
                text = id + " " + phone + " " + imei + " " + city + " " + address;
            }

        } catch (JSONException e) {

        }
        //      L.t(getActivity(), listMovies.size() + " rows fetched");
    }

    return listMilkbox;
}

我不知道这样做是否正确JSONArray arrayMilkboxes = new JSONArray(response);。伙计们,我解析它的方式是否正确?

不,这不是您将 JSONObject 解析为 JSONArray,在 String 中获取您的响应,然后解析它 JSONArray

public void requestMoviesJSON(RequestQueue requestQueue, String url) {

    public void requestMoviesJSON(RequestQueue requestQueue, String url) {
    StringRequest jobj = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {

            ArrayList<Milkbox> listMilkbox = new ArrayList<>();

            if (response != null && response.length() > 0) {
                try {

                    JSONArray arrayMilkboxes = new JSONArray(response);

                    for (int i = 0; i < arrayMilkboxes.length(); i++) {
                        int id = -1;
                        long phone = -1L;
                        long imei = -1L;
                        String city = Constants.NA;
                        String address = Constants.NA;

                        JSONObject currentMilkbox = arrayMilkboxes.getJSONObject(i);

                        id = currentMilkbox.getInt(KEY_ID);
                        phone = currentMilkbox.getLong(KEY_PHONE);
                        imei = currentMilkbox.getLong(KEY_IMEI);
                        city = currentMilkbox.getString(KEY_CITY);
                        address = currentMilkbox.getString(KEY_ADDRESS);


                        Milkbox milkbox = new Milkbox();
                        milkbox.set_id(id);
                        milkbox.set_phone(phone);
                        milkbox.set_imei(imei);
                        milkbox.set_city(city);
                        milkbox.set_address(address);

                        listMilkbox.add(milkbox);
                        text = id + " " + phone + " " + imei + " " + city + " " + address;
                    }
                } catch (JSONException e) {

                }
                //      L.t(getActivity(), listMovies.size() + " rows fetched");
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            volleyError.printStackTrace();

        }
    });
    jobj.setRetryPolicy(new DefaultRetryPolicy(
            DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 3,
            0,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

    requestQueue.add(jobj);
}

现在调用 requestMoviesJSON(requestQueue,url) 方法进行网络调用。

我没有看到你将 KEY_ID 声明为常量字符串,所以我假设你想使用 KEY_ID 作为你的令牌来获取你的整数值。因此,您需要做的就是在您的令牌周围添加“”。 JSONObject 的方法需要字符串作为参数来获取标记。

Milkbox milkbox = new Milkbox();
milkbox.set_id( currentMilkbox.getInt("KEY_ID") );
milkbox.set_phone( currentMilkbox.getLong("KEY_PHONE") );
milkbox.set_imei( currentMilkbox.getLong("KEY_IMEI") );
milkbox.set_city( currentMilkbox.getString("KEY_CITY") );
milkbox.set_address( currentMilkbox.getString("KEY_ADDRESS") );