Click here to Skip to main content
15,887,355 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a good web service running on my pc and an application on android just to call it and it is suppose to return a string. it works fine on the browser on my pc, but on the android it is returning null. I tried 2 examples and both returning null, and they are not interracting with the web service, as I am monitoring it.
both not interracting with the web service and both returning null.

Trial 1.

String line = null;
	 
	try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("Valid url");
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
line = EntityUtils.toString(httpEntity);
Displaymsg(line);
	}
	catch(Exception ex)
	{
		Displaymsg(ex.getMessage());
	}



Trial 2.

try {
         HttpClient httpclient = new DefaultHttpClient();
         HttpPost post = new HttpPost("Valid url");
         
         HttpResponse response = httpclient.execute(post);
         HttpEntity entity = response.getEntity();
         Returned = EntityUtils.toString(entity);
         Toast.makeText(this, Returned, Toast.LENGTH_LONG).show();
       } catch ( IOException ioe ) {
        ioe.printStackTrace();
       }



Any help pls?

update: is it maybe because it uses honeycomb os? i've read it needs an asynctask extend but i can't find a good example to help me. Any one maybe used it before?
Posted
Updated 20-Dec-11 0:12am
v3

1 solution

Is it also returning null in the android web browser? I think you need to open the port on your pc that is hosting the service. Ports are normally closed for incoming messages. You can search google on where and how to do that for your windows version.

Good luck!
 
Share this answer
 
Comments
adnama 6-Dec-11 5:05am    
Yes it is null from the android web browser but even when i use local host on the simulator. it only works when i use local host on my pc web browser and when i use 127.0.0.1
E.F. Nijboer 8-Dec-11 15:16pm    
127.0.0.1 is looping back to the pc/device itself (it is also known as localhost), meaning that 127.0.0.1 will loop back to pc1 on pc1 and to pc2 on pc2. (http://en.wikipedia.org/wiki/Localhost)
You need to check your network connection information to see what your ip address is to the outside world. Probably starts with 192.168.x.x meaning that you are behind a NAT (http://en.wikipedia.org/wiki/Network_address_translation). This means that only a device behind the same NAT (both use your home router for example) can connect (to overcome this you would need to configure port forwarding in your router). But remember, you still need to configure your pc to allow incoming connections.
TorstenH. 7-Dec-11 2:08am    
which port and which IP are you using? both valid? please don't use small port numbers, go above 10.000 to be sure it is not used from another app.
Can you ping the other device?
adnama 20-Dec-11 4:41am    
I changed it to the ip address of the device, and from the browser of the device it is working and displaying the message that is suppose to display so it's working. but from the program it is still returning null.
TorstenH. 20-Dec-11 10:02am    
so it works. Sometimes there is a problem with the Loopback-Adapter in Windows. Please check if you can ping your own machine on 127.0.0.1 and localhost.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900