1 /* IscRequestHelper.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.util.Log;
19 import com.loopj.android.http.AsyncHttpClient;
20 import com.loopj.android.http.AsyncHttpResponseHandler;
22 import net.fortuna.ical4j.data.CalendarBuilder;
23 import net.fortuna.ical4j.data.ParserException;
24 import net.fortuna.ical4j.model.Calendar;
26 import java.io.ByteArrayInputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.io.UnsupportedEncodingException;
31 import cz.msebera.android.httpclient.Header;
34 * Created by pk910 on 20.01.2016.
36 public abstract class IscRequestHelper {
38 protected void requestCalenderFromWeb(String url) {
39 AsyncHttpClient client = new AsyncHttpClient();
40 Log.i("HTTPClient", "Request: "+url);
41 client.get(url, new AsyncHttpResponseHandler() {
43 public void onStart() {
46 public void onSuccess(int statusCode, Header[] headers, byte[] response) {
47 String plainisc = new String(response);
48 // Google Calendar parse error!
49 plainisc = plainisc.replaceAll("(X-APPLE-TRAVEL-ADVISORY-BEHAVIOR;)?ACKNOWLEDGED[:=][0-9TZ]+(:AUTOMATIC)?[\\r\\n]*", "");
50 response = plainisc.getBytes();
52 InputStream inputStream = new ByteArrayInputStream(response);
53 parseIsc(inputStream);
56 public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
59 error = new String(errorResponse, "US-ASCII");
60 } catch (Exception e1) {
62 Log.i("HTTPClient", " Error: " + statusCode + " - " + error);
63 onCalendarRequestFail(statusCode, error);
66 public void onRetry(int retryNo) {
71 private void parseIsc(InputStream input) {
72 CalendarBuilder builder = new CalendarBuilder();
73 Calendar calendar = null;
75 calendar = builder.build(input);
76 } catch (IOException e) {
78 } catch (ParserException e) {
84 ComponentList events = calendar.getComponents(Component.VEVENT);
85 ListIterator<Component> iterator = events.listIterator();
86 while (iterator.hasNext()) {
87 Component event = iterator.next();
88 event.getProperties();
91 onCalendarReceived(calendar);
94 protected abstract void onCalendarReceived(Calendar calendar);
95 protected abstract void onCalendarRequestFail(int statusCode, String errorMessage);