简体   繁体   中英

Android how to make GET or POST Http Request using url with an IP address instead of HostName

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM