From 62b303c3e8276dccddfd13dace1a863e83d0ceb4 Mon Sep 17 00:00:00 2001 From: Victor Date: Fri, 10 Jan 2014 15:03:50 +0200 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=20=D0=BF=D0=BE=D0=BB?= =?UTF-8?q?=D1=83=D1=87=D0=B5=D0=BD=D0=B8=D1=8F=20=D1=80=D0=B0=D0=B7=D0=B4?= =?UTF-8?q?=D0=B5=D0=BB=D0=BE=D0=B2=20=D1=84=D0=BE=D1=80=D1=83=D0=BC=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/annimon/client/utils/ApiUtils.java | 47 ++++++++++++++++++++++ 1 file changed, 47 insertions(+) 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);