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.

4

3 に答える 3

4

Apache HttpClient 4.0 はビジネスで最高であり、習得が適度に簡単です。

より簡単にしたい場合は、ブラウザーの動作を模倣するHtmlUnitを使用して、コンテンツを簡単に取得できます(Html、javascript、およびcssに解析し、コンテンツに対してjavascriptコードを実行して、JSONファイルを解析してJSONを使用できるようにすることもできます。 Web上の任意のページの.parseまたはその他の同等の関数)。

したがって、HtmlUnitのサンプルコードは次のとおりです。

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

ただし、要件に対してかなり重い可能性があるため、HttpClientライブラリの使用を強くお勧めします。Java用の多くのJSONライブラリを見つけることができると確信していますが、ここに json-lib用のライブラリがあります

于 2010-12-27T02:13:49.523 に答える
1

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.

于 2014-01-25T02:27:41.480 に答える
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.

于 2010-12-27T04:46:29.647 に答える