java - 有没有人使用过这个 twitter api - http ://www. twapime.com/

标签 java blackberry java-me

有人试过这个 api 吗?我在 BlackBerry 上执行它时遇到了一些问题。推文并不总是发送,我无法访问已发布的推文。

这是我的代码 -

private void twitterSetup(){

        HttpRequest req = new HttpRequest("https://api.twitter.com/oauth/access_token");
        req.setMethod(HttpConnection.POST);
        XAuthSigner signer = new XAuthSigner("", "");
        signer.signForAccessToken(req, "", "");
        try {
        HttpResponse resp = req.send();
        if (resp.getCode() == HttpConnection.HTTP_OK)
        {
        Token accessToken = Token.parse(resp.getBodyContent());
        req.close();
        req = new HttpRequest("http://api.twitter.com/1/statuses/update.xml");
        req.setMethod(HttpConnection.POST);
        req.setBodyParameter("status", "new message");
        req.setSigner(signer, accessToken);
        resp = req.send();

        Tweet[] twts = null;
        try {

            Credential c = new Credential("","","","");
            UserAccountManager uam = UserAccountManager.getInstance(c);
         List[] lists = null;
            ListManager ter = null;
            if (uam.verifyCredential()) {
                 ter = ListManager.getInstance(uam); //pode ser pela classe Timeline tambem.

                ListManager listMngr = ListManager.getInstance(uam);
                lists = listMngr.getLists();
                }


            ter.startGetListTweets(lists[0], null, new SearchDeviceListener() {
            public void searchCompleted() {}
            public void searchFailed(Throwable cause) {}
            public void tweetFound(Tweet tweet) {
                System.out.println(tweet);
            }
            });

        }
        catch(Exception e){
            e.printStackTrace();
        }
        }
        else { }

        } catch (IOException e) {
        e.printStackTrace();

        }
        catch(Exception e){

        }finally {
        try {
        req.close();
        } catch (IOException e) {}
        } 

    }

感谢您的帮助。

最佳答案

好的,

这是我用来根据类型和标签获取 Twitter 内容的类。方法 getContent 不是最优雅的,但它可以工作。它只是下载并解析一个 json 文件。 看http://search.twitter.com/api/

public class GetTwitterContent implements Runnable {

    private String tag;
    private String type;

    public GetTwitterContent(String type, String tag) {
        this.type = type;
        this.tag = tag;
    }

    public void run() {

        try {

            Hashtable twitterValuesHashtable = new Hashtable();
            String serviceUrl = "";

            if (type.equalsIgnoreCase(Constants.TWITTER_CONTENT_TYPE_HASHTAG)) {
                serviceUrl = Constants.TWITTER_CONTENT_HASHTAG_CONTENT;
            } else if (type.equalsIgnoreCase(Constants.TWITTER_CONTENT_TYPE_USER)) {
                serviceUrl = Constants.TWITTER_CONTENT_USER_CONTENT;
            }

            ByteArrayOutputStream baos = getContent(serviceUrl + this.tag);
            JSONObject jsonObject = new JSONObject(new String(baos.toByteArray(), 0, baos.size(), "utf-8"));
            JSONArray jsonArray = jsonObject.getJSONArray("results");
            for (int counter = 0; counter < jsonArray.length(); ++counter) {
                JSONObject thisJsonObject = (JSONObject) jsonArray.get(counter);
                TwitterResponse twitterResponse = new TwitterResponse();
                twitterResponse.setCreatedAt(thisJsonObject.optString("created_at", "na"));
                twitterResponse.setTweetText(thisJsonObject.optString("text","na"));
                twitterResponse.setFromUser(thisJsonObject.optString("from_user", "na"));
                twitterValuesHashtable.put(new Integer(counter),twitterResponse);
            }
            ServerContent.future.addContent(Constants.TWITTER_KEY, twitterValuesHashtable);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private ByteArrayOutputStream getContent(String url) {

        ByteArrayOutputStream baos = null;
        // len = 0;
        try {

            javax.microedition.io.HttpConnection connection = (javax.microedition.io.HttpConnection) Connector
                    .open(url);
            connection.setRequestMethod(HttpConnection.GET);
            // connection.setRequestProperty("Connection", "close");
            java.io.InputStream inputStream = connection.openDataInputStream();

            // inputStream = getClass().getResourceAsStream(url);
            baos = new ByteArrayOutputStream();
            int c;
            while (true) {
                c = inputStream.read();
                if (c == -1)
                    break;
                // ++len;
                baos.write(c);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return baos;
    }

}

关于java - 有没有人使用过这个 twitter api - http ://www. twapime.com/,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5313696/

相关文章:

java-me - 为什么 Java 公共(public) API 有私有(private)方法?

java - 如何将本地spring boot项目添加为另一个spring boot项目的依赖

java - 我怎样才能到达处理程序内的框架标题

iphone - 用于开发移动应用程序并将其部署到 iphone、Android、Blackberry 等的通用 IDE

java - 我想使用 J2ME 在移动设备的后台运行应用程序,而无需在移动设备中使用任何 GUI?

android - J2ME/Android/BlackBerry - 行车路线,两个地点之间的路线

java - 获取 HashMap 中具有未知键的第一个值

java - Spring Boot 和 Gradle 多模块项目,未能正确加载依赖项

Blackberry 权限问题 - "Prompt"在不同设备上的行为不同

android - 在 phonegap 上播放 youtube 视频