Extracting data in java from a received java-script page - java

I receive a java-script encoded page when I am doing the GET request for a search in Google api.
Now I need to extract the value of "unescapedUrl" and "titleNoFormatting" out of this.
Normally in C++ I would use search by word and then use pointers to get the next coming characters, but I have no idea what to do in java.

I receive a java-script encoded page when i am doing the GET request for a search in Google api.
This appears to be mostly JSON.
normally in c++ i would use search by word and then use pointers to get the next coming characters
If so, and if this is JSON output, then a C++ developer should use a JSON parsing library.
but i have no idea what to do in java.
Similarly, a Java developer should use a JSON parsing library. Android has the org.json parsing classes built in, and there is a separate JSONReader (based on GSON) in Android 3.x.

Related

Java: http request, print headers and parse json

I have no knowledge of Java, but a collaborator is writing code and attempts to parse a JSON response that the server that I am writing is providing. I also created this question yesterday trying to see if there was a problem on my end. However, the problem still remains.
So, can someone give me some simple code of what needs to be written in Java in order to make an http request to a server (say http://www.google.com) print the headers of the response and moreover parse a json object that is returned? Or at least, which functions should I be using and which libraries I should include in the source code. I want to believe that I will figure it out after that point.
I am totally clueless how to do that and what sort of libraries I need in java. Honestly, I do not know java.
Your first step would be looking into the Servlet specification of the Java language. I would recommend the Google GSON library for parsing JSON.

How do I get natural json notation from app engine?

I have built a REST application with Jersey and didn't succeed in getting natural notation out of JAXB. I simply adapted my GWT application to cast the wrongly transmitted types to what I wanted. I know, not the best solution, but there were no time for something else.
Now that I try to set up cloud endpoints on app engine and get the exact same format which is not what google show in their examples, I suspect that there is something I have missed regarding GAE.
This is what I want from GAE:
{
"anumberprop":123,
"atextprop":"blah",
"anarray":[{"another_no":432}]
}
This is what I actually get:
{
"anumberprop":"123",
"atextprop":"blah",
"anarray":{"another_no":"432"}
}
This causes problems since the client will be a GWT app or a Google Apps Script which parses the JSON internally and this will give a lot of exceptions when the types don't match.
What am I doing wrong?
Best regards,
Peter
The 432 vs "432" is likely because Cloud Endpoints quotes 64-bit integers so JavaScript clients can use a standard JSON parser without losing precision on those numbers. I'm unable to reproduce the missing array brackets. Can you post your response object?

How do I use Google Maps API in a Java code to fetch coordinates of specified location?

In a BI project I'm currently working on, we are in need of geo-coordinates for a list of locations. With the address location (such as "New York, US") as input, the output should be the coordinates as a latitude-longitude pair (like {40.71435, -74.00597}). The behaviour is similar to what is seen on this page.
A similar question earlier on SO points to using the Google Maps API in JavaScript to achieve this, but I'm looking for a Java solution -- some function of the form getCoordinates(location), because this is a small requirement in a larger Java program already in existence.
Any pointers on how I may use the Google Maps API (or any other maps API) in Java to achieve this would be of great help!
You can use the Google Geocoding HTTP API (see here).
To connect to it and get the responses you can use a Java URLConnection (tutorial is here) and parse the response using your favourite Json library (I personally use Jackson)
So you'd like a way to perform Google Maps API Geocoding via Java - here's one that might work for you. The response might not be in the exact same format you need but should be pretty workable:
http://code.google.com/p/geocoder-java/
You can see the final format returned in LatLng.java - just trace the code through starting from GeocodeResponse.java and you'll see the final format - the classes are pretty simple.

Android - Obtaining data from a website

I'm finding my way around Android and so far so good. My next big challenge is coming to grips with web services. I would like to build an app that reads data from a web site or database on web server and store the data in my app.
Basically, it will be an app that I build in conjunction with a news website that pulls their latest articles into the app. What I'm finding difficult is how to bridge the gap between my application and the data in the SQL Server database.
I'm familiar with building asp websites that read data from a database, but how would I do something similar with an app?
Do I ask the website to store the articles in an xml format? Or, is there another way that I can request a specific article and be provided with the content?
I hope I'm phrasing the question correctly and that someone can just guide me to the right way to approach this.
Thanks in advance.
You can approach this problem from different perspectives.
The common solution is to build a Webservice that will bridge the gap between your mobile application and the data that remain in your server. I personnaly prefer to setup a Rails backend and thus have a RESTful API that will help me access my data. For instance, to retrieve the list of articles I could just request the following url: http://my_server_host/articles. So for the Webservice part you can have whatever you want: Rails, J2EE, .NET etc. And you can choose the model that fits your needs (REST, SOAP, XML-RPC etc.).
Then you will have to write a class that will contain all the necessary calls to the Webservice you have built. Basically, if your Webservice returns the results as an XML format you will have to:
Send the request to the appropriate URL. (See: HttpGet or HttpPost if you want to modify a resource).
Parse the XML returned. (In short, you can use SAX or DOM to parse your XML response and transform them to a business entity (an Article, a User etc.).)
This hopefully gives you a hint about a possible solution. By the way Google is your friend, but I will probably come back to add external links/resources to help you more.
Edit
Another possible solution that could work for you, since all you need is to retrieve some articles. Just setup a simple Wordpress blog for instance. Wordpress gives you an URL for the blog's RSS feed, all you will have to do is to parse that RSS feed (XML). There is a great article on the IBM website for parsing an RSS feed that you can find here. By the way, this solution is only possible if you want to save your articles on a Wordpress blog. But you got the point hopefully.
Reading your data form the Database on the Server would be bad practice. You'd have to open up some ports and that's defiantly not what you want (if you don't have root-access, you also can't).
For non-interactive content (what you want) you would use XML or JSON.

Parse JSON response from Google Maps page

I'm trying to find the best way of parsing the response from a "normal" (i.e. not using the API) Google Maps page in my java code.
Reason: I want to submit a query string requesting a listing (be it hotels, restaurants etc.) and then parse the JSON that comes back. I had looked into using the Google Maps API, but it doesn't seem to cover what I want to do, as this type of URL:
http://maps.google.de/maps/geo?q=address&output=xml&oe=utf8&sensor=false&key=...
is OK but this isn't:
http://maps.google.de/maps/geo?q=address+hotels&output=xml&oe=utf8&sensor=false&key=...
(due to the "+hotels" term). So I think the only option is to use a google maps response e.g.
http://maps.google.de/maps?q=address+hotels
and parse the JSON information that is included at the end. Does anyone have some hints as to how best accomplish this?
You should first make absolutely sure that the API doesn't support what you need. Checking the docs and maybe even reaching a real Googler might pay off. It strikes me as odd that their API wouldn't support something as simple as adding in another term.
If you're forced to do it the "hard way", there are two main steps:
1) Find and learn a JSON parsing library for Java. I can recommend Jackson -- fast, sturdy, and just released a version 1.0.0.
2) Teach your code to understand the spec the Google uses in their response. This is by far the most challenging part. My apologies, but I know nothing about Google's spec in this area. If you can find official docs, that's best. Or find unofficial docs published by someone else who had to do similar work. Otherwise, you may have to "reverse engineer".
Re. the google api docs: it does seem that what you're trying to do goes against the intention of Google to make their product (= a map) available to you, the developer, for your custom enhancement (by adding business outlet information or whatever). There's plenty of stuff on the Google maps API site describing this. But to parse their data (coming out of their database) and to display it independently of their product would seem to be rather different: section 10.12 of the terms explicitly cover this:
...code.google.com/intl/de/apis/maps/terms.html
However, there are apps out there (the "Around Me" iPhone app, for example) that seem to do just that: there might be a special arrangement between Google and Apple in that regard.
EDIT: alternatively you could look at this problem another way and use the Google Base API feed, since this allows you to build query strings specifying resource, distance, location etc. - i.e. it returns the data you require without using the Maps API (which you don't need anyway, given your description).

Categories