如何从一个网站上检索表中的数据,并在Android的一个TextView显示它 [英] How to retrieve table data from a website and display it in a textview in android
本文介绍了如何从一个网站上检索表中的数据,并在Android的一个TextView显示它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在开发Android的一个简单的应用程序,但我坚持。我需要帮助检索本网站和在一个TextView显示。下面是code我一直在尝试使用。
公共类MainActivity延伸活动{
TextView的电视;
最终字符串URL =http://mtn.co.ug/MTN-Internet/MTN-Mobile-Internet.aspx; 在此输入code @覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
电视=(的TextView)findViewById(R.id.textView);
新MyTask()执行();
} 私有类MyTask扩展的AsyncTask<太虚,太虚,太虚> {
ProgressDialog PROG;
//字符串title =;
@覆盖
在preExecute保护无效(){
PROG =新ProgressDialog(MainActivity.this);
prog.setMessage(加载......);
prog.show();
}
@覆盖
保护无效doInBackground(虚空...... PARAMS){
尝试{
文档文件= Jsoup.connect(\"http://mtn.co.ug/MTN-Internet/MTN-Mobile-Internet.aspx\").timeout(90000).ignoreHttpErrors(true).get(); / *为(元素表:Doc.select(tabscontent)){
对(件行:表格。选取(TR)){
元素TDS = row.select(TD);
如果(tds.size()→6){
的System.out.println(tds.get(0)的.text()+:+ tds.get(1)的.text());
}
}
} * / //标题= Doc.title();
对于(元黄釉:Doc.select(分区TBODY:包含(捆绑):EQ(6)TR TD)){
的System.out.println(Yello.text());
tv.setText(Yello.text()); //标题= Yello.toString();
}
}赶上(IOException异常五){
e.printStackTrace();
}
//返回null;
返回null;
} @覆盖
保护无效onPostExecute(虚空结果){
super.onPostExecute(结果);
如果(this.prog.isShowing()){
this.prog.dismiss();
}
//tv.setText(title); }
} @覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
//充气菜单;如果是present这增加了项目操作栏。
。getMenuInflater()膨胀(R.menu.main,菜单);
返回true;
}
}
解决方案
试试这个
文档DOC = Jsoup.connect(\"http://www.mtn.co.ug/Mobile-Plans/PayAsYouGo/MTN-PerMinute.aspx\").userAgent(\"Mozilla/5.0 (Windows NT的6.1; WOW64; RV:27.0)的Gecko / 20100101火狐/ 27.0)
.timeout(9999999).followRedirects(真).header(接受,text / html的,是application / xhtml + xml的,应用/ XML; Q = 0.9 * / *; Q = 0.8)获得()。 ;
对于(元黄釉:doc.select(分区:包含(包)TR)){ 。字符串结果= Yello.select(TD)的文本()修剪();
tv.setTxt(结果)
} }
I am developing a simple app in android but am stuck. I need help to retrieve data from a table on this website and display it in a TextView. Below is the code I've been trying to use.
public class MainActivity extends Activity {
TextView tv;
final String URL = "http://mtn.co.ug/MTN-Internet/MTN-Mobile-Internet.aspx";
enter code here @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.textView);
new MyTask().execute();
}
private class MyTask extends AsyncTask<Void, Void, Void> {
ProgressDialog prog;
//String title = "";
@Override
protected void onPreExecute() {
prog = new ProgressDialog(MainActivity.this);
prog.setMessage("Loading....");
prog.show();
}
@Override
protected Void doInBackground(Void... params) {
try {
Document Doc= Jsoup.connect("http://mtn.co.ug/MTN-Internet/MTN-Mobile-Internet.aspx").timeout(90000).ignoreHttpErrors(true).get();
/*for (Element table : Doc.select("tabscontent")) {
for (Element row : table.select("tr")) {
Elements tds = row.select("td");
if (tds.size() > 6) {
System.out.println(tds.get(0).text() + ":" + tds.get(1).text());
}
}
}
*/
//title = Doc.title();
for (Element Yello: Doc.select("div tbody:contains(Bundle):eq(6) tr td") ) {
System.out.println(Yello.text());
tv.setText(Yello.text());
//title = Yello.toString();
}
} catch (IOException e) {
e.printStackTrace();
}
//return null;
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (this.prog.isShowing()) {
this.prog.dismiss();
}
//tv.setText(title);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
解决方案
Try This
Document doc = Jsoup.connect("http://www.mtn.co.ug/Mobile-Plans/PayAsYouGo/MTN-PerMinute.aspx").userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0")
.timeout(9999999).followRedirects(true).header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8").get();;
for (Element Yello: doc.select("div:contains(bundle) tr") ) {
String results = Yello.select("td").text().trim();
tv.setTxt(results)
}
}
这篇关于如何从一个网站上检索表中的数据,并在Android的一个TextView显示它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文