Using JSON Web Service from a Java Client Application

I am developing a Java client application that has some functionality that requires receiving data from some web services that are passed in JSON (some are RESTful, some are not). No JavaScript, no web browser, just a simple JAR file that will run locally with Swing for a graphical interface.

This is not a new or unique problem; of course, there must be some open source libraries that will handle JSON data transfer over HTTP. I already found some that will parse JSON, but I'm having trouble finding any that will handle HTTP communications in order to use the JSON web service.

So far, I have found that Apache Axis2 apparently has at least part of the solution, but I don't see enough documentation to find out if it will do what I need, or how to use it. Perhaps part of the problem is that I don’t have experience with web services, so I can’t understand the solution when I see this. I hope some of you can point me in the right direction. Examples would be helpful.

+3
source share
4 answers

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

, HtmlUnit, , ( Html, javascript css, javascript- , , , JSON JSON.parse ) .

HtmlUnit :

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

, HttpClient. , JSON- java, json-lib

+4

, URLConnection, Resty for Java. , , .

http://beders.github.com/Resty

JSON:

Resty r = new Resty();
String name = r.json("http://ws.geonames.org/postalCodeLookupJSON?"+
    "postalcode=66780&country=DE").get("postalcodes[0].placeName").toString();

. JAR, .

+5

, - RESTful. - RESTful . ... , JSON ... , JSON XML, HTTP-.. :

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();
    ....
    ....
}

XML -. StringBuilder, XML. - JSON. javaJSON API, , JSON.

...

PS: HTTPConnectionManager, XMLParserand Request( Requestobject) classes are not standard API. they are written by my account to handle multiple web service calls. This piece of code is intended only to give you my idea.

0
source

I did this using 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 web service uses OAuth and an access token, you cannot use the example above.

0
source

Source: https://habr.com/ru/post/1782217/


All Articles