Tuesday, November 27, 2012

Java Client to Consuming Liferay JSON web Services

Download java class and required jar files from following URL
Consuming Liferay Json Web Service

1)      Write fooling java program in your java project.

public static void main(String[] args) throws ClientProtocolException, IOException {
            ArrayList<HashMap<String, String>>mylist = new ArrayList<HashMap<String, String>>();

                        //Get the data (see above)
                        HttpHost targetHost = new HttpHost("localhost", 8080, "http");
                        DefaultHttpClient httpclient = new DefaultHttpClient();
                        httpclient.getCredentialsProvider().setCredentials(new AuthScope(targetHost.getHostName(), targetHost.getPort()),new UsernamePasswordCredentials("test@liferay.com", "test"));
                        AuthCache authCache = new BasicAuthCache();
                        BasicScheme basicAuth = new BasicScheme();
                        authCache.put(targetHost, basicAuth);
                        BasicHttpContext ctx = new BasicHttpContext();
                        //ctx.setAttribute(ClientContext.AUTH_CACHE,authCache);
                        HttpPost post = new HttpPost("/tunnel-web/secure/json");
                        List<NameValuePair> params = new ArrayList<NameValuePair>();
                        params.add(new BasicNameValuePair("serviceClassName", "com.liferay.portal.service.CountryServiceUtil"));
                        params.add(new BasicNameValuePair("serviceMethodName", "getCountries"));
                        params.add(new BasicNameValuePair("serviceParameters", "[]"));
                        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
                        post.setEntity(entity);
                        HttpResponse resp = httpclient.execute(targetHost, post, ctx);
                        resp.getEntity().writeTo(System.out);
                        httpclient.getConnectionManager().shutdown();
      
            }
           
}


2)       Compile and Run the java program using dos command prompt or using eclipse.
3)      See the console you will get the all Countries data in the json format.
Following are the required Jar files
commons-codec-1.7.jar
httpclient-4.1-beta1.jar
           
Add above jar file in your project build path. these are required to compile and run the the java client program.


Popular Posts

Recent Posts

Recent Posts Widget