如何使用TwitterObjectFactory.jsonStoreEnabled获取JSON推文及API弃用咨询
Hey there! Let's tackle your questions step by step:
JSONStoreEnabled to get JSON-formatted tweets You're already on the right track here! In your code, you've set configurationBuilder.setJSONStoreEnabled(true)—this is the critical step to enable Twitter4J to store raw JSON data for status objects.
Once that's enabled, you can use DataObjectFactory.getRawJSON(status) to retrieve the full raw JSON string for each tweet, which you're already doing in your insertStatus method. Just to confirm: this method will only work if JSONStoreEnabled is set to true; otherwise, it'll return null or throw an exception. Your current JSON implementation is correct!
The compile warning tells you some API you're using is marked as deprecated. To pinpoint exactly which part is causing the issue, recompile with the -Xlint:deprecation flag to get detailed details. Run this command in your project directory:
gradle compileJava -Xlint:deprecation
This will print the specific class/method that's deprecated, along with its recommended replacement.
A common fix here is to ensure you're using the latest stable version of Twitter4J. Check your build.gradle file and update the dependency to the newest release (e.g., 4.0.7 is a recent stable version):
implementation 'org.twitter4j:twitter4j-core:4.0.7'
Newer versions often replace deprecated APIs with updated alternatives, so this might resolve the warning automatically.
Great news—Twitter4J has built-in support for generating raw XML for status objects, similar to how you get JSON. Here's how to set it up:
First, add the XMLStoreEnabled configuration to your ConfigurationBuilder:
configurationBuilder.setDebugEnabled(true) .setJSONStoreEnabled(true) // Keep this if you still want JSON support .setXMLStoreEnabled(true) // Add this line to enable XML storage .setOAuthConsumerKey(properties.getProperty("oAuthConsumerKey")) .setOAuthConsumerSecret(properties.getProperty("oAuthConsumerSecret")) .setOAuthAccessToken(properties.getProperty("oAuthAccessToken")) .setOAuthAccessTokenSecret(properties.getProperty("oAuthAccessTokenSecret"));
Then, in your insertStatus method, use DataObjectFactory.getRawXML(status) to get the XML string directly:
private void insertStatus(Status status) throws JSONException, TwitterException { // Optional: Keep your JSON handling if you still need it String jsonString = DataObjectFactory.getRawJSON(status); JSONObject json = new JSONObject(jsonString); log.fine("JSON Tweet: " + json.toString()); // Get XML-formatted tweet String xmlString = DataObjectFactory.getRawXML(status); log.fine("XML Tweet: " + xmlString); // Now you can use this XML string with BaseX, like your commented-out code: // XMLResource res = (XMLResource) col.createResource(id, XMLResource.RESOURCE_TYPE); // res.setContent(xmlString); // col.storeResource(res); }
This will give you the full XML representation of each tweet, which you can directly store in your BaseX database.
内容的提问来源于stack exchange,提问作者Thufir

