gpt4 book ai didi

android - JSoup 解析元素为空

转载 作者:行者123 更新时间:2023-11-30 01:40:32 28 4
gpt4 key购买 nike

我正在尝试使用 JSoup 解析一些数据,这一切都发生在我的 MainActivity 的异步任务 (doInBackground) 部分。不幸的是,当我执行应用程序时,所有元素 (9) 都是空的。

当我在代码线下方进行调试时,我实际上得到了完整 网站,它就在那里。readMultipleLinesRespone() 方法位于另一个类 HttpUtility 中,我也在这里调用我的 Post 和 Get 请求。

我通过将网站保存为文件并使用 JSoups assets 功能预先测试了这一点,然后它运行完美。

onPostExecute 中的 setupAdapter() 方法用数据填充 ExpandableListview,如果此信息是必需的。如果您需要更多信息,请询问。

有人可以协助并告诉我我做错了什么吗?

response1 = util.readMultipleLinesRespone(); <--- debugged and all data (seems) to be there but isn`t.

编辑:如果我打印response1,确实没有数据要解析。日志输出:

E/Resonse:: [Ljava.lang.String;@3d3410a

下面是 HttpUtility 类的 readMultipleLinesRespone 方法:

public String[] readMultipleLinesRespone() throws IOException {
InputStream inputStream = null;
if (httpConn != null) {
inputStream = httpConn.getInputStream();
} else {
throw new IOException("Connection is not established.");
}

BufferedReader reader = new BufferedReader(new InputStreamReader(
inputStream));
List<String> response = new ArrayList<String>();

String line = "";
while ((line = reader.readLine()) != null) {
response.add(line);
}
reader.close();

return (String[]) response.toArray(new String[0]);
}

一切都在发生的异步任务:

private class FetchWebsiteData extends AsyncTask<Void, Void, Void> {
ProgressDialog mProgressDialog;

@Override
protected void onPreExecute() {
this.mProgressDialog = new ProgressDialog(getActivity());
mProgressDialog.setMessage("Laden...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}

@Override
protected Void doInBackground(Void... result) {

try {
util.sendGetRequest("https://mobile.somesite.nl/Data", null);
response1 = util.readMultipleLinesRespone();
} catch (IOException e) {
e.printStackTrace();
}

if (response1.length > 0) {
Document doc = Jsoup.parse(response1.toString());

// Get the html document title
Elements els = doc.select("span[class=item-value pull-right]");

if (els.size() > 0) {
fac_naam = els.get(0).text();
fac_straat = els.get(1).text();
fac_post = els.get(2).text();
con_tel = els.get(3).text();
con_email = els.get(4).text();
betaal_reknr = els.get(5).text();
betaal_houd = els.get(6).text();
zig_gebruiker = els.get(7).text();
zig_wacht = els.get(8).text();
}
}
return null;
}

@Override
protected void onPostExecute(Void result) {
super.onPreExecute();
setupAdapter();
mProgressDialog.dismiss();
}
}

最佳答案

同时我解决了这个问题。我没有将响应字符串正确传递给解析所需元素的异步任务。只需要一个公共(public)字符串,其中设置和传递响应(不是一种优雅的方式,但它有效):

public static String HttpResponse = "";

在 HttpUtility 类中:

public String[] readMultipleLinesRespone() throws IOException {
...

TabFragment1.HttpResponse = response.toString();
...
return (String[]) response.toArray(new String[0]);
}

然后将其传递给异步任务:

@Override
protected Void doInBackground(Void... result) {

try {
util.sendGetRequest(LoginActivity.PersData_URL, null);

util.readMultipleLinesRespone();

} catch (IOException e) {
e.printStackTrace();
}

if (HttpResponse.length() > 0) {
Document doc = Jsoup.parse(HttpResponse.toString());
// Get the html document title
Elements els = doc.select("span[class=item-value pull-right]");
...

}
return null;
}

关于android - JSoup 解析元素为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34570729/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com