Android - HttpPost works on emulator, but not on my phone - android

I'm sending a POST to my development machine and it works with 10.0.2.2 in the emulator, but I can't get it to work when installed on my phone - the connection always times out. I tried 10.0.2.2, 192.168.1.3 (my computer) and my public IP..
I turned off the firewall and I tried forwarding port 8000 which is the port my web server is listening on.
Which IP should it be? Is port forwarding what I need to do? I have no clue how routers and my internal network work.

As you are using the same connection for your pc as well as emulator so as a result you can browse 10.0.2.2(aka localhost)
But when you are deploying it in a real handset you are facing the problem.
Make sure that your device and server share the same connection.
Use WIFI so that your device can detect 192.168.1.3
Hope it will help you

Related

How to connect to a local server using windows hosted network

I am programming an Android app which connects to a TCP server running on my computer using sockets. This works perfectly fine as long as both my computer and my smartphone are connected to the Wi-Fi of my router.
I want it to also work when my computer and my phone are not connected to the Wi-Fi. Therefore I configured a Wi-Fi hotspot using hosted network on my computer. My smartphone recognizes the Wi-Fi and is able to connect to it. But now my app can't connect to the server running on my PC although I changed the code to the new IP that I got by using ipconfig on my PC.
I downloaded an app named "Fing" from the Google play store. The app shows all devices in a network. It does show my computer under the right IP address. I don't understand why my app can't connect to the server running on the PC.
On the one hand, I don't think that the problem is in the app's code, because it works on the router's Wi-Fi network. On the other hand, I doesn't seem to be a setup mistake either, because Fing is detecting my PC.
Do you have any ideas what the problem might be, or any other solution for my needs?
I actually wrote the TCP server myself using c#. I configured the Server to listen to all of my computers networkinterfaces usingIPAdress.IPv6Any. I also did configure it as dual mode socket so it can accept ipv4 and ipv6 connections. I will post the code when i am back home. So the TCP server also looks ok to me.

How to communicate between computer and smartphone over wifi

First, my goal is to access from chrome on my HTC one X, to my local server on my laptop.
The local server works (wamp), and runs from 127.0.0.1.
I can't depend on outer network, thus I want to create a local wifi network.
Currently I turned on Wi-Fi Hotspot on my phone. The laptop successfully connected to the network, and phone actually recognized it.
The IP of the laptop is 192.168.1.150 (I found this both from the laptop and the phone).
My problem is that in the computer, when I try to access 192.168.1.150:80 I get 403 Forbidden, and I get nothing on my phone (I even tried to send packets with no success).
Finally found the solution!
http://www.mobitechie.com/android-2/how-to-access-localhost-on-android-over-wifi/
This tutorial explained each and every problem I had (how to fix 403 error and open the relevant port in the firewall).

Using Socket to connect to Server in Pc from mobile

I wrote an app to connect to java server running in PC. It works fine in emulator but its not working when i try the same from my mobile. I used the Mobiles 3G network to connect to IP address.
(Based on question comment)
I assume that your server application is running on the same machine as the android emulator. In this case the emulator can reach your server with 10.0.2.2.
If you're trying to contact your server via mobile 3G you have to use the IP of your Internet connection (What is my IP?). The request will reach your router. The router has to forward this request to your server machine. So if your server has a local IP 192.168.0.10 and it listens to port 2412 your router should forward all incoming request for this TCP port to this IP.
You should allow the 3G internet in Settings. (Emulator doesn't need this, it uses ethernet connection of your PC).
Your application should diagnose the state and possibilities of Internet connection and report it to you as a user of the debug mode. Or/and something - to the usual user.

Android HttpClient sending request to host machine over USB

I have an Android app running on a phone that's connected to a host machine over USB. The phone will always be connected to the machine and I need to go an HTTP request to the machine. Working in the emulator, I can access the webserver running on the computer using the IP address of 10.0.2.2 and that works fine. However running the same app on the phone, I get a SocketException of Network unreachable. The phone doesn't have a SIM card in it, nor does it have a wifi connection, I need the HTTP request to just go over USB to the computer. Is this at all possible? If so, what am I doing wrong.
I'm using standard HttpClient code, nothing special, which works absolutely fine if I run it on the phone using a wifi connection, so I know that's not the problem.
I ended up abandoning this and basically polling the phone for the new content I wanted from the computer using adb.
Reverse Tethering! not sure if it works. I never tried it. This thread might help:
https://android.stackexchange.com/questions/2298/how-to-set-up-reverse-tethering-over-usb
This Android app + Windows host app should do the trick:
http://www.codeproject.com/KB/android/UsbPortForwarding.aspx

Android network addresses

I have a tomcat web server running on my local machine. I also have a HTC desire and a Galaxy Tab. my problem is, I cannot connect from my real android device to my local machine. When I try to connect using the emulator, everything's fine. Emulator works with communication to the address:
10.0.2.2, but the real devices don't
Does someone know the solution, or why there's the problem?
You have to get the exposed IP address of your local machine and connect to that. If you are behind a router, you will have to make sure that it allows the packets to pass.
Are you using network or WiFi? You should be on WiFi and connecting through the same router to have 10.0.2.2 accessible.
Also make sure you have your local machine's incoming port (I'm assuming 80) open and reachable from the outside.

Categories

Resources