Liferay Development

Liferay Consulting

Liferay Training

Your Trusted Liferay Solutions Partner

Wednesday, November 28, 2012

Liferay Android Application


Life ray Android example


Hi I am doing Android example using life ray. Here I did sample application in android that is I am populating the life ray countries table data in Android by using web services.
The following are the steps to the Android Application.
Prerequisites
We should have minimum development knowledge in Android application development.
Step: 1
The following tutorial will give good understanding about Android Application Development



Step: 2
Get the Android Development Tool from Following URL.






Note: This downloaded rar file having eclipse and Android SDK and Its tools like Android Emulator.
Step: 3
Extract this rar file in your local system. You can find folders name like adt-bundle-windows-x86 (for windows). If we open the adt-bundle-windows-x86 you can find two folder eclipse and sdk.
The following screen depicts what is available in rar file.
You can find two folders
Step: 4

Go to eclipse folder and launch eclipse by clicking on eclipse.exe icon.
Now eclipse will be launched and it will ask you work space and select your local work space.
In the below screen you can see the Android icons in eclipse.
Step: 6
Go through following link this tutorial explain how to run basic example in Android.
Now we have experience with Android Application Development.

Liferay Android Application

This Life ray Android Application populating Liferay country table countries in Android Mobile.
The concept behind this application is by using life ray JSON web services I am populating data in android in grid view by using Android components.
Here we have good experience with calling life ray web services in liferay. The following links are give you good knowledge about how to create liferay  json web services and how to consume json web services.



Once you read the tutorial then you will get good experience on calling life ray json web service from other client.

Now you can get Android Liferay Example from following location.



Once download Android Application .Place application into your android work space and Import this project into your eclipse as Android Existing Application.
Note: While import you may get some errors so that from the downloaded app you can delete .project, .classpath and proguard.cfg  files from your downloaded example before import.

Once download Android Application .Place application into your android the following are the screens which depicts how to import Existed Android Project into eclipse.
Screen-1




Screen 2
Screen 3


Run Android Application from eclipse.


Once run the project you can observe the following information in console like this.
Once application successfully installed then you can find Android emulator there you can see the emulator screen with countries data.

Important Points:
In the example to get JSON I am using apache http client mechanism.
In that I used URL to connect client and get the data from liferay. In main.java class you can find following line.
JSONArray  countriesJsonArray=JSONfunctions.getJSONfromURL("http://www.yourdomainname.com/tunnel-web/json/?serviceClassName=com.liferay.portal.service.CountryServiceUtil&serviceMethodName=getCountries&serviceParameters=[]");

Note:
When use localhost  to app that is not working. So your application in network host with domain name (I am not sure but for me it’s not working for localhost ).
When I used liferay portal which Is in network with www.xxxx.com then the data is getting in mobile.

Solution For Above Problem:

Use IP  address of your system in the code instead of localhost
Example: 127.0.0.1 or your LAN IP like 10.0.0.25

You can get More Information  about why localhost is  not worked? see below link
 http://www.liferay.com/community/forums/-/message_boards/message/18228590

Output in Emulator











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.


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.


Expose Liferay Custom JSON web Services in Plug-in Environment


Download plugin portlet from following URL


Following is the step by step description of generating a custom liferay plugin service and exposing it as JSON Web Service.

STEP: 1 Create a liferay plugin project and create new service

Here is the sample service.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.0.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_0_0.dtd">
<service-builder package-path="com.meera.json">
      <author>meera</author>
      <namespace>json</namespace>

<entity name="Student" local-service="true" remote-service="true">
            <column name="studentId" type="long" primary="true" />
            <column name="studentName" type="String" />
      </entity>
</service-builder>

Make sure remote-service=”true” in entity tag declaration.

STEP: 2  Build the service.
  
STEP: 3 write you custom method implementation in StudentServiceImpl.java

public com.meera.json.model.Student getStudent(long studentId)
      throws com.liferay.portal.kernel.exception.PortalException,
            com.liferay.portal.kernel.exception.SystemException {
      return StudentLocalServiceUtil.getStudent(studentId);
}

Note: Here I am using existed method only don’t confuse. You can write your custom method implementation instead of my method.

STEP: 4 build the service using command ant build-service from your eclipse ant view

STEP: 5 Add the following <servlet> and <servlet-mapping> Entries to portlet’s  web.xml file

<servlet>
<servlet-name>JSON Web Service Servlet</servlet-name>
<servlet-class>com.liferay.portal.kernel.servlet.PortalClassLoaderServlet</servlet-class>
<init-param>
<param-name>servlet-class</param-name>
<param-value>com.liferay.portal.servlet.JSONServlet</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JSON Web Service Servlet</servlet-name>
<url-pattern>/api/jsonws/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>JSON Web Service Servlet</servlet-name>
<url-pattern>/api/secure/jsonws/*</url-pattern>
</servlet-mapping>

STEP: 6 deploy the portlet using ant deploy command from eclipse ant view

STEP: 7 to access your json web services enter the following url –
Note: if you access above url you will get blank page in browser but don’t worry. Make sure no exception will be occurred in console. Go through following url
Following is full URL to get json data according to my example

http://localhost:8080/LiferayCustomJsonWebService-portlet/api/jsonws/?serviceClassName=com.meera.json.service.StudentServiceUtil&serviceMethodName=getStudent&serviceParameters=[studentId]&studentId=1


Note: Change port number according to your environment and change portlet-context name according to your portlet context name

Tuesday, November 20, 2012

Liferay Jasper Reports Example


Steps to run plug-in portlet
1)      Get The Portlet from following location.
2)      Place into your plugin directory of your running portal.
3)      Run ant deploy command.
4)      Open your portal on your browser,Add portler your page.
5)      Browse QueryReport.jrxml   file(This will be available in your dowloded rar file).
6)      Click on get reports. Then you can see the downloaded PDF(This data getting from Liferay User_ table).

Note: In QueryReport.jrxml    I hard coded the one URL according to my local port.please change port number that you have.
<template>"http://www.localhost.com:8080/Jasperexampleportlet/html/reports/ReportStyles.jrtx"</tmplate> 

Recent Posts

Recent Posts Widget

Popular Posts