【问题标题】:How to GET volley JSONArray in Android如何在 Android 中获取凌空 JSONArray
【发布时间】:2018-10-27 04:02:50
【问题描述】:

我是 Volley JSONArray 的新手。我无法在 txtResponsesetText,因为我总是在 onErrorResponse 处收到此错误。

这是我的 Logcat

......com.example.simplevolley.simplevolley W/System.err: org.json.JSONException: Value [{"com_branch_id":7,"com_branch_comId":1,"com_branch_name":"JT Temerloh ","com_branch_manager":"Tn Hj Ahmad Bin Hj Mohd Sidin","com_branch_addr":"No.58, Jalan Ibrahim (Jln Masjid Abu Bakar)","com_branch_region":"Temerloh","com_branch_state":"PAHANG" ,"com_branch_country":"MALAYSIA","com_branch_poscode":"28000","com_branch_email":"tmlh@juaratravel.com.my","com_branch_licence":null,"com_branch_phoneNum":"09-2965625","com_branch_fax" :null,"com_branch_fb":null,"com_branch_ig":null},{"com_branch_id":9,"com_branch_comId":1,"com_branch_name":"JT Nibong Tebal","com_branch_manager":"Pejabat Penang","com_branch_addr ":"No. 10, Tingkat Atas, Jln Pekaka 1, Tmn Pekaka,","com_branch_region":"Nibong Tebal","com_branch_state":"PULAU PINANG","com_branch_country":"MALAYSIA","com_branch_poscode":" 14300","com_branch_email":"pen@juaratravel.com.my","com_branch_licence":null,"com_branch_phoneNum":"05-7171877","com_branch _fax":"057161877","com_branch_fb":null,"com_branch_ig":null}......

我想阅读这 JSON 行,但因为它以 JSONArray 开头,所以我很困惑。

{
"res": true,
"datas": [
    {
        "com_branch_id": 7,
        "com_branch_comId": 1,
        "com_branch_name": "JT Temerloh",
        "com_branch_manager": "Tn Hj Ahmad Bin Hj Mohd Sidin",
        "com_branch_addr": "No.58, Jalan Ibrahim (Jln Masjid Abu Bakar)",
        "com_branch_region": "Temerloh",
        "com_branch_state": "PAHANG",
        "com_branch_country": "MALAYSIA",
        "com_branch_poscode": "28000",
        "com_branch_email": "tmlh@juaratravel.com.my",
        "com_branch_licence": null,
        "com_branch_phoneNum": "09-2965625",
        "com_branch_fax": null,
        "com_branch_fb": null,
        "com_branch_ig": null
    },
    {
        "com_branch_id": 9,
        "com_branch_comId": 1,
        "com_branch_name": "JT Nibong Tebal",
        "com_branch_manager": "Pejabat Penang",
        "com_branch_addr": "No. 10, Tingkat Atas, Jln Pekaka 1, Tmn Pekaka,",
        "com_branch_region": "Nibong Tebal",
        "com_branch_state": "PULAU PINANG",
        "com_branch_country": "MALAYSIA",
        "com_branch_poscode": "14300",
        "com_branch_email": "pen@juaratravel.com.my",
        "com_branch_licence": null,
        "com_branch_phoneNum": "05-7171877",
        "com_branch_fax": "057161877",
        "com_branch_fb": null,
        "com_branch_ig": null
    },]}

我需要获取所有 数据 值。这是我的代码

 private void makeJsonArrayRequest() {
    showpDialog();

    JsonArrayRequest req = new JsonArrayRequest(urlJsonArry,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d("res", response.toString());

                    try {


                        jsonResponse = "";
                        for (int i = 0; i < response.length(); i++) {


                            JSONObject branchi = (JSONObject) response.get(i);
                            JSONObject branch = branchi.getJSONObject("datas");


                            Integer com_branch_id = Integer.valueOf(branch.getString("com_branch_id"));
                            Integer com_branch_comId = Integer.valueOf(branch.getString("com_branch_comId"));
                            String com_branch_name = branch.getString("com_branch_name");
                            String com_branch_manager = branch.getString("com_branch_manager");
                            String com_branch_addr = branch.getString("com_branch_addr");
                            String com_branch_region = branch.getString("com_branch_region");
                            String com_branch_state = branch.getString("com_branch_state");
                            String com_branch_country = branch.getString("com_branch_country");
                            String com_branch_poscode = branch.getString("com_branch_poscode");
                            String com_branch_email = branch.getString("com_branch_email");
                            String com_branch_licence = branch.getString("com_branch_licence");
                            String com_branch_phoneNum = branch.getString("com_branch_phoneNum");
                            String com_branch_fax = branch.getString("datas.com_branch_fax");
                            String com_branch_fb = branch.getString("datas.com_branch_fb");
                            String com_branch_ig = branch.getString("datas.com_branch_ig");


                            jsonResponse = "";
                            jsonResponse += "Branch name: " + com_branch_id + "\n\n";
                            jsonResponse += "Branch manager: " + com_branch_comId + "\n\n";
                            jsonResponse += "Branch state: " + com_branch_name + "\n\n";
                            jsonResponse += "Branch state: " + com_branch_manager + "\n\n";
                            jsonResponse += "Branch state: " + com_branch_addr + "\n\n";
                            jsonResponse += "Branch state: " + com_branch_region + "\n\n";
                            jsonResponse += "Branch state: " + com_branch_state + "\n\n";
                            jsonResponse += "Branch state: " + com_branch_country + "\n\n";
                            jsonResponse += "Branch state: " + com_branch_poscode + "\n\n";
                            jsonResponse += "Branch state: " + com_branch_email + "\n\n";
                            jsonResponse += "Branch state: " + com_branch_licence + "\n\n";
                            jsonResponse += "Branch state: " + com_branch_phoneNum + "\n\n";
                            jsonResponse += "Branch state: " + com_branch_fax + "\n\n";
                            jsonResponse += "Branch state: " + com_branch_fb + "\n\n";
                            jsonResponse += "Branch state: " + com_branch_ig + "\n\n";

                        }

                        txtResponse.setText(jsonResponse);

                    } catch (JSONException e) {
                        e.printStackTrace();
                        Toast.makeText(getApplicationContext(),
                                "res" + e.getMessage(),
                                Toast.LENGTH_LONG).show();

                        txtResponse.setText(e.getMessage());
                    }

                    hidepDialog();
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "res" + error.getMessage());
            Toast.makeText(getApplicationContext(),
                    error.getMessage(), Toast.LENGTH_LONG).show();
            Log.d("onErrorResponse", error.getMessage());
            hidepDialog();

        }
    });

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(req);
}

我尝试遵循其他解决方案,但不幸的是我失败并卡住了。

【问题讨论】:

  • 如果您发布的 json 示例是对您请求的响应,则它不是 JSONArray!
  • 我理解的是 JSON Object --> { ""...""} 和 JSON Array --> [ , , , ]
  • 使用Gsonjsonschema2pojo我的朋友
  • 感谢您的回复。我很期待。

标签: java android arrays json android-volley


【解决方案1】:

试试这个..

 JSONObject json = new JSONObject(response);

    JSONArray jsonarray = json.getJSONArray("datas");

    for (int i = 0; i < jsonarray.length(); i++) {
         JSONObject c = jsonarray.getJSONObject(i);

         //here you get by json key
         String id = c.getString("com_branch_id");

    }

【讨论】:

    【解决方案2】:

    JsonArrayRequest 会给你回复JSONArray。但是您的回复不是JSONArray。它的JSONObjectJSONObject 包含JSONArray

    在您的情况下使用JsonObjectRequest

    您可以使用Gson 将您的响应转换为Java 对象。

    创建一个与您的响应格式匹配的 Java Pojo 类。比将您的响应转换为 java 对象。

            public void onResponse(JSONObject response) {
    
                String message = "";
                if (response != null) {
                    message = response.toString();
                    Post post = null;
                    Gson gson = new Gson();
                    post = gson.fromJson(response.toString(), Post.class);
    
                } else {
                    message = "Response is null";
    
                }
            }
    

    【讨论】:

    • 你能给我示例代码或链接来解决我的问题吗?
    【解决方案3】:

    1) 你的 Json 不是一个数组(它以 { 开头暗示一个对象 json)。使用 JsonObjectRequest。
    2)而不是抓住元素, 在您的回复中,通过迭代,您可以使用 jackson2 来映射 回复您的 POJO。
    3)您可以使用 jsonschema2pojo 创建一个 POJO类

    【讨论】:

      猜你喜欢
      • 2021-12-03
      • 2017-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多