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.
Related
My understanding is that as an android developer in order to develop native android apps for existing web applications built using Spring MVC or PHP or Angular JS, I need to access the data in JSON format and use in my android app as per my needs. For this I don't have to know anything about the web frameworks which were used to build the websites.
Is my understanding correct? If no, please correct my understanding.
If yes, should I just ask the developer (or) find that info someplace (posted for use) to know how to access the json data for their website and use it in my code? (or) is there something else I need to do?
If the developer has to provide the info for JSON, what should I specifically ask for? and What will the developer provide me?
Thanks for your help in advance!
Let me answer your questions, even though they're a bit vague.
Is my understanding correct?
Yes, it is, you can make Http calls to the web API and get and parse the JSON response according to your needs.
If yes, should I just ask the developer (or) find that info someplace (posted for use) to know how to access the json data for their website and use it in my code? (or) is there something else I need to do?
Of course, if you know the developer, you should ask him. Most of the popular websites who have open API's have some kind of documentation about their JSON responses. A good example is taking a look at reddit's API, you can get any page as a JSON response by simply attaching /.json at the end of any url. This might prove useful if you want to practice parsing json and sending http requests. I'd recommend using OkHttp library for this.
If the developer has to provide the info for JSON, what should I specifically ask for? and What will the developer provide me?
This question I find the most difficult one to answer since it's a bit vague. You ask what you need and what is the request you need to send to get it.
What this means is what parameters your request should have (the best example being if the site uses authentication, does it use oauth or something else, do you need to first log in and get an access_token as is usually a case). This can vary tremendously depending on what you're trying to get, some requests are POST, some are GET and might not even require any additional parameters etc.
For example if you want to get a list of, for example, all cars from a website, you'd ask or search for a route that returns a JSONArray of cars and you'd presumably wanna send a GET request to that route (with access_token parameter if they use oauth, just an example) and you'd receive a respone, which may look something like this:
{
[ {"id" : "1",
"manufacturer" : "bmw",
"model" : "320d"},
{"id" : "1",
"manufacturer" : "audi",
"model" : "A6"}
]
}
This is purely for demonstrative purposes. The JSON may be arbitrarily more or less complicated, and you can parse it according to your needs, you might wanna use just some of the data contained there. There are a lot of questions on the topic of json parsing, so I won't get into that any further.
I hope this was informative to you, and if you're looking to make your own API, there are a lot of ways you can do this, you can also find many tutorials on this topic. I've done this in Laravel and Symfony PHP frameworks, and let me tell you, it's not that hard at all. All you need to do is always return a JSON and that's basically it.
Using the SurveyMonkey API, I'm writing some Java code to do survey analysis. The survey monkey API returns JSON data. I would like to generate some Java classes so I get some type safety and conciseness while I'm manipulating the data. I've had no luck finding a json schema for the Survey Monkey API.
So, I'm looking into tools that generate a schema from json directly, but obviously that will be less desirable than getting it from the canonical source.
The question:
Can anyone recommend a tool-chain that will take me from a set of json examples to a set of java classes that can be used to read an manipulate that json. This might include the intermediate step of generating a schema, but the end-game I'm after is the classes.
If anyone knows of a schema for the API, though, that would be even better.
Why don't you go with polljoy, it's Opensource and I have already integreat it on one of my site and it's working perfectly, you can find it out one below menioned link.
Polljoy
I want to get end of day stock quotes using Java and was given a WSDLurl showing the xml.
All the places that I find on this topic want to show me how to create a service, and that is complicated. All I want to do is connect to the url and get the data.
This link seems close, but still wants to generate some xml code.
http://axis.apache.org/axis2/java/core/tools/eclipse/wsdl2java-plugin.html
Anyone have a simple java example where you get data from a WSDL url?
Thanks
I highly recommend using something like http://cxf.apache.org/docs/wsdl-to-java.html. Otherwise, you'll have all the pain of trying to deal with the SOAP protocol, and all of it's associated quirks and hoops.
You can use wsimport.
Copy-paste solution
wsimport -keep http://localhost:9999/ws/hello?wsdl
Tutorial
Have a web service that implements REST (sort of) ,
Client request is made for some entity
Server populates a model (that was created with JAXB)
Converted to Xml and sent back
Client reads Xml into same JAXB Model
This works fine, but i dont use any REST libraries.
Am I missing out, I cant see what complexity they could hide because the code to send /receive a request and convert to/from JAXB is already pretty simple.
For your basic use case, you will probably not benefit a lot from using JAX-RS(http://en.wikipedia.org/wiki/Java_API_for_RESTful_Web_Services), which is the Java standard for REST.
However, as you can see if you follow the link, there are some useful annotations, which might make your life easier later on. For example, if you would start returning plain text next to xml as well, based on request header, that becomes incredibly easy to configure(with #Produces).
Same for when you want to support multiple types of request payloads(#Consumes).
Check http://docs.oracle.com/javaee/6/tutorial/doc/gkknj.html for a good tutorial.
In short, JAX-RS offers a lot of useful functionality with regard to request headers, parameters,etc.. that would otherwise be harder to implement.
However, many applications do not need this "full fine-grained REST" support, so sticking with just JAXB might be enough for your needs.
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.