0

I am developing an android app which whould take the source code from a webpage and then parse it.

I am able to get this to work as long as my device is connected to the pc on which it is developed, however, when I try to run it disconnected from the pc it seems that it is unable to retrieve information. Can somebody please tell me what I am doing wrong?

To get the source code I use the following code via an AsyncTask:

private String downloadUrl(String myurl) throws IOException {
    InputStream is = null;

    try {
        URL url                 = new URL(myurl);
        HttpURLConnection conn  = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(20000 /* milliseconds */);
        conn.setConnectTimeout(25000 /* milliseconds */);
        conn.setRequestMethod("GET");
        conn.setDoInput(true);

        // Starts the query
        conn.connect();
        response = conn.getResponseCode();
        Log.d(DEBUG_TAG, "The response is: " + response);

        if (response == 200) {

            is = conn.getInputStream();

            BufferedReader buffer = new BufferedReader(new InputStreamReader(is, "ISO-8859-1"));

            // Convert the InputStream into a string
            String s = "";
            while ((s = buffer.readLine()) != null) {
                myTextView.append(s);
                myTextView.append("\n");

            }

            conn.disconnect();
            is.close();
            return myTextView.getText().toString();
        } else {
            conn.disconnect();
            return "";
        }


    } finally {
        if (is != null) {
            is.close();

        } 
    }
}
Nick
  • 3,143
  • 20
  • 34
  • 1
    Dont know if it will help but you don't have to call `connect()` once you have called `openConnection()` – Nicolas Dusart Apr 11 '13 at 09:21
  • @Haelty Do I need to close the connection as I did? I have tried to run it in release mode as well, but the phone just won't make the connection. – Nick Apr 11 '13 at 09:46
  • Yes, it's better to close the connection ;) But you could call it in the `finally` block so that it will be called in any case (even if `return` is called before). – Nicolas Dusart Apr 11 '13 at 09:49
  • @Haelty No I can't because conn is defined within the try block – Nick Apr 11 '13 at 09:51
  • You're right. So keep it as it is. But what happens? Does it throw an exception or it returns an empty string ? What's the response code ? – Nicolas Dusart Apr 11 '13 at 09:57
  • I don't know, because I don't have the device connected, so I can't see the debug information. onPostExecute should make a toast displaying the response, but it seems that it is not reached when the device is not connected. – Nick Apr 11 '13 at 09:59
  • If you plug your device after, you should see all the logs from logcat. Add some logs in your functions to determine what's going on. – Nicolas Dusart Apr 11 '13 at 10:20

1 Answers1

1

It seemed that in my call to the ASyncTask the following line was keeping the device to wait for a computer connection.

android.os.Debug.waitForDebugger();
Nick
  • 3,143
  • 20
  • 34
  • What's the point of calling that ? In debug mode, the application isn't already waiting for the debugger ? :P – Nicolas Dusart Apr 11 '13 at 11:15
  • No it won't stop in the code called by a AsyncTask, as it is on a different thread, see [this answer](http://stackoverflow.com/a/4770967/1620085). – Nick Apr 11 '13 at 11:28