Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package com.codingtrickshub.checkboxlistview.serverCalls;
import com.codingtrickshub.checkboxlistview.model.Category;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import org.apache.http.util.EntityUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
public class FavouriteCategoriesJsonParser {
public static ArrayList<String> selectedCategories = new ArrayList<>();
public ArrayList<Category> getParsedCategories() {
String JsonFavouriteCategories = "";
ArrayList<Category> MyArraylist = new ArrayList<>();
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://xxx.xxx.x.xxx:(port)/CheckBoxListView/getFavouriteCategories.php?email=tutorials@codingtrickshub.com");
try {
HttpResponse httpResponse = httpClient.execute(httpGet);
JsonFavouriteCategories = EntityUtils.toString(httpResponse.getEntity());
JSONArray jsonArray = new JSONArray(JsonFavouriteCategories);
for (int i = 0; i < jsonArray.length(); i++) {
Category genres = new Category();
JSONObject MyJsonObject = jsonArray.getJSONObject(i);
genres.setCateogry_id(Integer.parseInt(MyJsonObject.getString("id")));
genres.setCategory_Name(MyJsonObject.getString("categoryName"));
genres.setSelected(Boolean.parseBoolean(MyJsonObject.getString("selected")));
MyArraylist.add(genres);
if (MyJsonObject.getString("selected").equals("true")) {
selectedCategories.add(MyJsonObject.getString("id"));
}
}
} catch (Exception e) {
e.printStackTrace();
}
return MyArraylist;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.