使用针对android

时间:2016-12-24 07:16:34

标签: android arrays android-volley

我试图从JsonArrayRequest收集字符串,但我还没有能够这样做。我收到错误如下

  

12-24 12:55:08.534 19626-19626 / com.domainzguru.butiko W / System.err:org.json.JSONException:肩膀无价值   12-24 12:55:08.537 19626-19626 / com.domainzguru.butiko W / System.err:at org.json.JSONObject.get(JSONObject.java:389)   12-24 12:55:08.537 19626-19626 / com.domainzguru.butiko W / System.err:at org.json.JSONObject.getString(JSONObject.java:550)   12-24 12:55:08.537 19626-19626 / com.domainzguru.butiko W / System.err:at com.domainzguru.butiko.Fragments.Orders.ItemsFragment $ 2 $ 1.onResponse(ItemsFragment.java:170)   12-24 12:55:08.537 19626-19626 / com.domainzguru.butiko W / System.err:at com.domainzguru.butiko.Fragments.Orders.ItemsFragment $ 2 $ 1.onResponse(ItemsFragment.java:163)   12-24 12:55:08.538 19626-19626 / com.domainzguru.butiko W / System.err:at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)   12-24 12:55:08.538 19626-19626 / com.domainzguru.butiko W / System.err:at com.android.volley.ExecutorDelivery $ ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)   12-24 12:55:08.538 19626-19626 / com.domainzguru.butiko W / System.err:at android.os.Handler.handleCallback(Handler.java:739)   12-24 12:55:08.538 19626-19626 / com.domainzguru.butiko W / System.err:at android.os.Handler.dispatchMessage(Handler.java:95)   12-24 12:55:08.538 19626-19626 / com.domainzguru.butiko W / System.err:at android.os.Looper.loop(Looper.java:148)   12-24 12:55:08.538 19626-19626 / com.domainzguru.butiko W / System.err:at android.app.ActivityThread.main(ActivityThread.java:5438)   12-24 12:55:08.538 19626-19626 / com.domainzguru.butiko W / System.err:at java.lang.reflect.Method.invoke(Native Method)   12-24 12:55:08.539 19626-19626 / com.domainzguru.butiko W / System.err:at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:739)   12-24 12:55:08.539 19626-19626 / com.domainzguru.butiko W / System.err:at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)

这是我的代码片段

JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST, ITEM_CUSTOMER_MEASUREMENTS, null,
                    new Response.Listener<JSONArray>() {
                        @Override
                        public void onResponse(JSONArray response) {

                            try {
                                JSONObject jsonobject = new JSONObject();

                                String shoulder = jsonobject.getString("shoulder");
                                et_shoulder.setText(shoulder);
                                String arm_hole = jsonobject.getString("arm_hole");
                                et_arm_hole.setText(arm_hole);

                                String chest_1 = jsonobject.getString("chest_1");
                                et_chest_1.setText(chest_1);

                                String chest_2 = jsonobject.getString("chest_2");
                                et_chest_2.setText(chest_2);

                                String waist = jsonobject.getString("waist");
                                et_waist.setText(waist);

                                String hip = jsonobject.getString("hip");
                                et_hip.setText(hip);

                                String slit = jsonobject.getString("slit");
                                et_slit.setText(slit);

                                String top_length = jsonobject.getString("top_length");
                                et_top_length.setText(top_length);

                                String f_neck = jsonobject.getString("f_neck");
                                et_f_neck.setText(f_neck);

                                String b_neck = jsonobject.getString("b_neck");
                                et_b_neck.setText(b_neck);

                                String sleeve_length = jsonobject.getString("sleeve_length");
                                et_sleeve_length.setText(sleeve_length);

                                String sleeve_breadth = jsonobject.getString("sleeve_breadth");
                                et_sleeve_breadth.setText(sleeve_breadth);

                                String sleeve_type = jsonobject.getString("sleeve_type");
                                et_sleeve_type.setText(sleeve_type);

                                String hip_size = jsonobject.getString("hip_size");
                                et_hip_size.setText(hip_size);

                                String ankle = jsonobject.getString("ankle");
                                et_ankle.setText(ankle);

                                String bottom_length = jsonobject.getString("bottom_length");
                                et_bottom_length.setText(bottom_length);

                                String knee = jsonobject.getString("knee");
                                et_knee.setText(knee);

                                String bottom_breadth = jsonobject.getString("bottom_breadth");
                                et_bottom_breadth.setText(bottom_breadth);

                                String thigh = jsonobject.getString("thigh");
                                et_thigh.setText(thigh);


                                Log.d("response", "onResponse: " +shoulder +arm_hole +chest_1 +chest_2 +hip +slit +top_length +f_neck +b_neck +sleeve_length
                                        +sleeve_breadth +sleeve_type +hip_size +ankle +bottom_length +knee +bottom_breadth +thigh);


                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                        }
                    }){
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    Map<String,String> headers = new HashMap<>();
                    headers.put("Authorization", api_token);
                    headers.put("X-Request-ID", cust_id);
                    headers.put("X-Requested-With", item_type);

                    Log.d("heade values", "getHeaders: " +api_token+" "+cust_id+" "+item_type);
                    return headers;
                }
            };

            //Creating request queue
            RequestQueue requestQueue = Volley.newRequestQueue(ItemsFragment.this.getActivity());

            //Adding request to the queue
            requestQueue.add(jsonArrayRequest);

任何人都能告诉我代码有什么问题。这就是服务器响应的样子

  

[     {       &#34; id&#34;:3,       &#34; order_id&#34;:3,       &#34; employee_id&#34;:1,       &#34; item_type&#34;:8,       &#34;肩膀&#34;:&#34;&#34;,       &#34; arm_hole&#34;:&#34;&#34;,       &#34; chest_1&#34;:&#34;&#34;,       &#34; chest_2&#34;:&#34;&#34;,       &#34;腰围&#34;:&#34;&#34;,       &#34; hip&#34;:&#34;&#34;,       &#34; slit&#34;:&#34;&#34;,       &#34; top_length&#34;:&#34;&#34;,       &#34; f_neck&#34;:&#34;&#34;,       &#34; b_neck&#34;:&#34;&#34;,       &#34; sleeve_length&#34;:&#34;&#34;,       &#34; sleeve_breadth&#34;:&#34;&#34;,       &#34; sleeve_type&#34;:&#34;&#34;,       &#34; hip_size&#34;:&#34; 38&#34;,       &#34;脚踝&#34;:&#34;&#34;,       &#34; bottom_length&#34;:&#34; 70&#34;,       &#34;膝盖&#34;:&#34;&#34;,       &#34; bottom_breadth&#34;:&#34; 60&#34;,       &#34;大腿&#34;:&#34;&#34;,       &#34;说明&#34;:&#34;测试&#34;,       &#34;金额&#34;:5000,       &#34; created_at&#34;:&#34; 2016-12-22 06:38:01&#34;,       &#34; updated_at&#34;:&#34; 2016-12-22 06:38:01&#34;,       &#34; customer_id&#34;:1,       &#34; item_id&#34;:3     }   ]

1 个答案:

答案 0 :(得分:0)

更改此部分:

    public void onResponse(JSONArray response) {

        try {
            JSONArray jsonobect = new JSONArray();

JSONObject jsonobject = response.getJSONObject(0);