Search This Blog

Thursday, 26 September 2013

java.lang.ClassCastException: org.json.simple.JSONObject cannot be cast to net.sf.json.JSONObject


Sep 27, 2013 11:42:56 AM com.sun.jersey.spi.container.ContainerResponse mapMappableContainerExceptionSEVERE: The RuntimeException could not be mapped to a response, re-throwing to the HTTP containerjava.lang.ClassCastException: org.json.simple.JSONObject cannot be cast to net.sf.json.JSONObject

 The Exception is very clear ..
org.json.simple.JSONObject cannot be cast to net.sf.json.JSONObject
since ,  we are using making use of two diff libs here ..

 The problem here is that the "jsonParser.parse("-string-");" will return a org.json.simple.JSONObject (json-simple-1.1.1.jar) && we are trying to store in into a JsonObject vreated via net.sf.json.JSONObject (json-lib-2.2.jar)

 Solution -simply remove the use of "net.sf.json.JSONObject" to create a JsonObject & INSTEAD use org.json.simple.JSONObject create a JsonObject.


 Hence, your code will also be slightly modified given the way you extract key-values going further..

 BEFORE-- 

  //## here the requisite jar(json-simple-1.1.1.jar) for JsonParser is included in the jersey bundle itself..
org.json.simple.parser.JSONParser jsonParser = new JSONParser();

// ## Included json-lib-2.2.jar for using JsonObject ,etc..
net.sf.json.JSONObject jsonObj;
try {
jsonObj = (JSONObject) jsonParser.parse(requestParam);
name = jsonObj.getString("name");
phone = jsonObj.getString("phone");
email = jsonObj.getString("email");

} catch (ParseException e) {
e.printStackTrace();
}

AFTER --

//## here the requisite jar(json-simple-x.x.jar) for JsonParser is included in the jersey bundle itself..
org.json.simple.parser.JSONParser jsonParser = new JSONParser();

// ## Included json-lib-2.2.jar for using JsonObject ,etc..
org.json.simple.JSONObject jsonObj = new JSONObject();
try {
jsonObj = (JSONObject) jsonParser.parse(requestParam);
name = jsonObj.get("name").toString();
phone = jsonObj.get("phone").toString();
email = jsonObj.get("email").toString();

/*phone = jsonObj.getString("phone");
email = jsonObj.getString("email");*/

} catch (ParseException e) {
e.printStackTrace();
}

Creating Render & Action URLs in Liferay

    
Creating A RENDER URL :

Using tags:    (preferred in JSP's)
<portlet:renderURL var="assignAgentTargetURL">
        <portlet:param name="agentId" value="<%= String.valueOf(AgentListBean.get_agentId())%>" />
        <portlet:param name="jspPage" value="/html/crm/team/assignAgentTarget.jsp" />
 </portlet:renderURL> 

Otherwise: (often used in case you wanna redirect to outside of current Portlet)
ie this type of URL creation is often helpful in case you are in (Portlet A) and wanna redirect to some jsp in(Portlet B).
Note: in such a case you must specify the portletId.portlet-name (refer below : plid) of the target Portlet.

long plid1 = PortalUtil.getPlidFromPortletId(themeDisplay.getScopeGroupId(), com.prabhakar.portal.product.util.Constants.PRODUCT_PORTLET_NAME);

 LiferayPortletURL url = PortletURLFactoryUtil.create(actionRequest, com.prabhakar.portal.product.util.Constants.PRODUCT_PORTLET_NAME,  plid1, PortletRequest.RENDER_PHASE);

---------------------------------------------------------------------------------------------------------------------------------------------------

Creating A ACTION URL:

Using tags: (preferred in JSP's)

    <portlet:renderURL name="assignAgentTarget" var="assignAgentTargetURL">
        <portlet:param name="agentId" value="<%= String.valueOf(AgentListBean.get_agentId())%>" />
    </portlet:renderURL> 

Otherwise: (often used in case you wanna redirect to outside of current Portlet)
ie this type of URL creation is often helpful in case you are in (Portlet A) and wanna redirect to some jsp in(Portlet B).
Note: in such a case you must specify the portletId.portlet-name (refer below : plid) of the target Portlet.

long plid1 = PortalUtil.getPlidFromPortletId(themeDisplay.getScopeGroupId(), com.prabhakar.portal.product.util.Constants.PRODUCT_PORTLET_NAME);

 LiferayPortletURL url = PortletURLFactoryUtil.create(actionRequest, com.prabhakar.portal.product.util.Constants.PRODUCT_PORTLET_NAME,  plid1, PortletRequest.ACTION_PHASE);

Monday, 26 August 2013

ANDROID: Connection to http://localhost:8080 refused

I wanted to make an Http Connection to my own WebServices(REST) exposed on another apache- tomcat server , but was getting this error :-
Connection to http://localhost:8080 refused
here's my code:
try{
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://localhost:8080/RestProject/rest/hello/Details");
HttpResponse httpResponse = httpClient.execute(httpGet);
InputStream content = httpResponse.getEntity().getContent();
BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
String s = "";
while ((s = buffer.readLine()) != null) {
response += s;

}
}

i checked the exposed webservices url it was working fine from the browser , but was surprised to see that my android app was unable to access the same ,being on the same machine..

hereby im sharing the solution that resolved my problem ,hoping it would help you too,

Actually, If you are referring to a localhost from your device(emulator) than use the http://10.0.2.2/ instead of the http://127.0.0.1/ or http://localhost/.
Because your Android emulator is running on a Virtual Machine(QEMU) and you can not connect to a server directly running on your PC.

So your code snippet should be like this:
try{
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://10.0.2.2:8080/RestProject/rest/hello/Details");
HttpResponse httpResponse = httpClient.execute(httpGet);
InputStream content = httpResponse.getEntity().getContent();
BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
String s = "";
while ((s = buffer.readLine()) != null) {
response += s;

}
}

ie use 10.0.2.2 instead of localhost or 127.0.0.1.



Sunday, 25 August 2013

How to create an war file in Eclipse without ant or maven?


Assuming that you created  a Dynamic Web project in Eclipse, here's a solution for you , 
 just right-click on the
project name > Export > WAR file

In case , you didn't created a Dynamic Web Project , you can convert your Static Project into one ..and then Proceed as above.

Tuesday, 20 August 2013

Unable to compile class for JSP

----------------------------------------------------------------------------------------------------------------------------------------
Aug 20, 2013 9:08:55 AM org.apache.catalina.core.ApplicationDispatcher invoke
SEVERE: Servlet.service() for servlet jsp threw exception
org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 2,062 in the generated java file
The code of method _jspService(HttpServletRequest, HttpServletResponse) is exceeding the 65535 bytes limit

----------------------------------------------------------------------------------------------------------------------------------------

TRY USING : 
<jsp:include page="/html/crm/customer/child.jsp"/>       
INSTEAD OF
<%@ include file="/html/crm/customer/child.jsp"%> 


Actually, if you think about it there is an extra performance hit with every action tag (@include file) .
For eg: say there are two files Child.jsp & Parent.jsp
where, Parent.jsp needs to include Child.jsp

--------------------( /html/.../Parent.jsp )----------------------------------------------------------------------------------
Try 
<jsp:include page="/html/.../Child.jsp"/>  
rather 
<%@include file="/html/.../Child.jsp"%> 
----------------------------------------------------------------------------------------------------------------------------------------
In case facing this Exception(above mentioned). But the only problem is that u need to remove all the dependencies of Parent from Child.
say,  boolean isAdmin= true;  is defined in Parent.jsp but Child.jsp is also being using this value.Otherwise ,in that case you might again get an exception saying : variable isAdmin in Child.jsp cannot be resolved . 

Because , the include directive(<%@include file=""%>) inserts the source of Child.jsp at translation time but the action tag(<jsp:include page=""/>) inserts the response of Child.jsp at runtime.

ANDROID: An established connection was aborted by the software in your host machine

----------------------------------------------------------------------------------------------
[2013-08-20 22:52:31 - ddmlib]An established connection was aborted by the software in your host machine
java.io.IOException: An established connection was aborted by the software in your host machine
    at sun.nio.ch.SocketDispatcher.write0(Native Method)
    at sun.nio.ch.SocketDispatcher.write(Unknown Source)
    at sun.nio.ch.IOUtil.writeFromNativeBuffer(Unknown Source)
    at sun.nio.ch.IOUtil.write(Unknown Source)
    at sun.nio.ch.SocketChannelImpl.write(Unknown Source)
    at com.android.ddmlib.JdwpPacket.writeAndConsume(JdwpPacket.java:213)
    at com.android.ddmlib.Client.sendAndConsume(Client.java:573)
    at com.android.ddmlib.HandleHeap.sendREAQ(HandleHeap.java:349)
    at com.android.ddmlib.Client.requestAllocationStatus(Client.java:419)
    at com.android.ddmlib.DeviceMonitor.createClient(DeviceMonitor.java:840)
    at com.android.ddmlib.DeviceMonitor.openClient(DeviceMonitor.java:808)
    at com.android.ddmlib.DeviceMonitor.processIncomingJdwpData(DeviceMonitor.java:767)
    at com.android.ddmlib.DeviceMonitor.deviceClientMonitorLoop(DeviceMonitor.java:635)
    at com.android.ddmlib.DeviceMonitor.access$100(DeviceMonitor.java:42)
    at com.android.ddmlib.DeviceMonitor$3.run(DeviceMonitor.java:563)

----------------------------------------------------------------------------------------------

These problem can be simply solved by closing Eclipse and restarting it again. Eclipse sometimes fails to establish a connection with the Emulator, so this can happen in some cases.

for eg:
This problem may occur if you have two devices connected to the computer at the same time. Adb does not support reaching both devices via command/console. So, if you debug your app after connecting and disconnecting the second device you will most probably have this problem. One solution might be restarting adb and/or eclipse if necessary. It can be quite annoying sometimes and I am afraid there is no other solution to that.

Also, If you develop in multiple IDE's or other programs that connect to AVD you should try closing them too.




Monday, 29 July 2013

For getting AVD or Android SDK Manager tool options in Eclipse Juno 4.2.1

Not able to see sdk manager tool in Eclipse (If Already Installed)

For getting sdk manager tool options in Eclipse Juno 4.2.1 Simple steps.

Window---->Customize Perspective--->Command Groups Availability

And then Simply check Android SDK and AVD Manager.


----------------------------------------------------------------------------------------
Install the ADT Plugin (In case not Installed)

 check the following link  [Install ADT Plugin] to install the ADT plugin into your Eclipse.

----------------------------------------------------------------------------------------

Updating the ADT Plugin (In case not Updated)
To update the ADT plugin to the latest version, follow these steps:
  1. Select Help > Check for Update ...
  2. If any update for ADT or Android tools is available, select and install.
----------------------------------------------------------------------------------------