我正在使用this wrapper开发一个android reddit应用。我能够登录,除了它解析时遇到了一些问题。这是代码:
package com.example.reddit;
import im.goel.jreddit.submissions.Submission;
import im.goel.jreddit.submissions.Submissions;
import im.goel.jreddit.user.User;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView username;
private TextView password;
private User reddit;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.password);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String usern = username.getText().toString();
String passw = password.getText().toString();
reddit = new User(usern, passw);
try {
reddit.connect();
for (Submission submission : Submissions.getSubmissions("cfb",
Submissions.Popularity.HOT, Submissions.Page.FRONTPAGE, reddit)) {
submission.upVote();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
LogCat在运行它并单击按钮时输出的消息是:
12-01 17:22:47.392: I/global(6955): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
12-01 17:22:48.452: I/System.out(6955): Response: {"kind": "Listing", "data": {"modhash": "42p97yhjc913d0d8fef0d29b8192d65e5a569cf9992512b5f1", "children": [{"kind": "t3", "data": {"domain": "self.CFB", "banned_by": null, "media_embed": {}, "subreddit": "CFB", "selftext_html": "<!-- SC_OFF --><div class=\"md\"><p>Post any pictures, videos, or gifs of highlights, players, coaches, stadiums, awesome plays, mascots, hot girls, etc, etc, as well as any requests of the above here.</p>\n</div><!-- SC_ON -->", "selftext": "Post any pictures, videos, or gifs of highlights, players, coaches, stadiums, awesome plays, mascots, hot girls, etc, etc, as well as any requests of the above here.", "likes": null, "secure_media": null, "link_flair_text": null, "id": "1rs14o", "secure_media_embed": {}, "clicked": false, "stickied": true, "author": "CFB_Referee", "media": null, "score": 186, "approved_by": null, "over_18": false, "hidden": false, "thumbnail": "self", "subreddit_id": "t5_2qm9d", "edited": false, "link_flair_css_class": null, "a
12-01 17:22:48.482: W/System.err(6955): Unexpected token END OF FILE at position 1024.
12-01 17:22:48.492: W/System.err(6955): at org.json.simple.parser.JSONParser.parse(JSONParser.java:257)
12-01 17:22:48.492: W/System.err(6955): at org.json.simple.parser.JSONParser.parse(JSONParser.java:81)
12-01 17:22:48.492: W/System.err(6955): at org.json.simple.parser.JSONParser.parse(JSONParser.java:75)
12-01 17:22:48.492: W/System.err(6955): at im.goel.jreddit.utils.Utils.get(Utils.java:116)
12-01 17:22:48.492: W/System.err(6955): at im.goel.jreddit.submissions.Submissions.getSubmissions(Submissions.java:62)
12-01 17:22:48.492: W/System.err(6955): at com.example.reddit.MainActivity$1.onClick(MainActivity.java:42)
12-01 17:22:48.492: W/System.err(6955): at android.view.View.performClick(View.java:2364)
12-01 17:22:48.492: W/System.err(6955): at android.view.View.onTouchEvent(View.java:4179)
12-01 17:22:48.492: W/System.err(6955): at android.widget.TextView.onTouchEvent(TextView.java:6661)
12-01 17:22:48.502: W/System.err(6955): at android.view.View.dispatchTouchEvent(View.java:3709)
12-01 17:22:48.502: W/System.err(6955): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
12-01 17:22:48.502: W/System.err(6955): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
12-01 17:22:48.502: W/System.err(6955): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
12-01 17:22:48.502: W/System.err(6955): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:924)
12-01 17:22:48.502: W/System.err(6955): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1695)
12-01 17:22:48.502: W/System.err(6955): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1116)
12-01 17:22:48.502: W/System.err(6955): at android.app.Activity.dispatchTouchEvent(Activity.java:2068)
12-01 17:22:48.502: W/System.err(6955): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1679)
12-01 17:22:48.502: W/System.err(6955): at android.view.ViewRoot.handleMessage(ViewRoot.java:1708)
12-01 17:22:48.512: W/System.err(6955): at android.os.Handler.dispatchMessage(Handler.java:99)
12-01 17:22:48.512: W/System.err(6955): at android.os.Looper.loop(Looper.java:123)
12-01 17:22:48.512: W/System.err(6955): at android.app.ActivityThread.main(ActivityThread.java:4595)
12-01 17:22:48.512: W/System.err(6955): at java.lang.reflect.Method.invokeNative(Native Method)
12-01 17:22:48.512: W/System.err(6955): at java.lang.reflect.Method.invoke(Method.java:521)
12-01 17:22:48.512: W/System.err(6955): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-01 17:22:48.512: W/System.err(6955): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-01 17:22:48.512: W/System.err(6955): at dalvik.system.NativeStart.main(Native Method)
显然,它正在从/ r / cfb的首页提交提交,但它只能读取1024个字节。错误是Unexpected token END OF FILE at position 1024
。任何人都知道这是什么或如何解决它?
答案 0 :(得分:0)
检索信息时缓冲区溢出。因此,我们不是获取并尝试存储reddit返回的所有数据,而是动态地解析它以获取所需信息并存储它。更高效并解决了缓冲区溢出错误。类似的东西:
JSONArray array = (JSONArray) ((JSONObject) object.get("data"))
.get("children");
JSONObject data;
for (int i = 0; i < array.size(); i++) {
data = (JSONObject) array.get(i);
}