3

I am developing a client-side Java application that has a bit of functionality that requires getting data from some web services that transmit in JSON (some RESTful, some not). No JavaScript, no web browser, just a plain JAR file that will run locally with Swing for the GUI.

This is not a new or unique problem; surely there must be some open source libraries out there that will handle the JSON data transmission over HTTP. I've already found some that will parse JSON, but I'm having trouble finding any that will handle the HTTP communication to consume the JSON web service.

So far I've found Apache Axis2 apparently which might have at least part of the solution, but I don't see enough documentation for it to know if it will do what I need, or how to use it. Maybe part of the problem is that I don't have experience with web services so I'm not able to know a solution when I see it. I hope some of you can point me in the right direction. Examples would be helpful.

Gigatron
  • 1,995
  • 6
  • 20
  • 27

3 Answers3

4

Apache HttpClient 4.0 is the best in the business and is moderately easy to learn.

If you want easier you could use HtmlUnit which imitates the behaviour of browsers so you could easily get the content (and parse it into Html, javascript and css, you could also execute javascript code on content so you could probably parse JSON files to using JSON.parse or any other equivalent functions) of any page on the web.

so for HtmlUnit here is a sample code:

WebClient wc = new WebClient(BrowserVersion.FIREFOX_3_6);
HtmlPage page = wc.getPage("http://urlhere");
page.executeJavaScript("JS code here");

but it maybe rather heavy for your requirements so a highly recommend the use of HttpClient library. I'm sure you could find many JSON libraries for java but here is one for you json-lib

Amjad Masad
  • 4,035
  • 1
  • 21
  • 20
  • Thanks, I'll check out those. I already have the JSON parsing under control (the application also reads and parses local JSON files). It's the HTTP communication and extracting the JSON out of it which I'm trying to solve without reinventing the wheel. – Gigatron Dec 27 '10 at 13:17
  • Then the HttpClient library suits you best. – Amjad Masad Dec 27 '10 at 13:27
2

I did it using a simple Java JSON libary. Use the Google library..

URL url = new URL("http://www.siteconsortium.com/services/hello.php");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

JSONParser parser=new JSONParser();
Object object = parser.parse(in);

JSONArray array = (JSONArray) object;        
JSONObject object2 = (JSONObject)array.get(0);
System.out.println(object2.get("hello")); 

If the webservice uses OAuth and an access token you can't use the above example though.

JTHouseCat
  • 445
  • 4
  • 4
0

Its great to see that your web services are RESTful. RESTful web services are pretty easy to develop and to consume.Well... you do not need to take any extra care to tranmit JSON data over the network... Data whether is in JSON on in XML format are embedded into the HTTP header..Following code snippet will help you understand the idea :

httpConnection = new HTTPConnectionManager(request);
HttpURLConnection httpURLConnection = httpConnection.connect();
int responseCode = httpURLConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
    in = httpURLConnection.getInputStream();
    int x;
    StringBuilder stringBuilder = new StringBuilder();
    while ((x = in.read()) != -1) {
        stringBuilder.append((char) x);
    }
    XMLParser xmParser = new XMLParser();
    ....
    ....
}

In this code i am receiving data in XML format from web services.After receiving the data into a StringBuilder object,i am parsing the XML. In the same way you can call your web services using this code and can receive your JSON data. you can use javaJSON APIs,available Here, to extract the data from JSON notation.

Hope code will help you...

PS: HTTPConnectionManager,XMLParser and Request(request object) classes are not any standard APIs. they are written by my own account to handle multiple web service calls. This code snippet is just to give you my idea.

Priyanjan
  • 329
  • 3
  • 5
  • Unfortunately some of the web services are external and non-RESTful. I've written web pages and servlets before so I'm familiar with GET and POST concepts and it seems the RESTful method of delivering web services will be easier to grasp. But when it is non-RESTful, where does the JSON or XML result go? In the same place where the body of an HTML page would go, if it was returning HTML? – Gigatron Dec 27 '10 at 12:46
  • Yes JSON or XML data is embedded into HTTP header.... It occupies the same place what HTML code occupies in HTTP Header. In fact, every data travelling through HTTP retains in same place. – Priyanjan Jan 04 '11 at 11:16