Search This Blog

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.