Archived
Fikset JSON parser og innlesning for Event og News
This commit is contained in:
1 parent
a0f975d892
commit
fd3870508d
5 files changed
+145
-33
No files matched your search
@@ -1,15 +1,12 @@
|
||||
package com.studentersamfundet.app;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
import android.net.Uri;
|
||||
|
||||
public class Event implements Serializable {
|
||||
private static final long serialVersionUID = -5222901012434000257L;
|
||||
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
|
||||
|
||||
public static final String ALL = "Alle";
|
||||
|
||||
@@ -44,8 +41,8 @@ public class Event implements Serializable {
|
||||
this.image = image;
|
||||
|
||||
try {
|
||||
this.date = (date == null) ? null : dateFormat.parse(date.trim());
|
||||
} catch (ParseException e) {
|
||||
this.date = (date == null) ? null : new Date(1000 * Long.parseLong(date.trim()));
|
||||
} catch (NumberFormatException e) {
|
||||
this.date = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,14 @@
|
||||
package com.studentersamfundet.app;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
public class JSONParserNews implements IParser {
|
||||
private static final long serialVersionUID = -2654591623794449527L;
|
||||
private final DataHandler dh;
|
||||
@@ -25,9 +32,34 @@ public class JSONParserNews implements IParser {
|
||||
String content = "";
|
||||
String pubDate = "";
|
||||
|
||||
for (int i = 0;i < 0;i++) {
|
||||
dh.insertNews(title, description, content, link, pubDate);
|
||||
try {
|
||||
String jString = Utils.convertStreamToString(in);
|
||||
in.close();
|
||||
JSONObject jObject = new JSONObject(jString);
|
||||
JSONObject tempJSON;
|
||||
JSONArray eventArray = jObject.getJSONArray("posts");
|
||||
int count = jObject.getInt("count");
|
||||
|
||||
for (int i = 0;i < count;i++) {
|
||||
tempJSON = eventArray.getJSONObject(i);
|
||||
title = tempJSON.getString("title");
|
||||
description = tempJSON.getString("excerpt");
|
||||
link = tempJSON.getString("url");
|
||||
content = tempJSON.getString("content");
|
||||
pubDate = tempJSON.getString("date");
|
||||
|
||||
dh.insertNews(title, description, content, link, pubDate);
|
||||
}
|
||||
|
||||
|
||||
} catch (IOException e) {
|
||||
Log.e("json-parser", e.getMessage());
|
||||
return dh;
|
||||
} catch (JSONException e) {
|
||||
Log.e("json-parser", e.getMessage());
|
||||
return dh;
|
||||
}
|
||||
|
||||
return dh;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,14 @@
|
||||
package com.studentersamfundet.app;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
public class JSONParserProgram implements IParser {
|
||||
private static final long serialVersionUID = 3364287016121595334L;
|
||||
private final DataHandler dh;
|
||||
@@ -19,33 +26,71 @@ public class JSONParserProgram implements IParser {
|
||||
}
|
||||
|
||||
public DataHandler parse(InputStream in) {
|
||||
for (int i = 0; i < 0; i++) {
|
||||
String id = "";
|
||||
String title = "";
|
||||
String description = "";
|
||||
String date = "";
|
||||
String location = "";
|
||||
String text = "";
|
||||
String category = "";
|
||||
String image = "";
|
||||
String priceReg = "";
|
||||
String priceMem = "";
|
||||
String ticketUrl = "";
|
||||
String id = "";
|
||||
String title = "";
|
||||
String description = "";
|
||||
String text = "";
|
||||
String image = "";
|
||||
|
||||
String date = "";
|
||||
String location = "";
|
||||
String category = "";
|
||||
String priceReg = "";
|
||||
String priceMem = "";
|
||||
String ticketUrl = "";
|
||||
|
||||
try {
|
||||
String jString = Utils.convertStreamToString(in);
|
||||
in.close();
|
||||
JSONObject jObject = new JSONObject(jString);
|
||||
JSONObject tempJSON;
|
||||
JSONArray eventArray = jObject.getJSONArray("events");
|
||||
int count = jObject.getInt("count");
|
||||
|
||||
// This is as ugly as it gets. Refactor?
|
||||
dh.insertEvent(id,
|
||||
title,
|
||||
description,
|
||||
date,
|
||||
location,
|
||||
text,
|
||||
category,
|
||||
image,
|
||||
priceReg,
|
||||
priceMem,
|
||||
ticketUrl);
|
||||
for (int i = 0;i < count;i++) {
|
||||
// Variables directly from the JSONObject
|
||||
tempJSON = eventArray.getJSONObject(i);
|
||||
id = tempJSON.getString("id");
|
||||
title = tempJSON.getString("title");
|
||||
description = tempJSON.getString("excerpt");
|
||||
date = tempJSON.getString("date");
|
||||
text = tempJSON.getString("content");
|
||||
image = tempJSON.getString("thumbnail");
|
||||
|
||||
tempJSON = tempJSON.getJSONObject("custom_fields");
|
||||
ticketUrl = tempJSON.getJSONArray("_neuf_events_bs_url").getString(0);
|
||||
location = tempJSON.getJSONArray("_neuf_events_venue").getString(0);
|
||||
priceReg = tempJSON.getJSONArray("_neuf_events_price_regular").getString(0);
|
||||
priceMem = priceReg;
|
||||
date = tempJSON.getJSONArray("_neuf_events_starttime").getString(0);
|
||||
|
||||
|
||||
// This is as ugly as it gets. Refactor?
|
||||
dh.insertEvent(id,
|
||||
title,
|
||||
description,
|
||||
date,
|
||||
location,
|
||||
text,
|
||||
category,
|
||||
image,
|
||||
priceReg,
|
||||
priceMem,
|
||||
ticketUrl);
|
||||
}
|
||||
|
||||
|
||||
} catch (IOException e) {
|
||||
Log.e("json-parser", e.getMessage());
|
||||
return dh;
|
||||
} catch (JSONException e) {
|
||||
Log.e("json-parser", e.getMessage());
|
||||
return dh;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
return dh;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,13 @@
|
||||
package com.studentersamfundet.app;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
|
||||
public class Utils {
|
||||
public static String intToMonthStr(int monthInt) {
|
||||
String monthString;
|
||||
@@ -27,4 +35,34 @@ public class Utils {
|
||||
day = day.substring(1);
|
||||
return day;
|
||||
}
|
||||
|
||||
public static String convertStreamToString(InputStream is)
|
||||
throws IOException {
|
||||
/*
|
||||
* To convert the InputStream to String we use the
|
||||
* Reader.read(char[] buffer) method. We iterate until the
|
||||
* Reader return -1 which means there's no more data to
|
||||
* read. We use the StringWriter class to produce the string.
|
||||
*/
|
||||
if (is != null) {
|
||||
Writer writer = new StringWriter();
|
||||
|
||||
char[] buffer = new char[1024];
|
||||
try {
|
||||
Reader reader = new BufferedReader(
|
||||
new InputStreamReader(is, "UTF-8"));
|
||||
int n;
|
||||
while ((n = reader.read(buffer)) != -1) {
|
||||
writer.write(buffer, 0, n);
|
||||
}
|
||||
} finally {
|
||||
is.close();
|
||||
}
|
||||
return writer.toString();
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -20,8 +20,8 @@ import com.studentersamfundet.app.ui.lists.EventListTicketsActivity;
|
||||
import com.studentersamfundet.app.ui.lists.NewsListActivity;
|
||||
|
||||
public class DnsActivity extends BaseDnsActivity {
|
||||
public static final String eventFeedURL = "http://studentersamfundet.no/rss/robert_program_feed.php";
|
||||
public static final String newsFeedURL = "http://studentersamfundet.no/rss/nyheter_feed.php";
|
||||
public static final String eventFeedURL = "http://studentersamfundet.no/api/events/get_upcoming/";
|
||||
public static final String newsFeedURL = "http://studentersamfundet.no/?json=1";
|
||||
|
||||
private FeedFetcher eventFeedFetcher;
|
||||
private FeedFetcher newsFeedFetcher;
|
||||
|
||||
Reference in new issue
Block a user