java - 如何使用 TwitterObjectFactory.jsonStoreEnabled 获取 JSON 格式的推文?

标签 java json xml twitter twitter4j

seem使用deprecated API:

thufir@dur:~/NetBeansProjects/twitterBaseX$ 
thufir@dur:~/NetBeansProjects/twitterBaseX$ gradle clean run

> Task :compileJava
Note: /home/thufir/NetBeansProjects/twitterBaseX/src/main/java/twitterBaseX/App.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

BUILD SUCCESSFUL in 2s                                                            
4 actionable tasks: 4 executed
<-------------> 0% WAITING                                                        
> IDLE                                                                            
thufir@dur:~/NetBeansProjects/twitterBaseX$ 

代码:

package twitterBaseX;

import java.io.IOException;
import java.util.Properties;
import java.util.Set;
import java.util.logging.Logger;
import twitter4j.JSONException;
import twitter4j.JSONObject;
import twitter4j.Query;
import twitter4j.QueryResult;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder;
import twitter4j.json.DataObjectFactory;

public class App {

    private static final Logger log = Logger.getLogger(App.class.getName());

    private Properties loadProperties(String filePrefix) throws IOException {
        Properties properties = new Properties();
        properties.loadFromXML(App.class.getResourceAsStream("/" + filePrefix + ".xml"));

        Set<Object> keySet = properties.keySet();
        String key = null;
        String value = null;

        for (Object obj : keySet) {
            key = obj.toString();
            value = System.getenv(key);
            properties.setProperty(key, value);
        }
        return properties;
    }

    private TwitterFactory configTwitterFactory(Properties properties) throws IOException {
        ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();

        configurationBuilder.setDebugEnabled(true)
                .setJSONStoreEnabled(true)
                .setOAuthConsumerKey(properties.getProperty("oAuthConsumerKey"))
                .setOAuthConsumerSecret(properties.getProperty("oAuthConsumerSecret"))
                .setOAuthAccessToken(properties.getProperty("oAuthAccessToken"))
                .setOAuthAccessTokenSecret(properties.getProperty("oAuthAccessTokenSecret"));

        return new TwitterFactory(configurationBuilder.build());
    }

    private void twitterQueryResult() throws TwitterException, IOException, JSONException {
        Twitter twitter = configTwitterFactory(loadProperties("twitter")).getInstance();
        Query query = new Query("lizardbill");
        QueryResult result = twitter.search(query);
        String string = null;
        JSONObject JSON_complete = null;
        for (Status status : result.getTweets()) {
            insertStatus(status);
        }
    }


    /*

      // Content of the new document
      String doc = "<xml>Hello World!</xml>";

      System.out.println("\n* Create new resource.");

      // Create a new XML resource with the specified ID
      XMLResource res = (XMLResource) col.createResource(id,
          XMLResource.RESOURCE_TYPE);

      // Set the content of the XML resource as the document
      res.setContent(doc);

      System.out.println("\n* Store new resource.");

      // Store the resource into the database
      col.storeResource(res);
     */
    private void insertStatus(Status status) throws JSONException {
        String string = DataObjectFactory.getRawJSON(status);
        JSONObject json = new JSONObject(string);
        String language = json.getString("lang");
        log.fine(json.toString());
        //    XMLResource res = (XMLResource) col.createResource(id, XMLResource.RESOURCE_TYPE);

    }

    public static void main(String[] args) throws TwitterException, IOException, JSONException {
        new App().twitterQueryResult();
    }
}

真的,更多地寻找xml - 但jsonsuffice .

最佳答案

这似乎不使用已弃用的方法:

private void insertStatus(Status status) throws JSONException {
    String string = TwitterObjectFactory.getRawJSON(status);
    JSONObject json = new JSONObject(string);
    String language = json.getString("lang");
    log.info(language);
    //    XMLResource res = (XMLResource) col.createResource(id, XMLResource.RESOURCE_TYPE);
}

关于java - 如何使用 TwitterObjectFactory.jsonStoreEnabled 获取 JSON 格式的推文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59982929/

相关文章:

javascript - 如何在 React Native 中访问 json 对象的字符串

c# - 如何使用 Linq to XML 将 HTML 保存在 XML 文件中?

php - 从 SQL 中检索数据以显示为 XML

java - Byte.decode ("10") 和 Byte.valueOf ("10") - 有什么区别?

java - 最好的 Java OpenID 库

java - 启动器 Activity 未启动并崩溃

java - java中int...或N...的含义

json - 错误 错误 : getaddrinfo ENOTFOUND - mysql

javascript - JSON 将所选项目保存在数组中并传递到其他页面

javascript - 使用 AngularJS 的 $resource (v1.2.x) POST 原始 XML