【发布时间】:2015-06-16 11:29:00
【问题描述】:
我目前正在使用 twitter 结构框架并尝试检索我自己的帐户推文列表。我尝试在线搜索无济于事。文档中的示例展示了如何根据 tweetID 显示推文。我不要那个。我确实了解 REST 客户端与提供的变量建立 http 连接,并将 JSON 结果检索回解析等。
这是我当前的代码,成功登录后它将显示另一个活动,我希望在此处显示我的推文。
public void success(Result<TwitterSession> result) {
// Do something with result, which provides a TwitterSession for making API calls
TwitterSession session = Twitter.getSessionManager().getActiveSession();
TwitterAuthToken authToken = session.getAuthToken();
token = authToken.token;
secret = authToken.secret;
Log.i("token",token);
Log.i("secret",secret);
successNewPage();
}
我还使用意图将令牌和密钥传递给下一个活动
public void successNewPage(){
Intent intent = new Intent(this, LoginSuccess.class);
intent.putExtra("token",token);
intent.putExtra("secret", secret);
startActivity(intent);
}
在新的活动课上,我按照他们的文档提出了这个,
TwitterAuthConfig authConfig = new TwitterAuthConfig("consumerKey", "consumerSecret");
Fabric.with(this, new TwitterCore(authConfig), new TweetUi());
TwitterCore.getInstance().logInGuest(new Callback() {
public void success(Result appSessionResult) {
//Do the rest API HERE
Bundle extras = getIntent().getExtras();
String bearerToken = extras.getString("token");
try {
fetchTimelineTweet(bearerToken);
} catch (IOException e) {
e.printStackTrace();
}
}
public void failure(TwitterException e) {
Toast.makeText(getApplicationContext(), "Failure =)",
Toast.LENGTH_LONG).show();
}
});
}
推文的检索将是:
// Fetches the first tweet from a given user's timeline
private static String fetchTimelineTweet(String endPointUrl)
throws IOException {
HttpsURLConnection connection = null;
try {
URL url = new URL(endPointUrl);
connection = (HttpsURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("GET");
connection.setRequestProperty("Host", "api.twitter.com");
connection.setRequestProperty("User-Agent", "anyApplication");
connection.setRequestProperty("Authorization", "Bearer " + endPointUrl);
connection.setUseCaches(false);
String res = readResponse(connection);
Log.i("Response", res);
return new String();
} catch (MalformedURLException e) {
throw new IOException("Invalid endpoint URL specified.", e);
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
我在日志中得到的是:
380-380/com.example.john.fabric W/System.err: java.io.IOException: 指定的端点 URL 无效。
我的网址错了吗?还是我在 endpointURL 中设置的令牌也错误? 任何建议将不胜感激。谢谢!
【问题讨论】:
标签: android twitter twitter-fabric