在尝试其他用户在以下 SO 帖子中发布的建议后,我收到 3 个错误:
这是我第一次使用 JSoup 构建任何东西,因此非常感谢任何提示/技巧或我应该知道的事情。
错误:
Type mismatch: cannot convert from Elements to Element
The method select(String) in the type Element is not applicable for the arguments (Element)
tr cannot be resolved to a variable
资源:
package com.example.test;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView tv;
final String URL = "http://exampleurl.com";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.TextView01);
new MyTask().execute(URL);
}
private class MyTask extends AsyncTask<String, Void, String> {
ProgressDialog prog;
String title = "";
@Override
protected void onPreExecute() {
prog = new ProgressDialog(MainActivity.this);
prog.setMessage("Loading....");
prog.show();
}
@Override
protected String doInBackground(String... params) {
try {
Document doc = Jsoup.connect(params[0]).get();
Element tableElement = doc.select(".datagrid");
Element th = doc.select(tr).first;
Element firstTh = th.select(th).first();
title = firstTh.text();
} catch (IOException e) {
e.printStackTrace();
}
return title;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
prog.dismiss();
tv.setText(result);
}
}
}
修订版 1:
public class MainActivity extends Activity {
TextView tv;
final String URL = "http://exampleurl.com";
String tr;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.TextView01);
new MyTask().execute(URL);
}
private class MyTask extends AsyncTask<String, Void, String> {
ProgressDialog prog;
String title = "";
@Override
protected void onPreExecute() {
prog = new ProgressDialog(MainActivity.this);
prog.setMessage("Loading....");
prog.show();
}
@Override
protected String doInBackground(String... params) {
try {
Document doc = Jsoup.connect(params[0]).get();
Elements tableElement = doc.select(".datagrid");
// Elements th = doc.select(tr).first;
Elements th = doc.select(".attribute").first();
title = firstTh.text();
} catch (IOException e) {
e.printStackTrace();
}
return title;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
prog.dismiss();
tv.setText(result);
}
}
}