1 /* XmlRequestHelper.java
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 package de.dhbwloe.campusapp.network;
17 import android.os.Bundle;
18 import android.util.Log;
20 import com.loopj.android.http.AsyncHttpClient;
21 import com.loopj.android.http.AsyncHttpResponseHandler;
23 import net.fortuna.ical4j.data.CalendarBuilder;
24 import net.fortuna.ical4j.data.ParserException;
25 import net.fortuna.ical4j.model.Calendar;
27 import org.xmlpull.v1.XmlPullParser;
28 import org.xmlpull.v1.XmlPullParserException;
29 import org.xmlpull.v1.XmlPullParserFactory;
31 import java.io.ByteArrayInputStream;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.io.UnsupportedEncodingException;
35 import java.security.KeyStore;
36 import java.util.ArrayList;
37 import java.util.List;
39 import cz.msebera.android.httpclient.Header;
42 * Created by pk910 on 20.01.2016.
44 public abstract class XmlRequestHelper {
45 private static final String ns = null;
46 private String sRootElementName, sElementName;
48 protected void requestXmlFromWeb(String url, String rootElementName, String elementName) {
49 AsyncHttpClient client = new AsyncHttpClient();
51 sRootElementName = rootElementName;
52 sElementName = elementName;
54 Log.i("HTTPClient", "Request: "+url);
55 client.get(url, new AsyncHttpResponseHandler() {
57 public void onStart() {
61 public void onSuccess(int statusCode, Header[] headers, byte[] response) {
62 ByteArrayInputStream inputStream = new ByteArrayInputStream(response);
63 List<XmlEntry> entries = parseXml(inputStream, sRootElementName, sElementName);
65 onXmlReceived(entries);
69 public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
72 error = new String(errorResponse, "US-ASCII");
73 } catch (Exception e1) {
75 Log.i("HTTPClient", " Error: " + statusCode + " - " + error);
76 onXmlRequestFail(statusCode, error);
80 public void onRetry(int retryNo) {
85 public static List<XmlEntry> parseXml(InputStream input, String rootElementName, String elementName) {
86 List<XmlEntry> entries = null;
88 XmlPullParserFactory xmlFactoryObject = XmlPullParserFactory.newInstance();
89 XmlPullParser myparser = xmlFactoryObject.newPullParser();
91 myparser.setInput(input, null);
95 } while(!myparser.getName().equalsIgnoreCase(rootElementName));
97 entries = readXml(myparser, rootElementName, elementName);
98 } catch (Exception e) {
105 private static List<XmlEntry> readXml(XmlPullParser parser, String rootElementName, String elementName) throws XmlPullParserException, IOException {
106 List<XmlEntry> entries = new ArrayList<XmlEntry>();
108 parser.require(XmlPullParser.START_TAG, ns, rootElementName);
109 while (parser.next() != XmlPullParser.END_TAG) {
111 if (parser.getEventType() != XmlPullParser.START_TAG) {
114 String name = parser.getName();
115 // Starts by looking for the entry tag
116 if (name.equalsIgnoreCase(elementName)) {
117 Log.i("XMLParser", "found "+elementName);
118 entries.add(readXmlEntry(parser, elementName));
120 entries.addAll(readXml(parser, name, elementName));
126 private static XmlEntry readXmlEntry(XmlPullParser parser, String elementName) throws XmlPullParserException, IOException {
127 parser.require(XmlPullParser.START_TAG, ns, elementName);
128 XmlEntry entry = new XmlEntry(elementName);
130 int attrcount = parser.getAttributeCount();
131 Bundle attributes = new Bundle();
132 for(int i = 0; i < attrcount; i++) {
133 attributes.putString(parser.getAttributeName(i), parser.getAttributeValue(i));
135 entry.setAttributes(attributes);
137 switch (parser.next()) {
138 case XmlPullParser.START_TAG:
139 XmlEntry child = readXmlEntry(parser, parser.getName());
140 entry.addChild(child);
142 case XmlPullParser.END_TAG:
145 case XmlPullParser.TEXT:
146 entry.setValue(parser.getText());
153 private static void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
154 if (parser.getEventType() != XmlPullParser.START_TAG) {
155 throw new IllegalStateException();
159 switch (parser.next()) {
160 case XmlPullParser.END_TAG:
163 case XmlPullParser.START_TAG:
170 protected abstract void onXmlReceived(List<XmlEntry> entries);
171 protected abstract void onXmlRequestFail(int statusCode, String errorMessage);