Android: How to parse HTML Table into ListView? - java

Hello i want to parse a HTML Table into a Android ListView but i don't know where to start. The Table has a lot of information. Could someone help me to start with this?
Thanks in advance!
The HTML Table: http://intranet.staring.nl/toepassingen/rooster/lochem/2W2/2012090320120909/2W01533.htm (Just click view source).

You will first need to parse the HTML table into a data structure, and then use ListView to display that information. Try using the JSoup library to do the HTML parsing: http://jsoup.org/cookbook/introduction/parsing-a-document

I don't know if you already got your answer here but I did the same with the link you suggest, I will post my code here but it is still very messy and don't apply for the newest timetable(9th hour)
Im using HTML Cleaner library for parsing the html:
try {
HtmlCleaner hc = new HtmlCleaner();
CleanerProperties cp = hc.getProperties();
cp.setAllowHtmlInsideAttributes(true);
cp.setAllowMultiWordAttributes(true);
cp.setRecognizeUnicodeChars(true);
cp.setOmitComments(true);
String loc = sp.getString( Constants.pref_locatie , "" );
String per = sp.getString( Constants.pref_persoon , "" );
String oob = sp.getString( Constants.pref_onderofboven , "" );
int counteruurmax;
int[] pauze;
if (oob.contains("onder")){
pauze = Constants.pauzeo;
} else if (oob.contains("boven")) {
pauze = Constants.pauzeb;
} else {
return false;
}
String url = "";
if (loc.contains("lochem")) {
url += Constants.RoosterLochem;
url += t.getDatum();
url += "/";
url += per;
counteruurmax = 11;
} else if (loc.contains("herenlaan")) {
url += Constants.RoosterHerenlaan;
url += per;
counteruurmax = 13;
} else if (loc.contains("beukenlaan")) {
url += Constants.RoosterBeukenlaan;
url += per;
counteruurmax = 11;
} else {
return false;
}
String htmlcode = t.getHtml(url);
TagNode html = hc.clean(htmlcode);
Document doc = new DomSerializer(cp, true).createDOM(html);
XPath xp = XPathFactory.newInstance().newXPath();
NodeList nl = (NodeList) xp.evaluate(Constants.XPathRooster, doc, XPathConstants.NODESET);
int counteruur = 1;
int counterdag = 1;
int decreaser = 0;
Boolean isPauze = false;
RoosterItems RItems = new RoosterItems();
RoosterItem RItem = null;
for (int i = 0; i < nl.getLength(); i++){
if ((counteruur == pauze[0]) || (counteruur == pauze[1]) || (counteruur == pauze[2])) {
isPauze = true;
decreaser++;
}
if (!isPauze) {
RItem = new RoosterItem();
switch (counterdag){
case 1:
RItem.setDag("ma");
break;
case 2:
RItem.setDag("di");
break;
case 3:
RItem.setDag("wo");
break;
case 4:
RItem.setDag("do");
break;
case 5:
RItem.setDag("vr");
break;
}
Node n = nl.item(i);
String content = n.getTextContent();
if (content.length() > 1) {
RItem.setUur(""+(counteruur-decreaser));
NodeList t1 = n.getChildNodes();
NodeList t2 = t1.item(0).getChildNodes();
NodeList t3 = t2.item(0).getChildNodes();
for (int j = 0; j < t3.getLength(); j++) {
Node temp = t3.item(j);
if (t3.getLength() == 3) {
switch (j) {
case 0:
RItem.setLes(""+temp.getTextContent());
break;
case 1:
RItem.setLokaal(""+temp.getTextContent());
break;
case 2:
RItem.setDocent(""+temp.getTextContent());
break;
default:
return false;
}
} else if (t3.getLength() == 4) {
switch (j) {
case 0:
break;
case 1:
RItem.setLes("tts. " + temp.getTextContent());
break;
case 2:
RItem.setLokaal(""+temp.getTextContent());
break;
case 3:
RItem.setDocent(""+temp.getTextContent());
break;
default:
return false;
}
} else if (t3.getLength() == 1) {
RItem.setLes(""+temp.getTextContent());
} else {
return false;
}
}
} else {
RItem.setUur("" + (counteruur-decreaser));
RItem.setLokaal("Vrij");
}
RItems.add(RItem);
}
if (counteruur == counteruurmax) { counteruur = 0; counterdag++; decreaser = 0;}
counteruur++;
isPauze = false;
}
if (RItems.size() > 0) {
mSQL = new RoosterSQLAdapter(mContext);
mSQL.openToWrite();
mSQL.deleteAll();
for (int j = 0; j < RItems.size(); j++) {
RoosterItem insert = RItems.get(j);
mSQL.insert(insert.getDag(), insert.getUur(), insert.getLes(), insert.getLokaal(), insert.getDocent());
}
if (mSQL != null) mSQL.close();
}
return true;
} catch (ParserConfigurationException e) {
e.printStackTrace();
return false;
} catch (XPathExpressionException e) {
e.printStackTrace();
return false;
}
There are a few constants but I think you can guess them yourself;) and otherwise you know how to ask me for them:)
The RoosterItem class will hold all variables of an hour, and the RoosterItems will hold more than one RoosterItem
Good Luck!

So far i think JSoup is one of the best way to extract or manipulate the HTML.....
See this link :
http://jsoup.org/
But somehow.... this did't worked in my case, so i converted the entire HTML code into String, then parsed it.....

Related

The Google Analytics API does not find PagePath

I am trying to report google analytics data to a database using JAVA, but certain addresses are returning null even if the URL exists.
Example: / catalog / companyX / US / en ----- it finds.
/catalog/companyX/US/en/research ------ he can't find it.
I tried using the filter "ga: pagePath=#/research" and it always returns null. What will be the problem? Because on google analytics site he finds these URLs.
public GetReportsResponse getReport(AnalyticsReporting service) throws IOException {
// Create the DateRange object.
DateRange dateRange = new DateRange();
dateRange.setStartDate("2019-10-06");
dateRange.setEndDate("2019-10-06");
// Create the Metrics object.
Metric timeOnPage = new Metric().setExpression("ga:timeOnPage");
Metric totalEvents = new Metric().setExpression("ga:totalEvents");
Metric avgSessionDuration = new Metric().setExpression("ga:avgSessionDuration");
Metric hits = new Metric().setExpression("ga:hits");
// Create the Dimensions object.
Dimension browser = new Dimension().setName("ga:browser");
Dimension device = new Dimension().setName("ga:deviceCategory");
Dimension country = new Dimension().setName("ga:country");
Dimension page = new Dimension().setName("ga:pagePath");
Dimension eventCategory = new Dimension().setName("ga:eventCategory");
Dimension eventAction = new Dimension().setName("ga:eventAction");
Dimension eventLabel = new Dimension().setName("ga:eventLabel");
Dimension date = new Dimension().setName("ga:date");
Dimension pageTitle = new Dimension().setName("ga:pageTitle");
ReportRequest request6 = new ReportRequest().setViewId(VIEW_ID).setDateRanges(Arrays.asList(dateRange))
.setPageSize(100000)
.setMetrics(Arrays.asList(totalEvents, hits, avgSessionDuration, timeOnPage))
.setDimensions(Arrays.asList(date, country, page, browser, device, eventCategory, eventAction,
eventLabel, pageTitle));
ArrayList<ReportRequest> requests = new ArrayList<ReportRequest>();
requests.add(request6);
// Create the GetReportsRequest object.
GetReportsRequest getReport = new GetReportsRequest().setReportRequests(requests);
// Call the batchGet method.
GetReportsResponse response = service.reports().batchGet(getReport).execute();
// Return the response.
return response;
}
public void printResponse(GetReportsResponse response) {
for (Report report : response.getReports()) {
ColumnHeader header = report.getColumnHeader();
List<String> dimensionHeaders = header.getDimensions();
List<MetricHeaderEntry> metricHeaders = header.getMetricHeader().getMetricHeaderEntries();
List<ReportRow> rows = report.getData().getRows();
if (rows == null) {
System.out.println("No data found for " + VIEW_ID);
return;
}
for (ReportRow row : rows) {
TableGA tableGA = new TableGA();
Acessos acessos = new Acessos();
List<String> dimensions = row.getDimensions();
List<DateRangeValues> metrics = row.getMetrics();
if (row.getDimensions() != null) {
for (int i = 0; i < dimensionHeaders.size() && i < dimensions.size(); i++) {
switch (dimensionHeaders.get(i)) {
case "ga:browser":
tableGA.setBrowser(dimensions.get(i));
break;
case "ga:deviceCategory":
tableGA.setDevice(dimensions.get(i));
break;
case "ga:country":
tableGA.setCountry(dimensions.get(i));
break;
case "ga:pagePath":
tableGA.setPage(dimensions.get(i));
break;
case "ga:eventCategory":
tableGA.setEventCategory(dimensions.get(i));
break;
case "ga:eventAction":
tableGA.setEventAction(dimensions.get(i));
break;
case "ga:eventLabel":
tableGA.setEventLabel(dimensions.get(i));
break;
case "ga:date":
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date convertedCurrentDate = null;
String year = dimensions.get(i).substring(0, 4);
String month = dimensions.get(i).substring(4, 6);
String day = dimensions.get(i).substring(6, 8);
String date = year + "-" + month + "-" + day;
try {
convertedCurrentDate = simpleDateFormat.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
tableGA.setActualDate(convertedCurrentDate);
break;
case "ga:pageTitle":
tableGA.setPageTitle(dimensions.get(i));
break;
}
}
}
for (int j = 0; j < metrics.size(); j++) {
DateRangeValues values = metrics.get(j);
for (int k = 0; k < values.getValues().size() && k < metricHeaders.size(); k++) {
switch (metricHeaders.get(k).getName()) {
case "ga:timeOnPage":
tableGA.setTimeOnPage(Double.parseDouble(values.getValues().get(k)));
break;
case "ga:totalEvents":
tableGA.setTotalEvents(Integer.parseInt(values.getValues().get(k)));
break;
case "ga:avgSessionDuration":
tableGA.setAvgSessionDuration(Double.parseDouble(values.getValues().get(k)));
break;
case "ga:hits":
tableGA.setHits(Integer.parseInt(values.getValues().get(k)));
break;
}
}
}
gaService.save(tableGA);
System.out.println(tableGA.getPage());
gaService.save(acessos);
}
}
}

J2ME , Quizz using choiceGroups

I am working on a driving licence project on j2Me wich is including Tests like quizz , well and i am having a problem after parsing the questions and moving them into choiceGroups just like that :
if (questions.length > 0) {
for (int i = 0; i < questions.length; i++) {
ChoiceGroup reponses = new ChoiceGroup("Reponses" + i, Choice.EXCLUSIVE);
reponses.append(questions[i].getReponse1(), null);
reponses.append(questions[i].getReponse2(), null);
reponses.append(questions[i].getReponse3(), null);
pass.append(questions[i].getContenu());
pass.append(reponses);
}
}
} catch (Exception e) {
System.out.println("Exception:" + e.toString());
}
disp.setCurrent(pass);
and the next step is the command who's controlling the choiceGroups to test them if they are like the true answer or not .
so i am blocked here .
if (c == valider) {
int result = 0;
for (int i = 0; i < pass.size(); i++) {
String ch = pass.get(i).getLabel();
System.out.println(ch);
}
}
I don't know how to get the choice from the choicegroup
any help
Actually, I am not sure what totally you want for:
This code will help you get selected items from choicegroup that i did long time before:
//get a selected array in choicegroup
private String[] choiceGroupSelected(ChoiceGroup cg) {
String selectedArray[] = new String[cg.size()];
int k = 0;
for (int i = 0; i < cg.size(); i++) {
if (cg.isSelected(i)) {
selectedArray[k] = cg.getString(i);
k++;
}
}
return selectedArray;
}
That function will help me get all selected items for deleting action below:
private void deleteSpecificItem() {
try {
String temp = null;
int index;
//get ChoiceGroup size
int numbers = cgTrip.size();
String selectedItems[] = choiceGroupSelected(cgTrip);
//
rs = services.RecordStoreManager.openRecordStoreByName("TripRS");
re = rs.enumerateRecords(null, null, true);
String[] tripList = new String[2];
for (int i = 0; i < numbers; i++) {
temp = selectedItems[i];
if (temp != null) {
while (re.hasNextElement()) {
try {
index = re.nextRecordId();
System.out.println("RecordID: " + index);
byte[] byteBuff = rs.getRecord(index);
String source = new String(byteBuff);
tripList = services.StringManager.getItems(source, ";", 2);
String strProcess = tripList[0] + "-" + tripList[1];
//inspect all of items in choicegroup and if they are selecting then compare with record
//If comparison is true then delete this record
if (temp.equals(strProcess)) {
System.out.println("Delete RecordID: " + index);
rs.deleteRecord(index);
re.keepUpdated(true);
break;
}
} catch (RecordStoreException ex) {
ex.printStackTrace();
}
}
}
}
try {
rs.closeRecordStore();
} catch (RecordStoreException ex) {
ex.printStackTrace();
}
rs = null;
re.destroy();
this.LoadTripItem();
} catch (RecordStoreNotOpenException ex) {
ex.printStackTrace();
}
}

Get text from xml - Android

I have this xml online http://64.182.231.116/~spencerf/test.xml
And I am trying to get the two text values Assorted Cereal and Yogurt Parfait (2). Here is how I am currently parsing it, and I get the values I want as well as all the values under then, all the numbers and such, but I just want to get the names, and I am struggling how to just do that, any help or guidance would be great. Here is my code:
String currentDay = "";
String currentMeal = "";
String counter = "";
String icon1 = "";
LinkedHashMap<String, List<String>> itemsByCounter = new LinkedHashMap<String , List<String>>();
List<String> items = new ArrayList<String>();
while (eventType != XmlResourceParser.END_DOCUMENT) {
String tagName = xmlData.getName();
switch (eventType) {
case XmlResourceParser.START_TAG:
if (tagName.equalsIgnoreCase("day")) {
currentDay = xmlData.getAttributeValue(null, "name");
}
if (tagName.equalsIgnoreCase("meal")) {
currentMeal = xmlData.getAttributeValue(null, "name");
}
if (tagName.equalsIgnoreCase("counter") && currentDay.equalsIgnoreCase(day) && currentMeal.equalsIgnoreCase(meal)) {
counter = xmlData.getAttributeValue(null, "name");
}
if (tagName.equalsIgnoreCase("name") && counter != null && currentDay.equalsIgnoreCase(day) && currentMeal.equalsIgnoreCase(meal)) {
icon1 = xmlData.getAttributeValue(null, "icon1");
Log.i(TAG, "icon1: " + icon1);
}
break;
case XmlResourceParser.TEXT:
if (currentDay.equalsIgnoreCase(day) && currentMeal.equalsIgnoreCase(meal) && counter !=(null)) {
if (xmlData.getText().trim().length() > 0) {
//Here gets everything but I just want 2 names
Log.i(TAG, "data: " + xmlData.getText());
items.add(xmlData.getText().trim().replaceAll(" +", " "));
}
}
break;
case XmlPullParser.END_TAG:
if (tagName.equalsIgnoreCase("counter")) {
if (items.size() > 0) {
itemsByCounter.put(counter, items);
items = new ArrayList<String>();
recordsFound++;
}
}
break;
}
eventType = xmlData.next();
So as you can see in the comment in my code I am getting everything under the name tag, back but I just want the value of the name, and not all the other stuff.
You will need to store the name in its own child element (meaning put an end tag before the nutritional facts). Under each dish, you could have this:
<name>Assorted Cereal</name>
<nutrition_facts> ... </nutrition_facts>
Not tested but could do it along these lines:
List<Nutrition_Facts> nutrition_facts = new ArrayList<Nutrition_Facts>();
XMLDOMParser parser = new XMLDOMParser();
AssetManager manager = context.getAssets();
InputStream stream;
try {
stream = manager.open("test.xml"); //need full path to your file here - mine is stored in assets folder
Document doc = parser.getDocument(stream);
}catch(IOException ex){
System.out.printf("Error reading map %s\n", ex.getMessage());
}
NodeList nodeList = doc.getElementsByTagName("nutrition_facts");
for (int i = 0; i < nodeList.getLength(); i++) {
Element e = (Element) nodeList.item(i);
String name;
if (elementName.equals(e.getAttribute("Assorted Cereal"))){
name = e.getAttribute("name");
//do some stuff
}
}
//XMLDOMParser Class
public class XMLDOMParser {
//Returns the entire XML document
public Document getDocument(InputStream inputStream) {
Document document = null;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = factory.newDocumentBuilder();
InputSource inputSource = new InputSource(inputStream);
document = db.parse(inputSource);
} catch (ParserConfigurationException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (SAXException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (IOException e) {
Log.e("Error: ", e.getMessage());
return null;
}
return document;
}
/*
* I take a XML element and the tag name, look for the tag and get
* the text content i.e for <employee><name>Kumar</name></employee>
* XML snippet if the Element points to employee node and tagName
* is name I will return Kumar. Calls the private method
* getTextNodeValue(node) which returns the text value, say in our
* example Kumar. */
public String getValue(Element item, String name) {
NodeList nodes = item.getElementsByTagName(name);
return this.getTextNodeValue(nodes.item(0));
}
private final String getTextNodeValue(Node node) {
Node child;
if (node != null) {
if (node.hasChildNodes()) {
child = node.getFirstChild();
while(child != null) {
if (child.getNodeType() == Node.TEXT_NODE) {
return child.getNodeValue();
}
child = child.getNextSibling();
}
}
}
return "";
}
}

Does memory effect preformance or lot of if conditions effect?

Can any one please suggest best Performance code between these two code snippets
Code 1
Map token_map = new HashMap();
for (int i = 1; i < req_tokens.length; ++i) {
String[] spl_token = null;
spl_token = req_tokens[i].trim().split("=");
token_map.put(spl_token[0], spl_token[1]);
}
cs_chnl_id = (String) token_map.get("ChannelId");
obj_id = (String) token_map.get("ObjectId");
channel_name = (String) token_map.get("ChannelName");
username = (String) token_map.get("Username");
pwd = (String) token_map.get("Password");
hotel_key = (String) token_map.get("Channel_hotel_key");
resId = (String) token_map.get("Res_Id");
String booking_date = (String) token_map.get("Booking_Date");
days_past = (String) token_map.get("Days_past");
Code 2
for (int i = 1; i < req_tokens.length; ++i) {
String[] spl_token = null;
String key = null;
spl_token = req_tokens[i].trim().split("=");
key = spl_token[0].trim();
// initilizing cron values for instance varibles
if (key.equalsIgnoreCase("ChannelId")) {
cs_chnl_id = spl_token[1].trim();
} else if (key.equalsIgnoreCase("ObjectId")) {
objectId = spl_token[1].trim();
} else if (key.equalsIgnoreCase("ChannelName")) {
channel_name = spl_token[1].trim();
} else if (key.equalsIgnoreCase("Username")) {
username = spl_token[1].trim();
} else if (key.equalsIgnoreCase("Password")) {
pwd = spl_token[1].trim();
} else if (key.equalsIgnoreCase("Channel_hotel_key")) {
hotel_key = spl_token[1].trim();
} else if (key.equalsIgnoreCase("Res_Id")) {
resId = spl_token[1].trim();
} else if (key.equalsIgnoreCase("Booking_Date")) {
booking_date = spl_token[1].trim();
} else if (key.equalsIgnoreCase("Days_past")) {
days_past = spl_token[1].trim();
}
}
In HashMap, insertion and lookup operations have time complexity of O(1), they'll have almost the same performance, but the first code is much more readable, don't waste your time about performance, go with the first.

String statement returning null from switch statement

I am currently working on an Android application that allows you to watch streaming video in a VideoView. I have a method for allowing you to select one of four streams via a switch statement. That is working correctly and the code for that is as follows:
public void playStream(int position) {
switch (position) {
case 0:
streamOn = true;
streamPos = 0;
logString = "M";
posSelected = "0";
break;
case 1:
streamOn = true;
streamPos = 1;
logString = "J";
posSelected = "1";
break;
case 2:
streamOn = true;
streamPos = 2;
logString = "B";
posSelected = "2";
break;
case 3:
streamOn = true;
streamPos = 3;
logString = "N";
posSelected = "3";
break;
default:
break;
}
checkStreamLink(position);
Log.wtf(logString, posSelected);
Log.wtf(logString, streamURL);
}
What is not working correctly is that in this method for selecting the stream, I have a call to another method ( checkStreamLink(); ) that runs a thread. Depending on which stream you have selected, the thread will call another method that opens up a webpage, reads a line of text, and then sets that text to a String streamURL. The code for those two methods is as follows:
public void checkStreamLink(final int position) {
Thread thread = new Thread(new Runnable() {
#Override
public void run() {
try {
switch (position) {
case 0:
streamURL = getStreamLink("LINK 0 GOES HERE");
break;
case 1:
streamURL = getStreamLink("LINK 1 GOES HERE");
break;
case 2:
streamURL = getStreamLink("LINK 2 GOES HERE");
break;
case 3:
streamURL = getStreamLink("LINK 3 GOES HERE");
break;
default:
break;
}
}
catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}
public String getStreamLink (String textSource) {
URL streamURL;
String errorParsingURL = "ERROR PARSING URL";
try {
streamURL = new URL(textSource);
BufferedReader bufferReader = new BufferedReader(new InputStreamReader(streamURL.openStream()));
String StringBuffer;
String stringText = "";
while ((StringBuffer = bufferReader.readLine()) != null) {
stringText += StringBuffer;
}
bufferReader.close();
return stringText;
}
catch (MalformedURLException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return errorParsingURL;
}
The issue I'm having is that the String streamURL is returning null on its first use as evidenced by the Log statements I have included. Each time you select a stream after that, the String streamURL returns the text that you should have received the previous time you select a stream. I cannot seem to figure out why this is happening and I would appreciate any assistance.
You are getting a null because getStreamLink is returning its value after you have already printed the result. Print the result log messages at the end of the getStreamLink method to see the actual value which is being returned, and call any additional functionality at that point as well.

Categories