我是Jsoup的新手,我正在尝试使用以下html解析网站,并检索下面html中输入文本的值,特别是“value = 14”,然后我想显示该值(这个案例中的数字14)作为我的Android应用程序中文本视图中的字符串。我尝试了多种方法,但它没有用,我只是收到“null”。请举例说明。
<div id="PatientsCurrentlyInClinic" style="display: none"> <!-- Messages are shown when a link with these attributes are clicked: href="#messages" rel="modal" -->
<h3>Which clinic are you updating?</h3>
<form action="" method="get">
<p>
<select name="patientclinicid" id="patientclinicid"><option value="2" selected>Location Two</option><option value="1">Location One</option><option value="3">Location Three</option></select> </p>
<h4>How many patients are in the clinic?</h4>
<p>
To provide better service to your patients, please enter the current number of patients in your clinic.
</p>
<input class="text-input medium-input" type="text" id="small-input" name="patientsInClinic" value="14"/>
<p><input class="button" name="patients-clinic" type="submit" value="Update" /></p>
</form>
</div> <!-- End #messages -->
我的尝试“null”如下:
private class Title extends AsyncTask<Void, Void, Void> {
String name;
String value;
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(HTML.this);
mProgressDialog.setTitle("Checking Database");
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
try {
Document doc = Jsoup.connect(url).get();
Elements inputElems =doc.select("input#small-input");
for (Element inputElem : inputElems){
name = inputElem.attr("name");
value = inputElem.attr("value");
}
} catch(Throwable t) {
t.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// Set title into TextView
TextView txttitle = (TextView) findViewById(R.id.showPatientNumber);
txttitle.setText(value);
mProgressDialog.dismiss();
}
}
答案 0 :(得分:0)
试试这个:
Elements inputElems =doc.select("input");
for (Element inputElem : inputElems){
name = inputElem.attr("name").first();
value = inputElem.attr("value").first();
}
答案 1 :(得分:0)
我最终可以使用以下代码获取值:
Element pInput = doc.select("input[id=small-input]").first();
rPts = pInput.attr("value");
这最初是null,但在我添加以下代码以获取身份验证cookie后,我将其传递到存储我的数据值的下一个网页以及我试图从中获取解析数据的地方,它是成功的。
Document doc1 = res.parse();
String sessionId = res.cookie("SESSIONID");
Document doc = Jsoup.connect(url)
.cookie("SESSIONID", sessionId)
.get();
答案 2 :(得分:0)
试试这个,
Elements inputElems =doc.select("input");
Iterator<Element> linksIt = inputElems .iterator();
while (linksIt.hasNext()) {
Element inputElem = linksIt.next();
String id = inputElem.attr("id");
if(id.equals("small-input")){
name = inputElem.attr("name");
value= inputElem.attr("value");
}
}