Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Http Connection

 
Bilal A.Siddiqui
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"


 
Monu Tripathi
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at Issue 133: localhost not allowed
 
Nehel Patel
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
internet permission is already there in my manifest file.

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

 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bilal A.Siddiqui
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18001
47
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bilal A.Siddiqui
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18001
47
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic