无法解析 JSONException Android Studio

Can't resolve JSONException Android Studio

我不知道我的 JSONData 出了什么问题,但我的代码捕获了 JSONException,也许它是无效值... ?我的 url 和 json 数据是:this website。我已经在这个网站上使用了 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);

我想要来自 Posts > content

的字符串

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

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 a\u00a0list of specific\u00a0events. We are open for other bookings when the events below are on. We can be flexible with dates for group bookings\u00a0and 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-22nd\u00a0<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-8th\u00a0<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\u00a0<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-24th\u00a0<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

您可以使用 jsonArray 和 jsonObject 获取数据,听说您可以看到如何在您的应用程序中使用 api 的示例。 Example, example 2

"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