BufferedReader readLine 未使用 HttpURLConnection 到达流的末尾
BufferedReader readLine not reaching end of stream using HttpURLConnection
我正在尝试从网站下载 html,我尝试了两种不同的方法。每次 InputStreamReader 或 BufferReader 都停在同一个地方,我不明白为什么。
IDE: Android 工作室使用 API 28
public class MainActivity extends AppCompatActivity {
String html;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
html = getTheHtml("http://www.posh24.se/kandisar");
Log.i("html: ", "" + html);
Log.i("Length", "" + html.length());
}
public class DownloadTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
// String result = null;
URL url;
HttpURLConnection connection = null;
StringBuilder response = null;
try {
url = new URL(urls[0]);
connection = (HttpURLConnection) url.openConnection();
int responseCode = connection.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK){
BufferedReader br = new BufferedReader((new InputStreamReader(connection.getInputStream())));
response = new StringBuilder();
String inputLine;
while((inputLine = br.readLine())!= null) {
response.append(inputLine);
response.append("\n");
}
br.close();
}
return response.toString();
// InputStream in = connection.getInputStream();
// InputStreamReader reader = new InputStreamReader(in);
// int data = reader.read();
//
// while(data != -1){
// char current = (char) data;
// result += current;
// data = reader.read();
// }
// return result;
} catch (Exception e) {
e.printStackTrace();
return "Failed";
}
}
}
public String getTheHtml (String url){
String result = null;
DownloadTask task = new DownloadTask();
try {
result = task.execute(url).get();
return result;
} catch (Exception e) {
e.printStackTrace();
return "Failed";
}
}
}
这是我的 Logcat 在流结束前突然停止的两行开始:
<div class="title">Lista:</div>
<div class
06-14 01:02:20.745 10602-10602/com.example.heato.guessthatcelebrity I/Length: 55847
06-14 01:02:20.769 10602-10602/com.example.heato.guessthatcelebrity D/OpenGLRenderer: Skia GL Pipeline
06-14 01:02:20.843 10602-10629/com.example.heato.guessthatcelebrity I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
06-14 01:02:20.844 10602-10629/com.example.heato.guessthatcelebrity I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
06-14 01:02:20.844 10602-10629/com.example.heato.guessthatcelebrity I/OpenGLRenderer: Initialized EGL, version 1.4
06-14 01:02:20.844 10602-10629/com.example.heato.guessthatcelebrity D/OpenGLRenderer: Swap behavior 1
06-14 01:02:20.844 10602-10629/com.example.heato.guessthatcelebrity W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
06-14 01:02:20.844 10602-10629/com.example.heato.guessthatcelebrity D/OpenGLRenderer: Swap behavior 0
06-14 01:02:20.853 10602-10629/com.example.heato.guessthatcelebrity D/EGL_emulation: eglCreateContext: 0xeadc0920: maj 3 min 1 rcv 4
06-14 01:02:20.890 10602-10629/com.example.heato.guessthatcelebrity D/EGL_emulation: eglMakeCurrent: 0xeadc0920: ver 3 1 (tinfo 0xe3695500)
06-14 01:02:20.891 10602-10629/com.example.heato.guessthatcelebrity E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
glUtilsParamSize: unknow param 0x000082da
06-14 01:02:20.972 10602-10629/com.example.heato.guessthatcelebrity D/EGL_emulation: eglMakeCurrent: 0xeadc0920: ver 3 1 (tinfo 0xe3695500)
我在 google 上搜索了又搜索,直到我的 googler 感到很痛...任何帮助将不胜感激。是的,这是我正在尝试遵循的教程,我正在尽最大努力不让我对看似无法解释的错误的焦虑和不安全感阻止我学习如何编程!!
尝试记录异常:
Log.e(MainActivity.class.getSimpleName(), "Request failed", e);
printStackTrace
的输出未在任何地方捕获。
因为logcat消息的长度有限,太长的消息会被截断。使用 adb logcat -g
:
检查限制
$ adb logcat -g
main: ring buffer is 256Kb (254Kb consumed), max entry is 5120b, max payload is 4068b
system: ring buffer is 256Kb (242Kb consumed), max entry is 5120b, max payload is 4068b
crash: ring buffer is 256Kb (3Kb consumed), max entry is 5120b, max payload is 4068b
在你的情况下,你的 html 字符串 (Log.i("Length", "" + html.length());
) 的长度似乎是正确的,所以问题只是关于 logcat 的输出,你可以检查完整的通过将字符串保存到文件中,或将字符串拆分成小片段来获取字符串。
result = task.execute(url).get();
使用 .get() 语句非常糟糕。
删除它。
您的结果将在 onPostExecute() 中可用,因此请在那里处理结果。
我正在尝试从网站下载 html,我尝试了两种不同的方法。每次 InputStreamReader 或 BufferReader 都停在同一个地方,我不明白为什么。
IDE: Android 工作室使用 API 28
public class MainActivity extends AppCompatActivity {
String html;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
html = getTheHtml("http://www.posh24.se/kandisar");
Log.i("html: ", "" + html);
Log.i("Length", "" + html.length());
}
public class DownloadTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
// String result = null;
URL url;
HttpURLConnection connection = null;
StringBuilder response = null;
try {
url = new URL(urls[0]);
connection = (HttpURLConnection) url.openConnection();
int responseCode = connection.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK){
BufferedReader br = new BufferedReader((new InputStreamReader(connection.getInputStream())));
response = new StringBuilder();
String inputLine;
while((inputLine = br.readLine())!= null) {
response.append(inputLine);
response.append("\n");
}
br.close();
}
return response.toString();
// InputStream in = connection.getInputStream();
// InputStreamReader reader = new InputStreamReader(in);
// int data = reader.read();
//
// while(data != -1){
// char current = (char) data;
// result += current;
// data = reader.read();
// }
// return result;
} catch (Exception e) {
e.printStackTrace();
return "Failed";
}
}
}
public String getTheHtml (String url){
String result = null;
DownloadTask task = new DownloadTask();
try {
result = task.execute(url).get();
return result;
} catch (Exception e) {
e.printStackTrace();
return "Failed";
}
}
}
这是我的 Logcat 在流结束前突然停止的两行开始:
<div class="title">Lista:</div>
<div class
06-14 01:02:20.745 10602-10602/com.example.heato.guessthatcelebrity I/Length: 55847
06-14 01:02:20.769 10602-10602/com.example.heato.guessthatcelebrity D/OpenGLRenderer: Skia GL Pipeline
06-14 01:02:20.843 10602-10629/com.example.heato.guessthatcelebrity I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
06-14 01:02:20.844 10602-10629/com.example.heato.guessthatcelebrity I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
06-14 01:02:20.844 10602-10629/com.example.heato.guessthatcelebrity I/OpenGLRenderer: Initialized EGL, version 1.4
06-14 01:02:20.844 10602-10629/com.example.heato.guessthatcelebrity D/OpenGLRenderer: Swap behavior 1
06-14 01:02:20.844 10602-10629/com.example.heato.guessthatcelebrity W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
06-14 01:02:20.844 10602-10629/com.example.heato.guessthatcelebrity D/OpenGLRenderer: Swap behavior 0
06-14 01:02:20.853 10602-10629/com.example.heato.guessthatcelebrity D/EGL_emulation: eglCreateContext: 0xeadc0920: maj 3 min 1 rcv 4
06-14 01:02:20.890 10602-10629/com.example.heato.guessthatcelebrity D/EGL_emulation: eglMakeCurrent: 0xeadc0920: ver 3 1 (tinfo 0xe3695500)
06-14 01:02:20.891 10602-10629/com.example.heato.guessthatcelebrity E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
glUtilsParamSize: unknow param 0x000082da
06-14 01:02:20.972 10602-10629/com.example.heato.guessthatcelebrity D/EGL_emulation: eglMakeCurrent: 0xeadc0920: ver 3 1 (tinfo 0xe3695500)
我在 google 上搜索了又搜索,直到我的 googler 感到很痛...任何帮助将不胜感激。是的,这是我正在尝试遵循的教程,我正在尽最大努力不让我对看似无法解释的错误的焦虑和不安全感阻止我学习如何编程!!
尝试记录异常:
Log.e(MainActivity.class.getSimpleName(), "Request failed", e);
printStackTrace
的输出未在任何地方捕获。
因为logcat消息的长度有限,太长的消息会被截断。使用 adb logcat -g
:
$ adb logcat -g
main: ring buffer is 256Kb (254Kb consumed), max entry is 5120b, max payload is 4068b
system: ring buffer is 256Kb (242Kb consumed), max entry is 5120b, max payload is 4068b
crash: ring buffer is 256Kb (3Kb consumed), max entry is 5120b, max payload is 4068b
在你的情况下,你的 html 字符串 (Log.i("Length", "" + html.length());
) 的长度似乎是正确的,所以问题只是关于 logcat 的输出,你可以检查完整的通过将字符串保存到文件中,或将字符串拆分成小片段来获取字符串。
result = task.execute(url).get();
使用 .get() 语句非常糟糕。
删除它。
您的结果将在 onPostExecute() 中可用,因此请在那里处理结果。