我试图从HTML页面解析重定向的页面,当我试图在Java控制台中运行它时它运行得很好但是当我尝试运行它时它会失败一些Android中的相同代码。
这是堆栈:
W: java.lang.IllegalArgumentException: Header name must not be empty
W: at org.jsoup.helper.Validate.notEmpty(Validate.java:102)
W: at org.jsoup.helper.HttpConnection$Base.header(HttpConnection.java:292)
W: at org.jsoup.helper.HttpConnection$Response.processResponseHeaders(HttpConnection.java:828)
W: at org.jsoup.helper.HttpConnection$Response.setupFromConnection(HttpConnection.java:772)
W: at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:569)
W: at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:540)
W: at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:227)
W: at gavi_anna_netanel.com.madomes.ug_login.GradesParser$GradesFetcher.getGradesList(GradesParser.java:48)
W: at gavi_anna_netanel.com.madomes.ug_login.GradesParser$GradesFetcher.doInBackground(GradesParser.java:32)
W: at gavi_anna_netanel.com.madomes.ug_login.GradesParser$GradesFetcher.doInBackground(GradesParser.java:28)
W: at android.os.AsyncTask$2.call(AsyncTask.java:287)
W: at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
W: at java.util.concurrent.FutureTask.run(FutureTask.java:137)
W: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
W: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
W: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
W: at java.lang.Thread.run(Thread.java:856)
这是我的代码:
import android.os.AsyncTask;
import org.json.JSONArray;
import org.json.JSONObject;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.ByteArrayInputStream;
import java.util.Map;
/**
* Created by Anna on 6/18/2016.
*/
public class GradesParser {
String outputFileName;
public GradesParser(String outputFileName) {
this.outputFileName = outputFileName;
}
private class GradesFetcher extends AsyncTask<String, Void, Document> {
@Override
protected Document doInBackground(String... params) {
return getGradesList(params[0], params[1]);
}
private Document getGradesList(String username, String password) {
try {
//create session (?) and prepare initial cookie
Response resp1 = Jsoup.connect("http://techmvs.technion.ac.il/cics/wmn/wmngrad?ORD=1").followRedirects(true).execute();
String url1 = resp1.url().toString();
Map welcomeCookies = resp1.cookies();
//login for the session
String url2 = url1 + "&s=1";
Response resp2 = Jsoup
.connect(url1 + "&s=1")
.followRedirects(true)
.data("userid", username)
.data("password", password)
.data("function", "signon")
.header("Content-Type", "application/x-www-form-urlencoded")
.header("Referer", "http://ug3.technion.ac.il/Tadpis.html")
.cookies(welcomeCookies)
.method(Method.POST)
.execute();
Map<String, String> coockies = resp2.cookies();
if ((!coockies.containsKey("TechAnamUser")) || (coockies.get("TechAnamUser") != username)) {
return null;
}
Document doc3 = Jsoup.parse(new ByteArrayInputStream(resp2.bodyAsBytes()), "ISO-8859-8", url2);
return doc3;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
public JSONObject gradesParse(String username, String password) {
Document gradesList = null;
try {
gradesList = new GradesFetcher().execute(username, password).get();
} catch (Exception e) {
e.printStackTrace();
return null;
}
if (gradesList == null) {
return null;
}
Elements tables = gradesList.getElementsByTag("table");
Elements rows = tables.get(2).select("tr");
Elements cells = rows.get(1).select("td");
JSONObject gradesListObject = new JSONObject();
try {
gradesListObject.put("point num", Float.parseFloat(cells.get(0).text()));
gradesListObject.put("success rate", Integer.parseInt(cells.get(1).text()));
gradesListObject.put("average", Float.parseFloat(cells.get(2).text()));
} catch (Exception e) {
e.printStackTrace();
return null;
}
JSONArray semesters = new JSONArray();
if (tables.size() > 5) {
for (int i = 4; i < tables.size() - 1; i++) {
JSONObject semester = new JSONObject();
rows = tables.get(i).select("tr");
JSONArray courses = new JSONArray();
for (int j = 2; j < rows.size() - 1; j++) {
JSONObject course = new JSONObject();
cells = rows.get(j).select("td");
try {
course.put("grade", Integer.parseInt(cells.get(0).text()));
course.put("points", Float.parseFloat(cells.get(1).text()));
String numberWithName = cells.get(2).text();
numberWithName = numberWithName.replace("\u00a0", " ");
String[] splitted = numberWithName.split(" ");
String name = new StringBuilder(splitted[splitted.length - 2]).reverse().toString();
for (int k = splitted.length - 3; k >= 0; k--) {
name = name + " " + new StringBuilder(splitted[k]).reverse().toString();
}
course.put("number", splitted[splitted.length - 1]);
course.put("name", name);
courses.put(course);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
try {
semester.put("courses", (Object) courses);
Element lastRow = rows.get(rows.size() - 1);
cells = lastRow.select("td");
semester.put("points", Float.parseFloat(cells.get(1).text()));
String averageWithSuccessRate = cells.get(0).text();
averageWithSuccessRate = averageWithSuccessRate.replace("\u00a0", " ");
String[] separeted = averageWithSuccessRate.split(" ");
semester.put("average", Float.parseFloat(separeted[0]));
semester.put("success rate", Integer.parseInt(separeted[2].substring(0, separeted[2].length() - 1)));
semesters.put(semester);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
try {
gradesListObject.put("semesters", (Object) semesters);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return gradesListObject;
}
}
我可能对我需要的标题知之甚少,但我已经尝试了几个并且无法找到它。如果有人能帮助我,我会很感激。
答案 0 :(得分:-1)
OP选择的解决方案:
我们的团队决定操纵jsoup文件来忽略这样的标题。这对我们有用。