我正在开发一个Android应用程序,它可以从网页上获取源代码,然后解析它。
只要我的设备连接到开发它的电脑上,我就可以使用它,但是,当我尝试从电脑上断开连接时,它似乎无法检索信息。有人可以告诉我我做错了吗?
要获取源代码,我通过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();
}
}
}
答案 0 :(得分:1)
似乎在我对ASyncTask的调用中,以下行是让设备等待计算机连接。
android.os.Debug.waitForDebugger();