我正在制作一个 Android 应用程序,我需要帮助来定位元素中的特定文本
这就是我所在的位置:
Elements bookietemp = item.getElementsByClass("name");
String bookie1 = bookietemp.select("a[title]").first().text(); //This dosnt work
Log.d("test", bookie1);
我已经尝试过上述方法,但它不起作用或返回任何东西:
“bookietemp”将包含以下代码,我只想从中提取:“Toto”或“Tobet”(第二个单词/“Open”之后的单词,在title=之后)
这是来自“bookietemp”的值
<a rel="nofollow" class="name" title="Open Toto website!" target="_blank" href="/bookmakers/toto/web/"><span class="BK b6"> </span></a>
<a rel="nofollow" class="name" title="Open Tobet website!" target="_blank" href="/bookmakers/tobet/web/"><span class="BK b36"> </span></a>
我的完整代码在这里:
public class AsyncTaskActivity extends Activity {
Button btn_start;
TextView state;
TextView output;
ProgressDialog dialog;
Document doc;
String test;
Element test2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_async_task);
btn_start = (Button) findViewById(R.id.btn_start);
state = (TextView) findViewById(R.id.state);
output = (TextView) findViewById(R.id.output);
btn_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btn_start.setEnabled(false);
new ShowDialogAsyncTask().execute();
}
});
}
private class ShowDialogAsyncTask extends AsyncTask<String, Void, ArrayList<String>> {
ArrayList<String> arr_linkText=new ArrayList<String>();
@Override
protected void onPreExecute() {
// update the UI immediately after the task is executed
super.onPreExecute();
Toast.makeText(AsyncTaskActivity.this, "Invoke onPreExecute()",
Toast.LENGTH_SHORT).show();
output.setText("Please Wait!");
}
@Override
protected ArrayList<String> doInBackground(String... String) {
// String linkText = "";
try {
doc = Jsoup.connect("http://www.bmbets.com/sure-bets/").get();
// linkText = el.attr("href");
// arr_linkText.add(linkText);
Elements widgets = doc.getElementsByClass("surebets-widget");
for (Element widget : widgets){
//Log.d("test", el.toString());
Elements items = widget.getElementsByClass("item"); //Dette giver dig ca 8 items.
for (Element item : items)
{
Elements matchtemp = item.getElementsByClass("odd");
String matchname = matchtemp.select("a[title]").first().text();
Log.d("test", matchname);
//Here is the problem
Elements bookietemp = item.getElementsByClass("name");
String bookie1 = bookietemp.select("a[title]").first().text();
Log.d("test", bookie1);
Elements tipvals = item.getElementsByClass("tip-val");
if (tipvals.size() == 2)
{
Log.d("test", "Head to Head kamp");
Element tipval1 = tipvals.get(0);
String oddshome = tipval1.text().trim();
Element tipval2 = tipvals.get(1);
String oddsaway = tipval2.text().trim();
Log.d("test", oddshome + " " + oddsaway);
}
else
{
Log.d("test", "3 way");
Element tipval1 = tipvals.get(0);
String oddshome = tipval1.text().trim();
Element tipval2 = tipvals.get(1);
String oddsdraw = tipval2.text().trim();
Element tipval3 = tipvals.get(2);
String oddsaway = tipval3.text().trim();
Log.d("test", oddshome + " " + oddsdraw + " " + oddsaway);
}
}
// arr_linkText.add(linkText);
}
// return test2;
} catch (IOException e) {
e.printStackTrace();
}
return arr_linkText;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
// // progressBar.setProgress(values[0]);
// // txt_percentage.setText("downloading " +values[0]+"%");
}
@Override
protected void onPostExecute(ArrayList<String> result) {
// super.onPostExecute(result);
Toast.makeText(AsyncTaskActivity.this, "Invoke onPostExecute()",
Toast.LENGTH_SHORT).show();
state.setText("Done!");
//output.setText(result);
for (String temp_result : result){
output.append (temp_result +"\n");
}
btn_start.setEnabled(true);
}
}
请注意,我有一些类似于提取另一个文本的东西,它正在工作:
Elements matchtemp = item.getElementsByClass("odd");
String matchname = matchtemp.select("a[title]").first().text();
Log.d("test", matchname);