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,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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user