无法解析JSONException Android Studio

我不知道我的JSONData出了什么问题,但是我的代码捕获了JSONException ,可能它的值是无效的......? 我与JSON数据的网址是:本网站。 我已经在这个网站上使用过Json ,它可以工作,但是Json没有这个页面...为什么?

这是我的代码:

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();
    } 
}

我的代码停止后String jsonData = response.body().string(); 并进入Log.e(TAG, "Exception caught: ", e);

我想要从帖子 > 内容的字符串

我该如何解决这个问题? 干杯!

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

“posts”属性不是JSONObject,它是一个JSONArray。

尝试在getCurrentDetails方法中更改此行

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

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

您应该对JSONObject或JSONArray进行一些主动检查。

您可以使用以下方法来完成此操作:

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

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

你可以使用jsonArray和jsonObject获取数据,听说你可以看到如何使用api到你的应用程序的例子。 例子,例子2

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

上一篇: Can't resolve JSONException Android Studio

下一篇: Java Security: Illegal key size or default parameters?