Liferay Development

Liferay Consulting

Liferay Training

Your Trusted Liferay Solutions Partner

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.


1 comment :

  1. The effectiveness of IEEE Project Domains depends very much on the situation in which they are applied. In order to further improve IEEE Final Year Project Domains practices we need to explicitly describe and utilise our knowledge about software domains of software engineering Final Year Project Domains for CSE technologies. This paper suggests a modelling formalism for supporting systematic reuse of software engineering technologies during planning of software projects and improvement programmes in Project Centers in Chennai for CSE.

    Spring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Spring Framework Corporate TRaining the authors explore the idea of using Java in Big Data platforms.
    Specifically, Spring Framework provides various tasks are geared around preparing data for further analysis and visualization. Spring Training in Chennai

    ReplyDelete

Recent Posts

Recent Posts Widget

Popular Posts