gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 09:08:04 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com