首页 > 解决方案 > 任何人都可以为 JSONException 解决这个问题吗?我',

问题描述

我得到这个:E/fetchPosts(): org.json.JSONException: End of input at character 0 of

我正在尝试为“askreddit”点击 subreddit api,并尝试在我的应用程序中显示。不知道怎么了。我在清单中做了 cleartexttraffic true 并且还添加了互联网许可

我的 JSON 不正确吗

我正在“孩子”下尝试

我正在关注这个例子: http: //www.whycouch.com/2012/12/how-to-create-android-client-for-reddit.html

PostsHolder.java

package com.jimmytrivedi.jimmytrivedi_reddit;

import android.util.Log;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class PostsHolder {

    /**
     * We will be fetching JSON data from the API.
     */
    private final String URL_TEMPLATE =
            "http://www.reddit.com/r/SUBREDDIT_NAME/"
                    + ".json"
                    + "?after=AFTER";

    String subreddit;
    String url;
    String after;

    PostsHolder(String sr) {
        subreddit = sr;
        after = "";
        generateURL();
    }

    /**
     * Generates the actual URL from the template based on the
     * subreddit name and the 'after' property.
     */
    private void generateURL() {
        url = URL_TEMPLATE.replace("SUBREDDIT_NAME", subreddit);
        url = url.replace("AFTER", after);
    }

    /**
     * Returns a list of Post objects after fetching data from
     * Reddit using the JSON API.
     *
     * @return
     */
    List<Post> fetchPosts() {
        String raw = RemoteData.readContents(url);
        List<Post> list = new ArrayList<Post>();
        try {
            JSONObject data = new JSONObject(raw)
                    .getJSONObject("data");
            JSONArray children = data.getJSONArray("children");

            //Using this property we can fetch the next set of
            //posts from the same subreddit
            after = data.getString("after");

            for (int i = 0; i < children.length(); i++) {
                JSONObject cur = children.getJSONObject(i)
                        .getJSONObject("data");
                Post p = new Post();
                p.title = cur.optString("title");
                p.url = cur.optString("url");
                p.numComments = cur.optInt("num_comments");
                p.points = cur.optInt("score");
                p.author = cur.optString("author");
                p.subreddit = cur.optString("subreddit");
                p.permalink = cur.optString("permalink");
                p.domain = cur.optString("domain");
                p.id = cur.optString("id");
                if (p.title != null)
                    list.add(p);
            }
        } catch (Exception e) {
            Log.e("fetchPosts()", e.toString());
        }
        return list;
    }

    /**
     * This is to fetch the next set of posts
     * using the 'after' property
     *
     * @return
     */
    List<Post> fetchMorePosts() {
        generateURL();
        return fetchPosts();
    }
}

标签: javaandroidjson

解决方案


推荐阅读