aspose file tools*
The moose likes Android and the fly likes Http Connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Android
Bookmark "Http Connection" Watch "Http Connection" New topic
Author

Http Connection

Bilal A.Siddiqui
Ranch Hand

Joined: Feb 22, 2010
Posts: 31
Hi all

I am trying to access a servlet from my android application. my code looks like this


I am getting an error "connection refused"



Regards
Bilal
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

Take a look at Issue 133: localhost not allowed


[List of FAQs] | [Android FAQ] | [Samuh Varta]
Nehel Patel
Greenhorn

Joined: Feb 09, 2010
Posts: 21
Hi,

I think you need to add the following line to your AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />

and one more thing, replace 'localhost' in your URL with your machine IP.

try it...
Bilal A.Siddiqui
Ranch Hand

Joined: Feb 22, 2010
Posts: 31
hi

i have tested the code with 10.0.2.2 in place of localhost and then with my machine ip which is (172.18.24.188)

but in both cases i am getting an error localhost/127.0.0.1:8080- Connection refused
Bilal A.Siddiqui
Ranch Hand

Joined: Feb 22, 2010
Posts: 31
internet permission is already there in my manifest file.

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41461
    
  51
Note that 10.0.2.2 is the "magic" IP address you'd use if running the app in the emulator. If you run it on the actual device (maybe over a WLAN) then you need to use the IP address assigned by the router to the desktop machine, which is propably something like 10.0.1.2.


Ping & DNS - my free Android networking tools app
Bilal A.Siddiqui
Ranch Hand

Joined: Feb 22, 2010
Posts: 31
I am running this app in an emulator. my application is just sending "get" request to a servlet which is mapped in web.xml

but i am getting connection refused.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16011
    
  19

Bilal A.Siddiqui wrote:I am running this app in an emulator. my application is just sending "get" request to a servlet which is mapped in web.xml

but i am getting connection refused.


And you will, until the message stops returning the localhost IP address 127.0.0.1. Something's still wrong with your URL.


Customer surveys are for companies who didn't pay proper attention to begin with.
Bilal A.Siddiqui
Ranch Hand

Joined: Feb 22, 2010
Posts: 31
i have a doubt now whether this android activity knows about my servlet or not. I mean does it know to lookup for that in web.xml. i just felt so because whether tomcat server is ON or OFF the error is same.

localhost/127.0.0.1:8080- Connection refused
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

I understand, you tried http://10.0.2.2:8080/getAndroid on the emulator with Tomcat Running and it did not work, correct? Have you tried what Ulf suggested? [Try IP address of your desktop machine]
Bilal A.Siddiqui
Ranch Hand

Joined: Feb 22, 2010
Posts: 31
hi monu

i tried with my system ip also but the result was same. now i am confused because whether my tomcat server is running or not its giving same error. am i making any mistake in project structure??
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41461
    
  51
i just felt so because whether tomcat server is ON or OFF the error is same.

Can you make ANY network connections, for example using the browser? Does the machine have a firewall that's blocking incoming connections?
Bilal A.Siddiqui
Ranch Hand

Joined: Feb 22, 2010
Posts: 31
hey i have run it on other system in two modes

1. with tomcat server OFF--> nothing happens

2. with tomcat server running--> i get 404 status

what should i do now ???
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

with tomcat server OFF--> nothing happens

depending on what is the default Time Out interval set for HttpClient, your code may either block indefinitely or will get an TimeOutException.
with tomcat server running--> i get 404 status

This is classic Resource not found; are you sure your service is deployed on the server? Can you try it out from your browser?

Additionally:
1. Check if you can access other sites from your emulator.
2. Change the URL in your code to some thing more common like http://www.google.com and see if you get a valid response code.

---
If you are interested you can read up on Android Emulators Network Address Space mapping here.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16011
    
  19

Just to clarify on Monu: a 404 error comes from Tomcat. So you're definitely talking to the server now. However you haven't deployed the server application in a way that maps to your URL.
Bilal A.Siddiqui
Ranch Hand

Joined: Feb 22, 2010
Posts: 31
i have done it !!! wow

i called get method of my servlet from my android app, its runninh well.

thanks to all
Durga Sanju
Greenhorn

Joined: Jun 03, 2010
Posts: 10
Please send the code for connecting android with servlet. Yours is the first and foremost that you have successfully connected, because i have googled almost all the sites but nothing worked.

It will be very very helpful if you send me the code for both client and server.

Thanks in advance.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Http Connection