This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Android and the fly likes Connecting to local Web Service from Android emulator does not work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Mobile » Android
Bookmark "Connecting to local Web Service from Android emulator does not work" Watch "Connecting to local Web Service from Android emulator does not work" New topic
Author

Connecting to local Web Service from Android emulator does not work

Andre Kuntze
Greenhorn

Joined: Jun 29, 2011
Posts: 18
Hi everybody,

I am trying to connect from my Android emulator in Eclipse to my local Webservice I have written in Netbeans.

I tried the following -

Code in Eclipse - HelloAndroid.java:



Code in Netbeans - HelloWorld.java:



and in Netbeans - StatusObject.java:



In my little naive World I expected the following behavior:

1. Starting the web service in Netbeans and browsing to http://localhost:8080/HelloWorldApplication/resources/helloworld. That should give me the message: "Hello, World false" -> this works.
2. Starting my Android app which should make a HTTP put request -> no error, so I think this may work also.
3. Browsing to http://localhost:8080/HelloWorldApplication/resources/helloworld again which should give me the message "Hello, World from Android true" -> does not work.

Could you guys give me some hint? I am pretty new to both Web Services and Android and I am a bit lost right now :-(

Thanks in advance for any help
André
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42042
    
  64
"localhost" always points to the local system - which in the case of the emulator is ... the emulator. Which, I am prepared to guess, is not running a web server :-)

The emulator can access the host OS through the "magic" IP address 10.0.2.2. If you connect an actual device via USB, it can reach the host OS via 10.0.1.2.


Ping & DNS - my free Android networking tools app
Andre Kuntze
Greenhorn

Joined: Jun 29, 2011
Posts: 18
Hello Ulf,

thank you very much for the answer but it did not work. I knew about that "localhost" adress but while tinkering around with the code it seems like I overwrote my own changes. Here is the new code:



The behavior is still the same. I only see "Hello, World false" after executing the HTTP Put request via the Android emulator. I am pretty sure I am doing something really stupid here which keeps it from running properly but right now I just do not get it

If you guys need anymore information, I will post it. Just let me know.

Greetings
André
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42042
    
  64
The PUT call does not seem to execute anything - make sure it actually gets to the server. Calling httpCon.connect() may be sufficient for that, or writing something to the output stream.
Andre Kuntze
Greenhorn

Joined: Jun 29, 2011
Posts: 18
Hi Ulf,

thanks again for the quick answer. I tried both ways - the connect statement and the outputstream - but both did not work :-(

These was the Eclipse code i executed:

1. Adding the conenct statement:



and 2. the Output Stream:



Still I only get "Hello, World false". Is there something else I can try or any log I can provide?

Thanks again
André
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42042
    
  64
Is the putHtml method ever called? Have you used an HTTP monitor to determine whether the call is ever sent?
Andre Kuntze
Greenhorn

Joined: Jun 29, 2011
Posts: 18
Hi,

I did not monitor so far. How would I do that with Glassfish? Do I have to add a plugin or is this an already installed feature? Can I find it in Netbeans or in the admin console?

Sorry, Glassfish is new for me.

Greetings
André
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42042
    
  64
I'd start by finding out if putHtml is called; a well-placed System.out.println statement will tell you that (it should go to some GlassFish log file).

You can do HTTP monitoring by using a proxy like TcpMon, or a tool like WireShark.
Andre Kuntze
Greenhorn

Joined: Jun 29, 2011
Posts: 18
Hi,

I will try that when I am back home. Thanks for the advice.

Greetings
André
Andre Kuntze
Greenhorn

Joined: Jun 29, 2011
Posts: 18
Hi,

as you suggested I added some Sysouts to the code so I can see which methods are called. That gives me:

HelloAndroid.java




and HelloWorld.java:



Result: I can now see in the protocols on the Admin Console that HTTP GET was executing but HTTP PUT is still missing. I attached two screenshots showing that the method on the Android was executed but that only a GET request send from the browser shows up in the logs.

I installed wireshark. It looks really interesting but I am new to it and it gives me way too much information so I am having a hard time filtering and looking for the right stuff. I did not find the HTTP PUT but I am not sure if I am doing it right. Could you point me to the proper way of filtering so it only shows the java HTTP PUT if it is there? Maybe you know how to filter that and can give me an advice? That would be highly appreciated as I tinkered around with it for an hour but could not get the results I wanted.

Altogether I think the call from the Android is not reaching the server at all but I don't get why Would be nice if you could give me some more hints?

Greetings
André



[Thumbnail for Admin Console.png]


[Thumbnail for Android.png]

Andre Kuntze
Greenhorn

Joined: Jun 29, 2011
Posts: 18
Watching the wireshark introduction from their site right now. Hope to understand the filtering soon.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42042
    
  64
Thinking some more about, I'm not actually sure if Wireshark would be able to see HTTP requests between two processes on the same machine. Were you able to see the GET request? If not, I apologize for sending you on a wild goose chase.

What should definitely work is TCPMon. If you run that, say, listening on port 7070 and forwarding to port 8080, it will log all HTTP conversations going on. You'd just have to use port 7070 in your Android code.
Andre Kuntze
Greenhorn

Joined: Jun 29, 2011
Posts: 18
Discussing with some firends of mine I got the same idea about the http request and Wireshark today. I will try tcpmon also. But before that: I am on Windows 7 and did not check the firewall so far - I did not install one but forgot that there is one from Microsoft already installed which may block the connection.

I will post the results later.

Thanks for clearing things up as I already started to run in the wrong direction
André
Andre Kuntze
Greenhorn

Joined: Jun 29, 2011
Posts: 18
Deactivating the Windows Firewall did not do the trick. Will try TCPMon now. Reporting back soon...

Greetings
André
Andre Kuntze
Greenhorn

Joined: Jun 29, 2011
Posts: 18
Hi Ulf,

I tried TCP Mon and it works better for me than wireshark - even if I still will inspect wirshark as it looks pretty interesting.

TCPMon does not show what I expected: I used port 7070 and when I so the HTTP GET with http://localhost:7070/HelloWorldApplication/resources/helloworld it shows up niccely - see screenshot.

Executing the Android code gives unexpected results:



This results in an empty request followed by an empty response if I interpret TCPMon right - see screenshot #2.

Any idea why that is?

Thanks for the help so far
André


[Thumbnail for HTTP Get is working.png]

[Thumbnail for HTTP Put is not working.png]

Andre Kuntze
Greenhorn

Joined: Jun 29, 2011
Posts: 18
TCPMon with a deactivated firewall gives the same results. So I exclude the firewall as a posible reason.
Andre Kuntze
Greenhorn

Joined: Jun 29, 2011
Posts: 18
Hi,

I investigated the problem a bit further and decided to try the different HTTP methods:

First I tried "GET" and changed HelloAndroid.java:



The results are documented in the atttachments. This works and shows up in the log files.

I have some more attachments so I open another reply for further information.

Greetings
André



[Thumbnail for HTTP GET with Android - TCPMon.png]


[Thumbnail for HTTP GET with Android.png]

Andre Kuntze
Greenhorn

Joined: Jun 29, 2011
Posts: 18
I finally tried the HTTP POST method:

HelloAndroid.java




HelloWorld.java



So, this gives a "204" message but finally shows up in the logs - see attachment. BUT: The GET method through the browser still gives me the "Hello, World false" content - you can see that in the logs attachment also - and I don't get why!?

Can you help? The Post method was called and the variables changed. Any ideas about that? Do I have to write a bean for that or something? Or do I have to save that in a special way - through a ressource on the website - as this is a Rest Webservice? Really like to understand even if Imay get the code up and running now.

Do you think the POST method did not work because the ressource was already there? I read it is only for not existing stuff.

Greetings - and thanks so far as I think I learned a lot through that already... but still have a long way to go...
André


[Thumbnail for HTTP POST with Android at Logs.png]

[Thumbnail for HTTP POST with Android at TCPMon.png]

[Thumbnail for HTTP POST with Android.png]

Andre Kuntze
Greenhorn

Joined: Jun 29, 2011
Posts: 18
Hey,

found the mistake about the PUT method - I always called the GET through the browser to test that the application is running... In my little world i understand: It created the ressource so PUT makes no sense anymore and wassn't called on the server.

Did I interpret that right?

Still I don't get why GET is not returning the changed state when ccalling PUT or POST - hope to figure that out soon. Maybe a little hint?

Greetings
André
Andre Kuntze
Greenhorn

Joined: Jun 29, 2011
Posts: 18
It was a persistence problem as I did not create any EJB or similar to keep the data. So it always shows false instead of true.

Greetings
André
 
GeeCON Prague 2014
 
subject: Connecting to local Web Service from Android emulator does not work