views:

2190

answers:

2

I am trying to connect a web server I have running in my android App however for some reason it's failing.

I can connect to the webpage normally and using my phone's browser, I can also use the below code to connect to google but for some reason my code will not connect to my web server. If I host the server on port 80 I get error code 503 every time, if I host it on 8080 (which is preferable) I get a timeout exception which is below.

03-05 20:12:22.432: WARN/System.err(29254): java.net.SocketException: The operation timed out
03-05 20:12:22.602: WARN/System.err(29254):     at org.apache.harmony.luni.platform.OSNetworkSystem.connectSocketImpl(Native Method)
03-05 20:12:22.602: WARN/System.err(29254):     at org.apache.harmony.luni.platform.OSNetworkSystem.connect(OSNetworkSystem.java:125)
03-05 20:12:22.602: WARN/System.err(29254):     at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:227)
03-05 20:12:22.612: WARN/System.err(29254):     at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:521)
03-05 20:12:22.612: WARN/System.err(29254):     at java.net.Socket.connect(Socket.java:1019)
03-05 20:12:22.612: WARN/System.err(29254):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:67)
03-05 20:12:22.612: WARN/System.err(29254):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionManager$ConnectionPool.getHttpConnection(HttpConnectionManager.java:151)
03-05 20:12:22.612: WARN/System.err(29254):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionManager.getConnection(HttpConnectionManager.java:73)
03-05 20:12:22.612: WARN/System.err(29254):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getHTTPConnection(HttpURLConnection.java:826)
03-05 20:12:22.612: WARN/System.err(29254):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:812)
03-05 20:12:22.612: WARN/System.err(29254):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getResponseCode(HttpURLConnection.java:1275)
03-05 20:12:22.612: WARN/System.err(29254):     at com.project.library.remailer.ServerSelect.downloadDirectory(ServerSelect.java:84)
03-05 20:12:22.622: WARN/System.err(29254):     at com.project.main.NewMessage$3$1$1.run(NewMessage.java:156)

The code is:

try {
    URL url = new URL(directoryLocation);
    HttpURLConnection httpURLConnection = (HttpURLConnection) url
            .openConnection();

    // The line below is where the exception is called
    int response = httpURLConnection.getResponseCode();                 
    if (response == HttpURLConnection.HTTP_OK) {


        // Response successful
    InputStream inputStream = httpURLConnection.getInputStream();

    // Parse it line by line
    BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(inputStream));
    String temp;
    while ((temp = bufferedReader.readLine()) != null) {
        // Parsing of data here
    }
       } else {
            Log.e("SAMES", "INCORRECT RETURN CODE: " + response);
       }
} catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

When I try the code in the link suggested by CommonsWare for port 8080 I get the same error as before, the timeout exception. For port 80 I get the following exception, both of these errors occur on the url.openStream().

Port 80 exception:

03-06 11:53:53.296: WARN/System.err(639): java.io.FileNotFoundException: http://64.197.194.165:80/path
03-06 11:53:53.376: WARN/System.err(639):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1064)
03-06 11:53:53.376: WARN/System.err(639):     at java.net.URL.openStream(URL.java:674)
03-06 11:53:53.376: WARN/System.err(639):     at com.project.library.remailer.ServerSelect.downloadDirectory(ServerSelect.java:118)
03-06 11:53:53.376: WARN/System.err(639):     at com.project.main.NewMessage$3$1$1.run(NewMessage.java:156)

I also tried using the following code:

HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(directoryLocation);
        try
        {
            HttpResponse response = client.execute(request);
            BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            String line;
            while((line = reader.readLine()) != null) {
                Log.e("SAMES", line);
        }

    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

However that has resulted in the same timeout error for port 8080, which occurs when client.execute(response) is called.

For port 80 I get a webpage which says that it couldn't connect, which alot of HTML editing, however the main thing it says on the page is:

03-06 13:10:23.576: ERROR/SAMES(1267): The Web Server may be down, too busy, or experiencing other problems preventing it from responding to requests. You may wish to try again at a later time.

A: 

What happens if you try calling httpURLConnection.connect() after calling url.openConnection()?

Daniel Lew
I tried this and here are the results.When I try to connect to port 80 on the server I get to the getResponse90 function call then the 503 error.When I try with port 8080 I get the Timeout exception about at the connect() function call
David Read
A: 

Various notes:

  • You don't indicate where the timeout occurred.
  • You might try this example, which uses URL differently than you are.
  • Your error code 503 on port 80 should result in some error information on your server that might be useful.
  • Are there any proxy servers or other things between your phone and the Web server?
  • Have you tried with both WiFi and 3G?
  • You might try switching to HttpClient and see if you get better results or perhaps a better error message.
CommonsWare
Appended the above code with my response for trying the new code. On the web server it's meant to print a response when a connection comes through, however it doesn't say anyone is connecting for any of the above attempts. I will test HttpClient in a bit and get back with the results from that. Also I only have access to 3G atm and testing on Wifi will prove to be difficult. As far as I know there are no proxy servers in between my phone and the webserver either.
David Read
Your carrier may well have proxy servers between your phone and your server. Conceivably, they may not be supporting port 8080, though I would find that somewhat unusual. Particularly for your port 80 test, see if your server actually registers a hit. FWIW, I would recommend the ResponseHandler pattern for text responses with HttpClient.
CommonsWare
I don't think there is a proxy in the way as when I use the phone's normal browser I can access the web page regardless of the port. So there must be something wrong with my code.
David Read