Java BufferedReader readLine未使用HttpURLConnection到达流的结尾
我试图从一个网站下载html,我尝试了两种不同的方法。每次InputStreamReader或BufferReader停在同一个位置,我都不知道为什么 IDE:Android Studio使用API 28Java BufferedReader readLine未使用HttpURLConnection到达流的结尾,java,android,httpurlconnection,bufferedreader,Java,Android,Httpurlconnection,Bufferedreader,我试图从一个网站下载html,我尝试了两种不同的方法。每次InputStreamReader或BufferReader停在同一个位置,我都不知道为什么 IDE:Android Studio使用API 28 public class MainActivity extends AppCompatActivity { String html; @Override protected void onCreate(Bundle savedInstanceState) { super.onCr
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";
}
}
}
public类MainActivity扩展了AppCompatActivity{
字符串html;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
html=getTheHtml(“http://www.posh24.se/kandisar");
Log.i(“html:”,“+html”);
Log.i(“长度”,“html.Length());
}
公共类DownloadTask扩展了AsyncTask{
@凌驾
受保护的字符串doInBackground(字符串…URL){
//字符串结果=null;
网址;
HttpURLConnection=null;
StringBuilder响应=null;
试一试{
url=新url(url[0]);
connection=(HttpURLConnection)url.openConnection();
int responseCode=connection.getResponseCode();
if(responseCode==HttpURLConnection.HTTP\u确定){
BufferedReader br=新的BufferedReader((新的InputStreamReader(connection.getInputStream()));
响应=新的StringBuilder();
字符串输入线;
而((inputLine=br.readLine())!=null){
追加(inputLine);
响应。追加(“\n”);
}
br.close();
}
返回response.toString();
//InputStream in=connection.getInputStream();
//InputStreamReader reader=新的InputStreamReader(in);
//int data=reader.read();
//
//while(数据!=-1){
//当前字符=(字符)数据;
//结果+=电流;
//data=reader.read();
// }
//返回结果;
}捕获(例外e){
e、 printStackTrace();
返回“失败”;
}
}
}
公共字符串获取HTML(字符串url){
字符串结果=null;
DownloadTask任务=新建DownloadTask();
试一试{
结果=task.execute(url.get();
返回结果;
}捕获(例外e){
e、 printStackTrace();
返回“失败”;
}
}
}
这是我的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)
Lista:
尝试记录异常:
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的输出,您可以通过将字符串保存到文件中来检查完整的字符串,或者将字符串分割成小块
结果=task.execute(url.get()
使用.get()语句非常糟糕
移除它
您的结果将在onPostExecute()中可用,因此在那里处理结果。您从未设置请求方法。默认值是GET。您尝试访问的端点是GET端点还是POST端点?此外,您是否尝试过调试应用程序代码?如果出现异常,那么其中的堆栈跟踪在这里可能会有所帮助。您在读取行时不会记录行,因此,
s可能记录的唯一方法是在读取循环退出后,这意味着您确实到达了流的末尾。请澄清。使用列出的url在doInBackground中运行代码。这对我有用。我只需要将InputStream包装在GzipInputstream中,因为内容编码是gzip。谢谢大家的帮助,我非常感谢。事实证明,缓冲读取器捕获了整个网页的html,但将其打印到logcat让我感到很不舒服,因为它截断了消息,使消息看起来好像在整个流之前就停止了。这是有用的信息,但作为注释可能会更好,因为这不是一个直接的回答。非常感谢!我继续执行我想要的功能,就好像这个流已经完成并且工作了一样。它只是截断了整个字符串,因为它太长了。