我正在尝试根据用户输入字符串 推文和解析推文文本,从推文中提取网络链接(忽略没有网络链接的推文)并解析它以在下一个按钮的WebView对话框中显示(因此当单击以移动到下一个WebView(显示从推文解析的链接的下一个内容))。我得到推文结果(没有过滤),我如何过滤它们并获取URL,以便我可以为WebView解析它?
以下是我在后台线程上运行的代码:
@Override
protected WebView doInBackground(String... strings) {
//Get userInput
String searchTerm = strings[0];
//Initialize Twitter Connection
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setDebugEnabled(true);
builder.setOAuthConsumerKey(sharedPreferences.getString(TwitterConstants.getPrefConsumerKey(), ""));
builder.setOAuthConsumerSecret(sharedPreferences.getString(TwitterConstants.getPrefConsumerSecret(), ""));
AccessToken accessToken = new AccessToken(sharedPreferences.getString(TwitterConstants.getPrefAccessToken(), ""), sharedPreferences.getString(TwitterConstants.getPrefAccessTokenSecret(), ""));
Twitter twitter = new TwitterFactory(builder.build()).getInstance(accessToken);
try {
//Query object
Query query = new Query(searchTerm);
//The number of tweets to return per page [max:100]
query.count(2);
//Return only the most recent results in the response
query.resultType(Query.ResultType.recent);
QueryResult result = twitter.search(query);
Log.e("Twitter QueryResult", result.toString());
}catch(TwitterException e){
e.printStackTrace();
}
return null;
}
答案 0 :(得分:1)
好的,如果有人遇到同样的问题,答案就是答案:
@Override
protected WebView doInBackground(String... strings) {
//Get userInput
String searchTerm = strings[0];
//Initialize Twitter Connection
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setDebugEnabled(true);
builder.setOAuthConsumerKey(sharedPreferences.getString(TwitterConstants.getPrefConsumerKey(), ""));
builder.setOAuthConsumerSecret(sharedPreferences.getString(TwitterConstants.getPrefConsumerSecret(), ""));
AccessToken accessToken = new AccessToken(sharedPreferences.getString(TwitterConstants.getPrefAccessToken(), ""), sharedPreferences.getString(TwitterConstants.getPrefAccessTokenSecret(), ""));
//Gets Twitter instance with default credentials
Twitter twitter = new TwitterFactory(builder.build()).getInstance(accessToken);
try {
//Query object
Query query = new Query(searchTerm);
//The number of tweets to return per page [max:100]
query.count(2);
//Return only the most recent results in the response
query.resultType(Query.ResultType.recent);
//Get all results
QueryResult result = twitter.search(query);
//Fetch only tweets from the results
List<twitter4j.Status> tweets = result.getTweets();
for(twitter4j.Status tweet : tweets){
//Get single tweet
String singleTweet = tweet.getText();
Log.e("Twitter Tweet", singleTweet);
//Get URLs (it returns an array of Urls)
URLEntity[] urlEntity = tweet.getURLEntities();
//If the UrlEntity array is not empty
if(urlEntity != null){
for(int i=0; i<urlEntity.length; i++){
//Get URL from the tweet
String tweetUrl = urlEntity[i].getURL();
Log.e("Tweet URL", tweetUrl);
}
}else{
return null;
}
}
}catch(TwitterException e){
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(WebView webView) {
progressDialog.hide();
}
}
答案 1 :(得分:0)
如果您使用的是twitter4j库,则可以通过调用URLEntity
从twitter4j.Status
获取status.getURLEntities();
类。这将返回URLEntity
的数组。所以,你去,你可以检查推文里面是否有urlEntity,你可以通过调用urlEntity.getURL();
来获取推文中提到的网址
修改强> 对于包含代码的答案,请看下面的@hrskrs的答案。