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();
}
}
}