首页 > 解决方案 > Volley 错误:org.json.JSONException:在字符 0 处输入结束,代码:null

问题描述

这是我对 JSON DATA 的回应:

{
    "markers": [
        {
            "error": false,
            "title": "name",
            "lat": "17.415751",
            "lng": "78.443277",
            "video": "vlc-record-2020-02-14-16h29m53s-GlintandGlowJewellerypresentation.mp4",
            "image": "51457.jpg",
            "address": "Hyderabad,
            "phone": "4023384555",
            "name": "name"
        }
    ]
}

这是我第一次制作解析 JSON 数据的安卓应用程序。但是,我遇到了关于 JSON 解析的错误。我试过用谷歌搜索这个问题,但大多数解决方案都对我不起作用。

我的请求看起来像这样:

RequestQueue req = Volley.newRequestQueue(getApplicationContext());

    JSONObject jsonObject = null;


    JsonObjectRequest strReq = new JsonObjectRequest(Request.Method.GET,
            url,null, new Response.Listener<JSONObject>() {
        //http://justasec.in/JustASec/jas.php
        @Override
        public void onResponse(JSONObject response) {
            Log.e(TAG, "response: " + response);

            try {


                JSONObject obj = new JSONObject(response.toString());

                JSONArray commentsObj = obj.getJSONArray("markers");

                for (int i = 0; i < commentsObj.length(); i++) {

                    JSONObject commentObj = (JSONObject) commentsObj.get(i);
                    String title = commentObj.getString("title");
                    String address = commentObj.getString("address");
                    String phone = commentObj.getString("phone");

                    // String contact_person = commentObj.getString("contact_person");
                    lat = commentObj.getString("lat");
                    lng = commentObj.getString("lng");
                    /*Image = "http://amazearapp.com/jvdweb/images/medium/"+ commentObj.getString("images");

                    VIDEO_SAMPLE ="http://amazearapp.com/jvdweb/videos/" + commentObj.getString("video");
                    initializePlayer();

*/ mapView.getMapAsync(DataActivity.this);

                    Title.setText(title);
                    //Address.setText(address);
                  /*  if (contact_person != null && !contact_person.isEmpty()) {
                        ContactPerson.setText(contact_person);
                    } else {
                        ContactPerson.setText("NA");
                        ContactPerson.setVisibility(View.GONE);
                    }*/


                    if (phone != null && !phone.isEmpty()) {
                        Number = phone;
                    } else {
                        Number = "NA";
                        call.setVisibility(View.GONE);
                    }

                    sliderImg = new ArrayList<>();


                    DiscountModel model = new DiscountModel();
                    model.setName("Image");
                    model.setImage(commentObj.getString("image"));
                    sliderImg.add(model);



                    if (commentObj.getString("video") != null) {
                        DiscountModel model1 = new DiscountModel();
                        model1.setName("Video");
                        model1.setImage(commentObj.getString("video"));
                        sliderImg.add(model1);
                    }

                    Log.v("size", sliderImg.size() + " ");

                    viewPagerAdapter = new ViewPagerAdapter(sliderImg,DataActivity.this);
                    viewPager.setAdapter(viewPagerAdapter);

                }


            } catch (JSONException e) {
                Log.e(TAG, "json parsing error: " + e.getMessage());
                //  Toast.makeText(getApplicationContext(), "json parse error: " + e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            NetworkResponse networkResponse = error.networkResponse;
            Log.e(TAG, "Volley error: " + error.getMessage() + ", code: " + networkResponse);
            //  Toast.makeText(getApplicationContext(), "Server down", Toast.LENGTH_SHORT).show();
        }
    }){

标签: androidandroid-volley

解决方案


推荐阅读