doc = Jsoup.parse("<table><tr><td>Phone No</td><td>Officers</td></tr><tr><td>123456789</td><td>Csa</td></tr></table>");
try {
for (Element table : doc.select("table")) {
for (Element row : table.select("tr")) {
Elements tds = row.select("td");
String PhoneNo = tds.get(0).text();
String Officers = tds.get(1).text();
jsonObject.put("Phone No", PhoneNo);
jsonObject.put("Officers", Officers);
}
list.put(jsonObject);
}
} catch ( JSONException e) {
e.printStackTrace();
}
这段代码完美无缺,但是当我尝试使用代码时
doc = Jsoup.connect(url).get();
它会抛出NetworkOnMainThreadException
,然后我按照以下解决方案“Parsing with jsoup throws error (NetworkOnMainThreadException)”将我的代码放入AsyncTask
它会抛出java.lang.RuntimeException: An error occured while executing doInBackground()
这是Stacktrace:
FATAL EXCEPTION: AsyncTask #1 Process: com.rams.amar.information, PID: 12076 java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:304)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at com.rams.amar.information.activities.ContactsActivity$MyTask.doInBackground(ContactsActivity.java:58)
at com.rams.amar.information.activities.ContactsActivity$MyTask.doInBackground(ContactsActivity.java:41)
at android.os.AsyncTask$2.call(AsyncTask.java:292)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
以下是完整代码:ContactsActivity.java
package com.rams.a.information.activities;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.TextView;
import com.rams.a.information.R;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
public class ContactsActivity extends AppCompatActivity {
String url="http://www.uok.ac.in/contactus.html";
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nav_contacts);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
this.setTitle("Contacts");
MyTask mT = new MyTask();
mT.execute();
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
if((item.getItemId() == android.R.id.home))
{
onBackPressed();
}
return super.onOptionsItemSelected(item);
}
private class MyTask extends AsyncTask<Void, Void, String> {
Document doc;
JSONObject jsonObject = new JSONObject();
JSONArray list = new JSONArray();
@Override
protected String doInBackground(Void... params) {
try {
doc = Jsoup.connect(url).get();
} catch (IOException e) {
e.printStackTrace();
}
try {
for (Element table : doc.select("table")) {
for (Element row : table.select("tr")) {
Elements tds = row.select("td");
String PhoneNo = tds.get(0).text();
String Officers = tds.get(1).text();
jsonObject.put("Phone No", PhoneNo);
jsonObject.put("Officers", Officers);
}
list.put(jsonObject);
}
} catch ( JSONException e) {
e.printStackTrace();
}
return list.toString();//stringArray.toString();
}
@Override
protected void onPostExecute(String result) {
((TextView)findViewById (R.id.textView3)).setText(result);
}
}
}
答案 0 :(得分:1)
您的HTML表数据看起来并不总是有两列。
尝试添加if语句。
for (Element table : doc.select("table")) {
for (Element row : table.select("tr")) {
Elements tds = row.select("td");
JSONObject jsonObject = new JSONObject();
if (tds.size() >= 2) { // see here
jsonObject.put("Phone No", tds.get(0).text());
jsonObject.put("Officers", tds.get(1).text());
list.put(jsonObject);
}
}
}
答案 1 :(得分:0)
请检查表格主体之前的内容中是否有任何其他标签,如doctype或xml声明等。
答案 2 :(得分:0)
try {
for (Element table : doc.select("table")) {
for (Element row : table.select("tr")) {
JSONObject jsonObject = new JSONObject();
Elements tds = row.select("td");
if(tds.size()==1){continue;} else{
String PhoneNo = tds.get(0).text();
String Officers = tds.get(1).text();
jsonObject.put("Phone No", PhoneNo);
jsonObject.put("Officers", Officers);
}
list.put(jsonObject);
}
}
} catch ( JSONException e) {
e.printStackTrace();
}
此准则完美无缺。
答案 3 :(得分:-1)
在活动onCreate()
中记下以下代码var id="id_to_be_filtered";
App = jQuery.grep(obj, function( data ) {
return data.ApplicationId == id;
});