package org.softwerkskammer.socrates.viewmodel;

import android.os.AsyncTask;
import android.util.JsonReader;
import android.util.Log;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.softwerkskammer.socrates.viewmodel.Session;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

public class ReadJsonAsyncTask extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... voids) {
        try {
            readJson();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static InputStream getInputStreamFromUrl(String url) {
        InputStream content = null;
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = httpclient.execute(new HttpGet(url));
            content = response.getEntity().getContent();
        } catch (Exception e) {
            Log.e("[GET REQUEST]", "Network exception", e);
        }
        return content;
    }

    private Session[] readJson() throws IOException {
        String url = "http://gist.githubusercontent.com/mknittig/1ed20cd664331049dbdd/raw/bee9682905c70e2c8745258ce337fae7b980c0f9/socrates-event-stream.json";
        BufferedReader in = new BufferedReader(new InputStreamReader(getInputStreamFromUrl(url)));

        JsonReader reader = new JsonReader(new InputStreamReader(getInputStreamFromUrl(url), "UTF-8"));
        reader.beginObject();
        String name = reader.nextName();
        Log.e("foo", name);

        return null;
    }
}
@@ -506,16 +506,127 @@
 net.URL;
+%0Aimport java.text.SimpleDateFormat;%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0Aimport java.util.Locale;
 %0A%0Apublic
@@ -809,18 +809,16 @@
  catch (
-IO
 Exceptio
@@ -840,26 +840,42 @@
     
-e.printStackTrace(
+Log.e(%22%22, %22Could not read JSON%22, e
 );%0A 
@@ -1392,18 +1392,16 @@
  throws 
-IO
 Exceptio
@@ -1437,136 +1437,58 @@
 p://
-gist.githubusercontent.com/mknittig/1ed20cd664331049dbdd/raw/bee9682905c70e2c8745258ce337fae7b980c0f9/socrates-event-stream.json
+jsonblob.com/api/jsonBlob/53e547ffe4b0bab7bd401bd6
 %22;%0A 
@@ -1732,66 +1732,929 @@
     
-String name = reader.nextName();%0A        Log.e(%22foo%22, name
+reader.nextName();%0A        reader.beginArray();%0A%0A        List%3CSession%3E sessions = new ArrayList%3CSession%3E();%0A%0A        while (reader.hasNext()) %7B%0A            reader.beginObject();%0A            Session session = new Session();%0A%0A            reader.nextName();%0A            SimpleDateFormat someFormat = new SimpleDateFormat(%22HH:mm%22, Locale.GERMANY);%0A            session.startTime = someFormat.parse(reader.nextString());%0A%0A            reader.nextName();%0A            session.duration = reader.nextString();%0A%0A            reader.nextName();%0A            session.title = reader.nextString();%0A%0A            reader.nextName();%0A            session.owner = reader.nextString();%0A%0A            reader.nextName();%0A            session.room = reader.nextString();%0A%0A            reader.endObject();%0A%0A            sessions.add(session);%0A        %7D%0A        reader.endArray();%0A        reader.endObject();%0A%0A        Log.d(%22sessions%22, sessions.toString()
 );%0A%0A

read list of sessions