I'm trying to test an Android aplication that uses webservice to access localhost.
My device ip is 192.168.0.21 and my localhost ip is 192.168.0.22.
I tried this:
HttpsURLConnection conn;
Url url = new URL("http://192.168.0.22:80/admapp/adm_service.php?");
conn = (HttpsURLConnection) url.openConnection();
It does not work. I've tried to disable the firewall and nothing.
I'm using EasyPHP, but I think that this don't matter.
Could anyone help me?
It should work if you map your IP to localhost in hosts file. The file will be at C:\Windows\System32\drivers\etc\hosts in windows, /etc/hosts in Linux.
Related
I use the INTERNET uses-permission in the manifest file.
If I type URL url = new URL('http://www.example.com/page') (remote server) it will work.
But if I type URL url = new URL('http://example.local/page') (local server) it will return the error
java.net.UnknownHostException: Unable to resolve host "example.local": No address associated with hostname
Now in %WINDIR%/System32/drivers/etc/hosts I wrote this entry :
10.101.0.179 example.local
I can't use new URL('http://10.101.0.179/page') because example.local is a virtualHost in my apache configuration.
Can't my AVD looks into the hosts file ? what should I do to tell android to fetch the ip 10.101.0.179 but still send a proper information to apache and tell it to serve the virtual host example.local ?
If you try to request a web-page from your local HTTP server and the page is bounded to a virtualHost, so let say you need to reach http://example.local/mypage and if as pointed by #CommonsWare you don't use mDNS to resolve inter-local domain-names, android will probably throw the same error message as it reads in the title of this question. Here's the solution I am using :
let's say http://example.local/mypage refers to the ip address 10.101.0.179, Android won't be able to resolve example.local, you need to write the raw url (of your actual local server)
try {
...
URL url = new URL("http://10.101.0.179/mypage");
HttpUrlConnection conn = (HttpUrlConnection) url.openConnection();
...
}
...
But now when Apache receives this request, unless serving your desired host, 10.101.0.179 won't be understood in case of a VirtualHost. To round-about this problem, just modify the Host http-header property using setRequestProperty :
...
conn.setRequestProperty("Host", "example.local");
...
And it should work.
First you must check if you have given INTERNET permission in your manifest file or not, second you make sure that your emulator or computer has a valid internet. If it doesn't work than you can do following:
If you are using localhost url than you must convert that url into IP address to work with App specially using Android emulator.
For example :
My Current url is
http://mylocaladdress:9090/myapp.svc/ForgetPassword?EmailID=test#test.com
Now you can put the following command into terminal on mac or you can use cmd on windows
ping mylocaladdress
The result will give u the IP address and my final url becomes
http://10.1.2.111:9090/myapp.svc/ForgetPassword?EmailID=test#test.com
This url worked perfectly in android emulator without exception.
I need to connect my app to the wamp server located in my machine within my LAN, in order to test an api I am currently developing. While using the phone internet browser, I can access the ip address for api viewing, but using something similar to the following code does not work.
URL url = new URL("192.168.1.1/api/v1/home");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
Please give some suggestions in order to be able to test this.
Try adding http before the link
URL url = new URL("http://192.168.1.1/api/v1/home");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
Are you using the emulator? If so change your IP to this: http://10.0.2.2
I'm developing an App and have designed a bit of Frontend and backend, just the essential. And now, I want to connect both sides. First of all, I need to know how to connect to a localhost in Android.
I tried some tutorials in Internet using for example:
URL url = new URL("http://127.0.0.1");
HttpURLConnection urlConnection = (HttpURLConnection)
url.openConnection();
try {
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
readStream(in);
} finally {
urlConnection.disconnect();
}
I've tried several approaches but all of them fail. Some doesn't do anything and others stop the App showing some Zygote errors.
My question is clear. How can I connect to localhost in Android? I want a function of the style makeConnectionLocalHost() that returns whether the connection has been successful or not. Any idea why nothing works?
Try instead of using "localhost" your local ip. To retrieve it, open CMD and type ipconfig. There you will find your local ip.
127.0.0.1 will create a loop back to your own device.
You can use InetAddress.getLocalHost() to get localhost address. More details here. And for HttpURLConnection you should have Web-server on this address.
Hi all I am trying to send data to my development database from android application using Post but am getting exception
09-18 15:23:36.801: W/System.err(6755): java.io.FileNotFoundException: http://10.255.162.84:8080/web/events
09-18 15:23:36.801: W/System.err(6755): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:1162)
If I copy paste the url the web browser in my computer. the webpage I am trying to get works.
If I replace the url with https:www.google.com in my application, it works.
What does this mean? I can't connect make http connection without a host name?
code
URL url = new URL("http://10.255.162.84:8080/web/events");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setReadTimeout(20000 /*millis*/);
conn.setConnectTimeout(15000 /*millis*/);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.connect();
System.out.println(conn.getResponseCode());
InputStream is = conn.getInputStream();
Reader reader = new InputStreamReader(is, "UTF-8");
char[] buffer = new char[500];
reader.read(buffer);
System.out.println(new String(buffer));
Additional information
The server to receive get and post requests is running on my PC port 8080 via a broadband connection.
I am running the application on my low end phone running android 2.2 (Froyo)
I hope that's all the relevant information but am glad to provide more. I know you get this all the time but I swear to you I am really new to android
I'm guessing your android device is not on the same network as your computer. You can test this easily by pasting the url into an android browser and checking to see if it works. Your server is most likely behind your network's firewall. You'll need to configure port forwarding to allow external clients to connect.
When uploading the file from my emulators sdcard to the local wampserver I got this error.
03-22 10:46:15.459: ERROR/Debug(487): error: localhost/127.0.0.1:80 - Connection refused
sample code
String urlString = "http://localhost/uploads/index.php";
try
{
//------------------ CLIENT REQUEST
FileInputStream fileInputStream = new FileInputStream(new File(existingFileName));
// open a URL connection to the Servlet
URL url = new URL(urlString);
Can anybody tell me what this error means and what I have to do to avoid it?
Your issue is likely caused by the fact that the localhost name you are pointing at resides on the device and is not the host computer where the server is.
A way you could remedy this is to use the host pc's actual IP address.
You must replace "localhost" with "10.0.2.2".