标签: twitter4j

将Twitter4j状态转换为JSON对象

Android 2.3.3

我正在将Twitter API集成到我的Android应用程序中.我正在使用Twitter4j.

我可以使用以下代码获取特定用户的推文.

Paging paging = new Paging(1, 40);
List<twitter4j.Status> statuses = twitter.getUserTimeline("xxx", paging);

Log.d(Const.TAG,"Showing Timeline.");
for (twitter4j.Status status : statuses) {
    String rawJSON = DataObjectFactory.getRawJSON(status);
}
Run Code Online (Sandbox Code Playgroud)

我的要求是我需要解析JSON字符串(rawJSON),以获得一些其他值,如发布日期等.

我是JSON解析esp的新手,将事物转换为JSON.我在上面的代码中所知道的是,String rawJSON = DataObjectFactory.getRawJSON(status);转换Status Object为a String,我可以通过记录来查看它.

如何将其转换String为a JSONObject或a JSONArray?有人可以提供示例代码吗?

我尝试了下面的代码,但它给出了一个例外

try {
    JSONObject tweet = new JSONObject(rawJSON);
    JSONArray textArray = tweet.getJSONArray("text");
    String text = textArray.getString(0);

    Log.d(Const.TAG, "Text = "+text);

} catch (JSONException e) {
    // TODO Auto-generated catch block
    Log.d(Const.TAG, "Error …
Run Code Online (Sandbox Code Playgroud)

android json twitter4j

2
推荐指数
1
解决办法
5458
查看次数

有没有办法绕过Twitter的速率限制?也许定价计划?

我正在开发一个项目,需要从Twitter获取推文和用户信息.我甚至无法测试当前系统,因为我一直在达到Twitter速率限制.它有什么办法吗?

我希望从每个状态中提取的基本信息是:

  • 状态文本
  • 用户关注者数量
  • 用户关注计数
  • 转推计数
  • 地理位置协调

我正在使用Twitter4J API来做到这一点.任何帮助将不胜感激.提前致谢.
编辑
我正在使用twitter的搜索API来获取推文列表.

twitter twitter4j

2
推荐指数
1
解决办法
4480
查看次数

Twitter4j收听特定用户的推文

我想在特定用户推特的时候执行某些操作.那么在twitter4j中是否可以收听来自特定用户帐户的推文并处理该事件?

我知道答案是什么:阅读流式api.但我认为它对我来说太大了,我只想听一个特定账号的推文.我在这里问它.

java twitter4j twitter-streaming-api

2
推荐指数
1
解决办法
2522
查看次数

使用Java查找坐标集的中心

我正在使用Twitter4J从他/她的个人资料中检索用户的位置.我有一组地理坐标使用此调用形成多边形边界(通常为4个或更多坐标):

// Status tweet
Place place = tweet.getPlace();
GeoLocation[][] box = place.getBoundingBoxCoordinates();
Run Code Online (Sandbox Code Playgroud)

有没有办法计算这个区域/多边形/边界的中心(或近中心,或至少包含一个点)?是否有Java API?

是否有来自此帖子的 Java代码的Java等价物:

var bounds = new google.maps.LatLngBounds();
bounds.extend(results[0].geometry.location);
var center = bounds.getCenter();
Run Code Online (Sandbox Code Playgroud)

java geocoding twitter4j

2
推荐指数
2
解决办法
2342
查看次数

实时收听推文

我想实时收听推文,这意味着当有人发推文时我想看到该推文。

不过,我可以使用 twitter4j 库从我的新闻提要中获取推文。这是代码。

 package twitteroperation;

import java.util.List;
import twitter4j.Status;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder;

public class TwitterOperation {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws TwitterException {


          ConfigurationBuilder cb = new ConfigurationBuilder();

           cb.setDebugEnabled(true)
                .setOAuthConsumerKey("")
                .setOAuthConsumerSecret("")
                .setOAuthAccessToken("")
                .setOAuthAccessTokenSecret("");

     TwitterFactory tf=new TwitterFactory(cb.build());  

     twitter4j.Twitter tw=tf.getInstance();

      List<Status> statuses=  tw.getHomeTimeline();

         for (Status status1 : statuses) {
            System.out.println(status1.getUser().getName() + ":" + status1.getText());

        }   
    }
}
Run Code Online (Sandbox Code Playgroud)

我发现我必须使用 Streaming API 来实时访问推文。但我在 java 中找不到任何用于访问实时推文的示例代码。

java twitter twitter4j twitter-oauth

2
推荐指数
1
解决办法
2584
查看次数

如何使用twitter4j获取媒体相关信息

我是twitter4j的新手.我如何使用twitter4j库获取媒体相关信息?我想获取有关tweet中的照片,视频和文章摘要的信息.提前谢谢.为了这个目的,getMediaEntities()方法是否足够?

java twitter4j

1
推荐指数
1
解决办法
4077
查看次数

在twitter android app中获取访问令牌时出错

我在android应用程序中使用twitter4j.我正在使用我的消费者密钥和秘密的代码,当我登录到Twitter时,它首次尝试成功登录.但是当我再次运行代码然后尝试时,因为我之前已经过身份验证,它会让我登录但是会给出"获取访问令牌的错误".我不明白如何处理它..任何人都可以帮助..我正在使用此代码登录

    public class TwitterLoginActivity extends Activity {
        TextView tv;
        Button loginBtn;

        private TwitterApp mTwitter;
        private ConnectionDetector cd;
        ProgressDialog pDialog;
        private static SharedPreferences mSharedPreferences;
     AlertDialogManager alert = new AlertDialogManager();
        RequestToken requestToken;
        private static final String CONSUMER_KEY = "*****************";
        private static final String CONSUMER_SECRET = "*****************************************";

        static String PREFERENCE_NAME = "twitter_oauth";
      static final String PREF_KEY_OAUTH_TOKEN = "oauth_token";
      static final String PREF_KEY_OAUTH_SECRET = "oauth_token_secret";
      static final String PREF_KEY_TWITTER_LOGIN = "isTwitterLogedIn";


      static final String URL_TWITTER_AUTH = "auth_url";
      static final String URL_TWITTER_OAUTH_VERIFIER = "oauth_verifier";
      static …
Run Code Online (Sandbox Code Playgroud)

android twitter4j

1
推荐指数
1
解决办法
2470
查看次数

@ConversationScope bean不保留引用值

我正在尝试实现Twitter登录过程.在登录过程中,用户需要被重定向到Twitter网站以输入他/她的凭证,然后他/她将被重定向到我的网站URL.在第一次重定向之前,应该在请求之间存储和保留RequestToken对象(Twitter4J库)的实例.

为此,我决定使用ConverstaionScoped bean,但遗憾的是,请求之间不保留引用的值.

这是我的JSF页面:

twitter.xhtml:

<html xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      >

    <f:event listener="#{twitterLoginPageController.login()}" type="preRenderView" />

</html>
Run Code Online (Sandbox Code Playgroud)

twitterRedirect.xhtml:

<html xmlns="http://www.w3.org/1999/xhtml"   
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      >

    <f:event listener="#{twitterLoginPageController.onRedirect(param.oauth_token, param.oauth_verifier)}" type="preRenderView" />

</html>
Run Code Online (Sandbox Code Playgroud)

我的Twitter登录控制器:

@Named
@ConversationScoped
public class TwitterLoginPageController implements Serializable {


    @Inject
    private Conversation conversation;

    // These objects should be retained between requests
    Twitter twitter;
    RequestToken requestToken;

    public void login() {

        try {
            twitter = new TwitterFactory().getInstance();
            requestToken = twitter.getOAuthRequestToken("...");


            conversation.begin();
            conversation.setTimeout(30000);
            // Navigate to Twitter here
        } catch (TwitterException ex) {
             ...
        }

    } …
Run Code Online (Sandbox Code Playgroud)

ejb cdi twitter4j jsf-2 conversation-scope

1
推荐指数
1
解决办法
320
查看次数

Twitter4j发布用户的受保护数据

我正在使用twitter java api tweet4j来从twitter用户检索数据.我有一个用户ID列表,我想从中检索数据.我读了一个字符串ID列表,我试图通过twitter java api将数据提取到我的数据库中.但是,某些用户对其数据有保护.因此,我在某些ID中遇到身份验证错误.由于我的列表非常庞大,如何在没有身份验证的情况下自动创建用户异常?twitter4j中有没有针对受保护或受保护用户的方法?

我得到的错误:

Exception in thread "main" 401:Authentication credentials 
(https://dev.twitter.com/pages/auth) were missing or incorrect. Ensure that you have 
set valid consumer key/secret, access token/secret, and the system clock is in sync.
[Wed Apr 30 15:35:18 EEST 2014]date: Wed, 30 Apr 2014 12:36:58 GMT
[Wed Apr 30 15:35:18 EEST 2014]x-transaction: 0d6f0c50c647c52e 
[Wed Apr 30 15:35:18 EEST 2014]pragma: no-cache
[Wed Apr 30 15:35:18 EEST 2014]cache-control: no-cache, no-store, must-revalidate, pre-
check=0, post-check=0
{"request":"\/1.1\/statuses\/user_timeline.json?
user_id=787323013&include_my_retweet=true&include_entities=true","error":"Not 
authorized."}

[Wed Apr 30 15:35:18 …
Run Code Online (Sandbox Code Playgroud)

java twitter twitter4j

1
推荐指数
1
解决办法
709
查看次数

无论我尝试什么,JPanel都不会更新

我制作了一个小程序,显示团队/玩家/然后他们的推特,推文等.我的问题似乎与此无关,但它只是我的jPanel没有更新.我在项目中走得很远,并且我继续在我的方法中抛出随机代码段,以便一旦我点击某些东西,"左"jpanel要么被jbuttons替换,要么jbuttons被放入其中,我已经尝试过.下面是代码段,我一直试图开始工作,它确实如此,只是没有达到目的.

public void mousePressed(MouseEvent e) {        
    System.out.println("You clicked on " + ap.getTeams()  [addPlayers.OPTIC].getTeamName());
        for(int i = 0; i<4; i++){
            //JPanel temp = ap.makePanel(ap.getTeams()[ap.OPTIC].getPlayers().get(i).getTwitterScreenName());
            //temp.setBounds(0,(i*125),   450,125);
            //left.add(temp);

            JButton b = new JButton("Test");
            b.setBounds(30,30,30,30);
            left.add(b);
            left.revalidate();
            add(left);
            add(b);
            right.add(b);
            left.setVisible(false);
            left.setVisible(true);

            System.out.println(i);
        }
    }   
}
Run Code Online (Sandbox Code Playgroud)

注释掉的代码是我的主要代码,但我只是使用jbuttons进行测试.正如你所看到的,我只是不断添加随机位,希望能有效.我也尝试过稍后调用,但遗憾的是,这也没有用.不确定为什么jpanel没有更新,但任何反馈都会很棒,谢谢.

java swing jpanel jframe twitter4j

1
推荐指数
1
解决办法
197
查看次数