java - 任何人都可以为 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();
}
}
解决方案
推荐阅读
- python - 根据 iat 值填充数据框中的列
- julia - 在 julia 中使用 vcat 时避免内存分配
- javascript - 在 ReactJS 中使用 jQuery 是一种不好的做法吗?
- rpy2 - rpy2 glmnet 获取具有行名的系数
- java - 在 IntelliJ 中使用 Code Iris 创建图形会为一个项目引发 NullPointerException,但不会为另一个项目引发 NullPointerException
- java - 如何在junit5中自定义测试并行的逻辑
- azure - 加载缓存时如何对 Azure 云服务 WebRoles 进行负载平衡
- build - 发布时如何定位特定的 .NET Core 运行时?
- kotlin - CollectionsKt.removeAll 是做什么的?
- javascript - 如何指定 Web MIDI 通道?