0

I want to see the exact headers my android app is sending while making a web request so I thought I'd simply create a simple server app in java on my local machine and have my android app make a call to it. Then simply dump the request to the console so I could see what the app is sending. However when I tried to connect, the app hangs and stops responding.

I created a simple server the only accepts a connection and sysouts the data it gets. The server runs fine and if I hit it from a web browser on my computer will print the headers from the web browsers request. So I know the server works fine.

Here's the code from my app:

URL url = new URL("http://192.168.1.11:9000");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.connect();

PrintWriter writer = new PrintWriter(connection.getOuputStream(), true);
writer.write("hi");
writer.close();

Simple. I only want the headers after all. Now I started without a post and using:

URL url = new URL("http://192.168.1.11:9000");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();

BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
in.close();

but that doesn't work. The app stops responding on the getInputStream() request. It just stops and won't continue. The server gets no connection request either.

So in all, the app is blocking on the url connection's getInputStream and I can't figure out why.

Now I've searched for awhile and found these:

Android app communicating with server via sockets

socket exception socket not connected android

Android embedded browser cant connect to server on LAN

Using java.net.URLConnection to fire and handle HTTP requests

Client Socket cannot connect to running Socket server

But nothing helps. I'm not using the localhost like everyone with this problem seems to be and I've tried using the androids 10.0.0.2 but that doesnt work either.

I'm not on a network that restricts anything (I'm home) and I've tried using the first set of code shown in order to send a message to my server but not even that works (it runs fine but the server never gets a client. Hows that work?).

I tried using both URLConnection and HttpURLConnection, they both have the same problem.

I'm also using the internet permission in my app, so it does have the permission needed.

I'm at a loss at this point. Why can't I make a simple call to my server?

EDIT

I used the exact code from androids documentation:

private String downloadUrl(String myurl) throws IOException {
    InputStream is = null;
    // Only display the first 500 characters of the retrieved
    // web page content.
    int len = 500;

    try {
        URL url = new URL("http://10.0.2.2:9000");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(10000 /* milliseconds */);
        conn.setConnectTimeout(15000 /* milliseconds */);
        conn.setRequestMethod("GET");
        conn.setDoInput(true);
        // Starts the query
        conn.connect();
        int response = conn.getResponseCode();
        is = conn.getInputStream();

        // Convert the InputStream into a string
        String contentAsString = readIt(is, len);
        return contentAsString;

        // Makes sure that the InputStream is closed after the app is
        // finished using it.
    } finally {
        if (is != null) {
            is.close();
        }
    }
}

but even that doesn't work. It still hangs. Only now it hangs on the getResponseCode(). Then throws a timeout exception. The server never gets a request though.

Community
  • 1
  • 1
Jeremy Styers
  • 497
  • 5
  • 23
  • I'm quite sure your logcat is telling you exactly what the problem is. – Brian Roach Jul 31 '13 at 00:52
  • logcat says nothing actually since the first set of code throws no erros. – Jeremy Styers Jul 31 '13 at 00:53
  • When you say you can access the web page through a web browser just fine, did you mean to say that you can access the web page just fine through the default web browser on your Android device? Or was the web browser used the one from one of your desktop computers? – Stephan Branczyk Jul 31 '13 at 00:57
  • I mean from my local computer. So if I say localhost:9000 or the apps ver "http://192.168.1.11:9000", it will work just fine. My app is what seems to have a problem connecting. If I use the first set and post, the code throws no errors and runs but the server gets no request or client. If I use the second set of code, the thing freezes and asks if I want to close it (no errors are thrown). – Jeremy Styers Jul 31 '13 at 01:00
  • You're getting the FC dialog because connection.getInputStream is a blocking call, which will block the UI thread. Android automatically FC's apps that block the UI thread, forcing developers to make sure their UIs remain responsive to user input. – berwyn Jul 31 '13 at 02:13

3 Answers3

0

Your address must start with 'http://", try again!

0

I think the root of your issue is that Android is FCing your app before the connection completes, because I assume you haven't wrapped this in a Loader, AsyncTask or Thread. I suggest you follow the training guide Google provides, wrapping your call in an AsyncTask and seeing if that corrects the issue.

berwyn
  • 986
  • 7
  • 23
  • I tried moving it to it's own thread but that didn't change anything except the ui not freezing. It still hangs on the getInputStream though, just it's hanging the tread instead of the app now. Any ideas why? – Jeremy Styers Jul 31 '13 at 16:06
0

I have a Java class I use for making HTTP GET requests, I'm guessing its near identical to the android code your using so below I've dumped the relevant part of the code. I've used this class many times in Java applications (not on Android).

currentUrl = new URL(getUrl);
conn = (HttpURLConnection)currentUrl.openConnection();
conn.setRequestProperty("Cookie",  getCookies(currentUrl.getHost()));
conn.setRequestProperty("User-Agent", "robadob.org/crawler");
if(referrer!=null){conn.setRequestProperty("Referrer",referrer);}
conn.setRequestMethod("GET");
conn.connect();
//Get response
String returnPage = "";
String line;
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = rd.readLine()) != null) {
    returnPage+=line+"\n";
}
rd.close();

I can't see anything obvious that would be causing your code to fail, but hopefully you can spot something from this. The setRequestProperty is me setting headers, so you shouldn't need those.

If that fails, flood your code with System.out's so you can see which statement its stalling at.

Robadob
  • 5,319
  • 2
  • 23
  • 32