Can't resolve JSONException Android Studio

I don't know what's going wrong with my JSONData , but my code catch JSONException , maybe it's invalid value ...? My url with json data is : this website . I already used Json with this website, and it works, but not this page with Json... Why ?

Here is my code :

public class Tab4Fragment extends Fragment {

    private ConnectivityManager manager;
    public static final String TAG = Tab4Fragment.class.getSimpleName();
    private CurrentEventsString mCurrentEventsString;
    @InjectView(R.id.eventsText)
    TextView mJsonText;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (container == null) {
            return null;
        }

        View view = inflater.inflate(R.layout.tab_frag4_layout, container, false);
        ButterKnife.inject(this, view);

        String urlWebSite = "http://www.bundoransurfco.com/events/?json=1";

        if (isNetworkAvailable()) {
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                    .url(urlWebSite)
                    .build();

            Call call = client.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Request request, IOException e) {
                    Log.d(TAG, "onFailure Network");
                }

                @Override
                public void onResponse(Response response) throws IOException {
                    try {
                        String jsonData = response.body().string();
                        Log.d(TAG, "onResponse jsonData : "+jsonData);

                        if (response.isSuccessful()) {
                            mCurrentEventsString = getCurrentDetails(jsonData);
                            Log.d(TAG, "onResponse Successful");

                            // Write in the main Thread.
                            getActivity().runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    updateTextEvents();
                                    Log.d(TAG, "UITHREAD");
                                }
                            });

                        } else {
                            alertUserAboutError();
                            Log.d(TAG, "ERROR");
                        }
                    } catch (IOException e) {
                        Log.e(TAG, "Exception caught: ", e);
                    } catch (JSONException e) {
                        Log.e(TAG, "Exception caught: ", e);
                    }
                }
            });
        } else {
            Toast.makeText(getActivity(), getString(R.string.network_unavailable_message), Toast.LENGTH_LONG).show();
        }

        Log.d(TAG, "Main UI code is running!");

        return view;
    }

    private void updateTextEvents() {
        mJsonText.setText(mCurrentEventsString.getJsonContent());
    }


    private boolean isNetworkAvailable() {
        manager = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();
        boolean isAvailable = false;
        if (networkInfo != null && networkInfo.isConnected()) {
        isAvailable = true;
        }

        return isAvailable;
        }

    private void alertUserAboutError() {
        AlertDialogFragment dialog = new AlertDialogFragment();
        dialog.show(getActivity().getFragmentManager(), "error_dialog");
    }

    private CurrentEventsString getCurrentDetails(String jsonData) throws JSONException {
        JSONObject events = new JSONObject(jsonData);

        JSONObject postJson = events.getJSONObject("posts");
        CurrentEventsString currentEventsString = new CurrentEventsString();
        String contentString = postJson.getString("content");

        //Events String
        String jsonText = stripHtml(contentString);
        Log.i(TAG, "CHECK THAT :" +jsonText);
        currentEventsString.setJsonContent(jsonText);


        return currentEventsString;
    }

    //Delete HTML Markup
    public String stripHtml(String html) {
        return Html.fromHtml(html).toString();
    } 
}

My code stop after String jsonData = response.body().string(); and go in Log.e(TAG, "Exception caught: ", e);

I would like to have the string from Posts > content

How can I solve this ? Cheers!

Logcat :

04-24 05:58:46.696    2140-2140/jardelcompany.bundoransurfco D/Tab4Fragment﹕ Main UI code is running!
04-24 05:58:47.228    2140-2174/jardelcompany.bundoransurfco D/Tab4Fragment﹕ onResponse jsonData : {"status":"ok","count":1,"pages":1,"category":{"id":6,"slug":"events","title":"Events","description":"","parent":0,"post_count":1},"posts":[{"id":6969,"type":"post","slug":"2014-calander-of-events","url":"http://www.bundoransurfco.com/events/2014-calander-of-events/","status":"publish","title":"2015 Calendar of Events","title_plain":"2015 Calendar of Events","content":"<p>We open all year and have all our regular<a href="http://www.bundoransurfco.com/surf-school/beginners/"> surf lessons </a>and also <a href="http://www.bundoransurfco.com/surf-stay/lodge-packages/">surf and stay packages available year round</a>.</p>n<p>We also have <a href="http://www.bundoransurfco.com/surf-school/surf-camp/">week long surf camps</a> as well as <a href="http://www.bundoransurfco.com/surf-school/family-surf-and-stay-packages/">family surf and stay packages </a>all year.u00a0Below is au00a0list of specificu00a0events. We are open for other bookings when the events below are on. We can be flexible with dates for group bookingsu00a0and arrange specific packages : just call on 071-9841968 or email info@bundoransurfco.com.</p>n<p></p>n<p><strong>February 2015</strong></p>n<p>7th-8th <a href="http://www.bundoransurfco.com/surf-school/improver-surf-weekends/">Improvers Surf Weekend with Surf Fit Ireland</a></p>n<p>13th-15th<a href="http://www.bundoransurfco.com/surf-school/surf-guiding/">u00a0Surf Guiding weekend.</a> Explore the coast with our instructors. For improver and intermediate surfers</p>n<p>20th-22ndu00a0<a href="http://www.bundoransurfco.com/blog/singles-weekend/">Single Surfers Valentines Surf Weekend</a>. Meet new people, catch a few waves, have the craic. Only u20ac79pps</p>n<p>20th-22nd <a href="http://www.bundoransurfco.com/blog/soul-surfers-yoga-weekend/">Surf and Yoga weekend</a></p>n<p></p>n<p><strong>March 2015</strong></p>n<p>7th-8thu00a0<a href="http://www.bundoransurfco.com/surf-school/improver-surf-weekends/">Improvers Surf Weekend with Surf Fit Ireland</a></p>n<p>13th-15th <a href="http://www.bundoransurfco.com/blog/get-ready-for-summer-surf-weekend/">Get Ready for Summer Surf Weekend</a>. For anyone who has not been surfing all winter and wants to get back in the water</p>n<p>20th-22ndu00a0<a href="http://www.bundoransurfco.com/surf-school/surf-guiding/">Surf Guiding weekend.</a> Explore the coast with our instructors. For improver and intermediate surfers</p>n<p>20th-22nd <a href="http://www.bundoransurfco.com/blog/soul-surfers-yoga-weekend/">Surf and Yoga weekend</a>.</p>n<p></p>n<p><strong>April 2015</strong></p>n<p>3rd-6th <a href="http://www.bundoransurfco.com/blog/easter-surf-packages/">Easter Surf Weekend</a></p>n<p>17th-19th <a href="http://www.bundoransurfco.com/blog/get-ready-for-summer-surf-weekend/">Get Ready for Summer Surf Weekend</a>. For anyone who has not been surfing all winter and wants to get back in the water</p>n<p>24th-26th <a href="http://www.bundoransurfco.com/blog/soul-surfers-yoga-weekend/">Surf and Yoga Weekend</a></p>n<p></p>n<p><strong>May 2015</strong></p>n<p>1st-4th <a href="http://www.bundoransurfco.com/blog/may-bank-holiday-surf-weekend-2/">May Bank Holiday Surf Weekend</a>.u00a0Surf, B&B, craic and BBQ</p>n<p>8th-1oth <a href="http://www.bundoransurfco.com/blog/sup-tours/">Surf and SUP weekend</a>. Try a combination of surfing in the waves and SUP lessons on flat water. Perfect for beginners and families.</p>n<p>15th-17th<a href="http://www.bundoransurfco.com/blog/get-ready-for-summer-surf-weekend/"> Get Ready for Summer Surf Weekend.</a> For anyone who has not been surfing all winter and wants to get back in the water</p>n<p>22nd-24thu00a0<a href="http://www.bundoransurfco.com/blog/soul-surfers-yoga-weekend/">Surf and Yoga Weekend</a></p>n<p>May 29th-June 1st June bank holiday surf weekend.u00a0Surf, B&B, c
04-24 05:58:47.232    2140-2174/jardelcompany.bundoransurfco E/Tab4Fragment﹕ Exception caught:
    org.json.JSONException: Value [{"custom_fields":{},"comment_count":0,"tags":[],"status":"publish","excerpt":"<p>Check out what&#8217;s happening at  Surf Co</p>n","comment_status":"closed","date":"2014-01-09 16:43:57","type":"post","url":"http://www.bundoransurfco.com/events/2014-calander-of-events/","modified":"2015-04-01 14:27:02","content":"<p>We open all year and have all our regular<a href="http://www.bundoransurfco.com/surf-school/beginners/"> surf lessons </a>and also <a href="http://www.bundoransurfco.com/surf-stay/lodge-packages/">surf and stay packages available year round</a>.</p>n<p>We also have <a href="http://www.bundoransurfco.com/surf-school/surf-camp/">week long surf camps</a> as well as <a href="http://www.bundoransurfco.com/surf-school/family-surf-and-stay-packages/">family surf and stay packages </a>all year. Below is a list of specific events. We are open for other bookings when the events below are on. We can be flexible with dates for group bookings and arrange specific packages : just call on 071-9841968 or email info@bundoransurfco.com.</p>n<p></p>n<p><strong>February 2015</strong></p>n<p>7th-8th <a href="http://www.bundoransurfco.com/surf-school/improver-surf-weekends/">Improvers Surf Weekend with Surf Fit Ireland</a></p>n<p>13th-15th<a href="http://www.bundoransurfco.com/surf-school/surf-guiding/"> Surf Guiding weekend.</a> Explore the coast with our instructors. For improver and intermediate surfers</p>n<p>20th-22nd <a href="http://www.bundoransurfco.com/blog/singles-weekend/">Single Surfers Valentines Surf Weekend</a>. Meet new people, catch a few waves, have the craic. Only €79pps</p>n<p>20th-22nd <a href="http://www.bundoransurfco.com/blog/soul-surfers-yoga-weekend/">Surf and Yoga weekend</a></p>n<p></p>n<p><strong>March 2015</strong></p>n<p>7th-8th <a href="http://www.bundoransurfco.com/surf-school/improver-surf-weekends/">Improvers Surf Weekend with Surf Fit Ireland</a></p>n<p>13th-15th <a href="http://www.bundoransurfco.com/blog/get-ready-for-summer-surf-weekend/">Get Ready for Summer Surf Weekend</a>. For anyone who has not been surfing all winter and wants to get back in the water</p>n<p>20th-22nd <a href="http://www.bundoransurfco.com/surf-school/surf-guiding/">Surf Guiding weekend.</a> Explore the coast with our instructors. For improver and intermediate surfers</p>n<p>20th-22nd <a href="http://www.bundoransurfco.com/blog/soul-surfers-yoga-weekend/">Surf and Yoga weekend</a>.</p>n<p></p>n<p><strong>April 2015</strong></p>n<p>3rd-6th <a href="http://www.bundoransurfco.com/blog/easter-surf-packages/">Easter Surf Weekend</a></p>n<p>17th-19th <a href="http://www.bundoransurfco.com/blog/get-ready-for-summer-surf-weekend/">Get Ready for Summer Surf Weekend</a>. For anyone who has not been surfing all winter and wants to get back in the water</p>n<p>24th-26th <a href="http://www.bundoransurfco.com/blog/soul-surfers-yoga-weekend/">Surf and Yoga Weekend</a></p>n<p></p>n<p><strong>May 2015</strong></p>n<p>1st-4th <a href="http://www.bundoransurfco.com/blog/may-bank-holiday-surf-weekend-2/">May Bank Holiday Surf Weekend</a>. Surf, B&B, craic and BBQ</p>n<p>8th-1oth <a href="http://www.bundoransurfco.com/blog/sup-tours/">Surf and SUP weekend</a>. Try a combination of surfing in the waves and SUP lessons on flat water. Perfect for beginners and families.</p>n<p>15th-17th<a href="http://www.bundoransurfco.com/blog/get-ready-for-summer-surf-weekend/"> Get Ready for Summer Surf Weekend.</a> For anyone who has not been surfing all winter and wants to get back in the water</p>n<p>22nd-24th <a href="http://www.bundoransurfco.com/blog/soul-surfers-yoga-weekend/">Surf and Yoga Weekend</a></p>n<p>May 29th-June 1st June bank holiday surf weekend. Surf, B&B, craic and BBQ</p>n<p></p>n<p><strong>June 2015</strong></p>n<p>5th-7

The "posts" attribute isn't a JSONObject, its a JSONArray.

Try changing this line in your getCurrentDetails method

JSONObject postJson = events.getJSONObject("posts");

to

JSONArray postJson = events. getJSONArray("posts");

You should do some proactive check for JSONObject or JSONArray.

You can do this by using following :

Object obj = new JSONTokener(stringData).nextValue();

if (obj instanceof JSONObject)
  //you got JSONobject here
else if (obj instanceof JSONArray)
  //you got JSONArray here

you can get data using jsonArray and jsonObject , hear you can see the example how you can use api into your application. Example, example 2

链接地址: http://www.djcxy.com/p/20118.html

上一篇: 如何解决,JSONException:没有值为“1”

下一篇: 无法解析JSONException Android Studio