Back to project page android.app.niuz.io.
The source code is released under:
GNU General Public License
If you think the Android project android.app.niuz.io listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package io.niuz.services; /*from w w w . j a v a2 s . c o m*/ import io.niuz.model.CategoryInformation; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.StatusLine; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.content.Context; public class CategoriesSubcategoriesService extends BaseService { private static Map<Integer, String> categoriesMap = new HashMap<Integer, String>(); private static ArrayList<HashMap<Integer, String>> subcategoriesMap = new ArrayList<HashMap<Integer, String>>(); private final static String SERVER_LINK_LIST_CATEGORIES = SERVER_URL + "listCategories/"; private final static String SERVER_LINK_LIST_SUBCATEGORIES = SERVER_URL + "listSubcategories/"; private final static String SERVER_LINK_INIT_USER_SUBCATEGORIES = SERVER_URL + "initUserSubcategories/"; public static void initCategories() throws ClientProtocolException, IOException, JSONException { JSONArray result = initJSONArrayFromLink(SERVER_LINK_LIST_CATEGORIES, "categories"); subcategoriesMap.clear(); subcategoriesMap.add(null); // Increasing size for subcategories map for (int i=0 ; i<result.length() ; i++) { JSONObject valuesObject = result.getJSONObject(i); categoriesMap.put(valuesObject.getInt("id"), valuesObject.getString("name")); subcategoriesMap.add(null); // Increasing size for subcategories map } } public static void initSubcategories() throws ClientProtocolException, IOException, JSONException { JSONArray result = initJSONArrayFromLink(SERVER_LINK_LIST_SUBCATEGORIES, "subcategories"); for (int i=0 ; i<result.length() ; i++) { JSONObject valuesObject = result.getJSONObject(i); int categoryId = valuesObject.getInt("CategoryId"); if (subcategoriesMap.get(categoryId) == null) { subcategoriesMap.add(categoryId, new HashMap<Integer, String>()); } subcategoriesMap.get(categoryId).put(valuesObject.getInt("id"), valuesObject.getString("name")); } } private static JSONArray initJSONArrayFromLink(String url, String jsonName) throws ClientProtocolException, IOException, JSONException { String getUrl = url; HttpClient httpClient = new DefaultHttpClient(); HttpResponse response = httpClient.execute(new HttpGet(getUrl)); StatusLine statusLine = response.getStatusLine(); if (statusLine.getStatusCode() == HttpStatus.SC_OK){ ByteArrayOutputStream out = new ByteArrayOutputStream(); response.getEntity().writeTo(out); out.close(); return new JSONObject(out.toString()).getJSONArray(jsonName); } else{ response.getEntity().getContent().close(); throw new IOException(statusLine.getReasonPhrase()); } } public static void initUserSubcategories(CategoryInformation[] categoryInformation, Context context) throws Exception { // Forming JSON JSONArray subcategories = new JSONArray(); for (CategoryInformation infoObject : categoryInformation) { for (Map.Entry<Integer, String> entry : infoObject.getSubcategoriesSelected().entrySet()) { JSONObject subcategory = new JSONObject(); subcategory.put("sc", entry.getKey()); subcategories.put(subcategory); } } JSONObject objectToSend = new JSONObject(); PhoneService.initPhoneNumber(context.getApplicationContext()); objectToSend.put("phoneNumber", PhoneService.getPhoneNumber()); objectToSend.put("subcategories", subcategories); // Posting request HttpPost httpPost = new HttpPost(SERVER_LINK_INIT_USER_SUBCATEGORIES); httpPost.setEntity(new StringEntity(objectToSend.toString())); httpPost.setHeader("Accept", "application/json"); httpPost.setHeader("Content-type", "application/json"); HttpClient httpClient = new DefaultHttpClient(); HttpResponse response = httpClient.execute(httpPost); // Parsing response, throwing exception if required StatusLine statusLine = response.getStatusLine(); if (statusLine.getStatusCode() == HttpStatus.SC_OK){ ByteArrayOutputStream out = new ByteArrayOutputStream(); response.getEntity().writeTo(out); out.close(); if (!new JSONObject(out.toString()).getBoolean("success")) { throw new Exception("Something went wrong during API execution!"); } } else { response.getEntity().getContent().close(); throw new Exception("Bad http status!"); } } public static Map<Integer, String> getCategoriesMap() { return categoriesMap; } public static Map<Integer, String> getSubcategoriesMap(int categoryId) { return subcategoriesMap.get(categoryId); } }