Добавлен метод получения разделов форума

This commit is contained in:
Victor 2014-01-10 15:03:50 +02:00
parent 56169feb5e
commit 62b303c3e8

View File

@ -1,6 +1,9 @@
package com.annimon.client.utils; package com.annimon.client.utils;
import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -16,8 +19,11 @@ import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import com.annimon.client.R;
import com.annimon.client.entities.ForumPost; import com.annimon.client.entities.ForumPost;
import com.annimon.client.entities.ForumSection;
import android.content.Context;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Base64; import android.util.Base64;
@ -64,6 +70,47 @@ public class ApiUtils {
return posts; return posts;
} }
/**
* Get forum sections.
* @return arraylist of forum sections from raw resource
*/
public static List<ForumSection> 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<ForumSection> sections = new ArrayList<ForumSection>();
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<NameValuePair> data) throws IOException { private static HttpResponse sendPost(String url, List<NameValuePair> data) throws IOException {
HttpClient httpclient = new DefaultHttpClient(); HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url); HttpPost httppost = new HttpPost(url);