diff --git a/src/com/annimon/client/utils/ApiUtils.java b/src/com/annimon/client/utils/ApiUtils.java index 215f115..f3e99d9 100644 --- a/src/com/annimon/client/utils/ApiUtils.java +++ b/src/com/annimon/client/utils/ApiUtils.java @@ -1,6 +1,9 @@ package com.annimon.client.utils; +import java.io.BufferedReader; import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; @@ -16,8 +19,11 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import com.annimon.client.R; import com.annimon.client.entities.ForumPost; +import com.annimon.client.entities.ForumSection; +import android.content.Context; import android.text.TextUtils; import android.util.Base64; @@ -64,6 +70,47 @@ public class ApiUtils { return posts; } + /** + * Get forum sections. + * @return arraylist of forum sections from raw resource + */ + public static List getForumSections(Context context) { + InputStream is = context.getResources().openRawResource(R.raw.forum); + StringBuilder sb = new StringBuilder(); + try { + BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); + int length; + char[] buffer = new char[1024]; + while ((length = reader.read(buffer)) != -1) { + sb.append(buffer, 0, length); + } + reader.close(); + } catch (IOException ex) { + ExceptionHandler.log("getForumSections RAW", ex); + } + + String jsonString = sb.toString(); + List sections = new ArrayList(); + try { + if (TextUtils.isEmpty(jsonString)) return sections; + + JSONArray jsonArray = new JSONArray(jsonString); + for (int i = 0; i < jsonArray.length(); i++) { + JSONObject jsonObject = jsonArray.getJSONObject(i); + sections.add(new ForumSection(jsonObject.getString("name"))); + JSONArray subsections = jsonObject.getJSONArray("topics"); + for (int j = 0; j < subsections.length(); j++) { + int id = subsections.getJSONObject(j).getInt("id"); + String name = subsections.getJSONObject(j).getString("name"); + sections.add(new ForumSection(id, name)); + } + } + } catch (JSONException ex) { + ExceptionHandler.log("getForumSections", ex); + } + return sections; + } + private static HttpResponse sendPost(String url, List data) throws IOException { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url);