i'm trying to get data from twitter api with java jsoup and json-simple libs
Document doc = Jsoup.connect("https://api.twitter.com/1.1/search/tweets.json")
.header("Authorization", "Bearer " + token)
.header("charset", "utf-8")
.data("q", q)
.data("count", "2")
.data("max_id", currentStartId)
.ignoreContentType(true)
.get();
Then i'm receiving some json object. But when i try to parse it
String response = doc.text();
JSONObject requestObj = (JSONObject) parser.parse(response);
i'm getting this error
Exception in thread "main" Unexpected character (\) at position 3535.
at org.json.simple.parser.Yylex.yylex(Yylex.java:610)
at org.json.simple.parser.JSONParser.nextToken(JSONParser.java:269)
at org.json.simple.parser.JSONParser.parse(JSONParser.java:118)
at org.json.simple.parser.JSONParser.parse(JSONParser.java:81)
at org.json.simple.parser.JSONParser.parse(JSONParser.java:75)
in json position 3535
"description":""\u0412\u0435\u0434\u043e\u043c\u043e\u0441\u0442\u0438". \u0415\u0436\u0435\u0434\u043d\u0435\u0432\u043d\u0430\u044f \u0434\u0435\u043b\u043e\u0432\u0430\u044f \u0433\u0430\u0437\u0435\u0442\u0430"
You shouldn't be using Jsoup as its designed for parsing and cleaning HTML pages. It's unlikely that whatever it spits out is useful Json for you to process.
https://jsoup.org/apidocs/org/jsoup/Jsoup.html#connect-java.lang.String-
Use to fetch and parse a HTML page.
As the comment above suggests, you should use Twitter4J instead for this. Or even just process the JSON directly after fetching with URLConnection or OkHttp.
Related
How can i get only index name from response generated by calling _cat/indices using java. Using EntityUtils i am getting the response as string which gives me information in the way how it looks when called using CURL command. I am using rest low level client for fetching the response.
String responseBody = EntityUtils.toString(response.getEntity());
How should i process the responseBody to get only index name ?
ClusterStateResponse response = client.admin().cluster().prepareState().execute().actionGet();
String[] indices = response.getState().getMetaData().getConcreteAllIndices();
You will get the list of indices.
I have the following code that uses Java JSON:
Widget w = new Widget(true, "LIVE");
WidgetService service = new WidgetServiceImpl(); // 3rd party JSON web service
JSONObject response = service.postWidget(w);
System.out.println("Response is: " + response.toString());
System.out.println("Now fetching orderid...");
System.out.println(response.getString("order_id"));
Don't worry about Widget or WidgetService: this question has to do with how I'm using the Java JSON API (and specifically JSONObject).
When I run the above code, I get:
Response is: {"response":{"credits_used":"0.30","job_count":1,"order_id":"243050","currency":"USD"},"opstat":"ok"}
Now fetching orderid...
Exception in thread "main" org.json.JSONException: JSONObject["order_id"] not found.
at org.json.JSONObject.get(JSONObject.java:473)
at org.json.JSONObject.getString(JSONObject.java:654)
at com.me.myapp.MyDriver.main(MyDriver.java:49)
As you can see, there is an order_id String field coming back in the response, and it has a value of "243050". So why am I getting the exception?
Your JSONObject response points to the outer json object.
I am pretty sure, your response object has a property "response" (and "opstat" btw.) containing your expected object.
You have to do it like this:
response.getJSONObject("response").getString("order_id");
I am creating a Server HealthCheck page. Most of the servers return JSONObject and I am able to easily parse it using:
String jsonText = readAll(br);
JSONObject json = new JSONObject(jsonText);
JSONObject resp = json.getJSONObject("Response");
Now my problem is the other servers that do not return JSON. Some are returning String, some pdf file some image and as all the responses are 200 OK I should return Positive healthcheck.
However I am using Future Threads and timing out my request after 4 secs. And all the servers which return anything other than JSON get stuck at " new JSONObject(jsonText);"
Is there any way I can check if the type of respone is JSON or not in Java?
Update:
I found my mistake - stupid one.
In the try catch block I am only catching IOException, which is not catching JSONException(and didnt show any error too - dunno y). I have added a new catch block for JSONException which works for my solution for now.
However, this isnt elegant solution, #Dave, #Radai and #Koi 's solutions is the right approach to go with.
Don't parse the string. Go back one step, and check the Content-Type header of the HTTP response. If the response contains JSON data, the Content-Type should be application/json (source).
Check the MediaType in the response using response.getMediaType(). If it is json it returns application/json.
This helped me;
if (response.getMediaType().isCompatible(MediaType.APPLICATION_JSON_TYPE)) {
System.out.println("The media type matches application/json");
} else {
System.out.println("The media type does not match application/json");
}
I need to read the content of some posts i'm retrieving from a blogger feed.
This code retrieves the first available post from the blogger feed
URL postsFeedUrl = new URL("http://www.blogger.com/feeds/" + blogId + "/posts/default");
Query postsQuery = new Query(postsFeedUrl);
Feed resultFeed = myService.getFeed(postsQuery, Feed.class);
Entry e = resultFeed.getEntries().get(i);
The problem is: how to get the post content?
If i use
e.getContent();
i get a Content object from which i don't know how to extract the real post content.
If i use
e.getPlainTextContent();
It results in
Exception in thread "main" java.lang.IllegalStateException: TextConstruct object is not a PlainTextConstruct
at com.google.gdata.data.BaseEntry.getPlainTextContent(BaseEntry.java:358)
at BloggerFeed.printAllPosts(BloggerFeed.java:49)
at BloggerFeed.main(BloggerFeed.java:28)
How can i retrieve the post content with the GData API?
You need to add param fetchBodies=true.
I'm writing an application using the public Tumblr API, just for fun. I have set up my oauth keys, and I have the URL for accessing my blog's info. I was wondering how I could take the JSON-encoded data from that page and turn it into Strings for working with.
To be clear, if I wanted a blog's title, I could send a request to this URL and select the data for the title.
Thanks!
I've used Restlet for retrieving JSON data from a rest service. Below is the sample
Representation entity = new ClientResource("your url").get();
JsonRepresentation jsonRepresentation = new JsonRepresentation(entity.getText());
JSONObject jsonObject = jsonRepresentation.getJsonObject();