Java tutorial
/* * Copyright (C) 2012 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.isol.app.tracker; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import org.json.JSONArray; import org.json.JSONObject; import android.content.SharedPreferences; import android.os.StrictMode; import android.preference.PreferenceManager; import com.google.android.gms.plus.model.moments.ItemScope; /** * Handles creation of moment JSON. */ public class Utilita { /** * A mapping of moment type to target URL. */ public static final HashMap<String, String> MOMENT_TYPES; /** * A list of moment target types. */ public static final ArrayList<String> MOMENT_LIST; public static final String[] ACTIONS; static { MOMENT_TYPES = new HashMap<String, String>(9); MOMENT_TYPES.put("AddActivity", "https://developers.google.com/+/plugins/snippet/examples/thing"); MOMENT_TYPES.put("BuyActivity", "https://developers.google.com/+/plugins/snippet/examples/a-book"); MOMENT_TYPES.put("CheckInActivity", "https://developers.google.com/+/plugins/snippet/examples/place"); MOMENT_TYPES.put("CommentActivity", "https://developers.google.com/+/plugins/snippet/examples/blog-entry"); MOMENT_TYPES.put("CreateActivity", "https://developers.google.com/+/plugins/snippet/examples/photo"); MOMENT_TYPES.put("ListenActivity", "https://developers.google.com/+/plugins/snippet/examples/song"); MOMENT_TYPES.put("ReserveActivity", "https://developers.google.com/+/plugins/snippet/examples/restaurant"); MOMENT_TYPES.put("ReviewActivity", "https://developers.google.com/+/plugins/snippet/examples/widget"); MOMENT_LIST = new ArrayList<String>(Utilita.MOMENT_TYPES.keySet()); Collections.sort(MOMENT_LIST); ACTIONS = MOMENT_TYPES.keySet().toArray(new String[0]); int count = ACTIONS.length; for (int i = 0; i < count; i++) { ACTIONS[i] = "http://schemas.google.com/" + ACTIONS[i]; } } // /** // * Generates the "result" JSON object for select moments. // * // * @param momentType The type of the moment. // */ // public static ItemScope getResultFor(String momentType) { // if (momentType.equals("CommentActivity")) { // return getCommentActivityResult(); // } // if (momentType.equals("ReserveActivity")) { // return getReserveActivityResult(); // } // if (momentType.equals("ReviewActivity")) { // return getReviewActivityResult(); // } // return null; // } // /** // * Generates the "result" JSON object for CommentActivity moment. // */ // private static ItemScope getCommentActivityResult() { // return new ItemScope.Builder() // .setType("http://schema.org/Comment") // .setUrl("https://developers.google.com/+/plugins/snippet/examples/blog-entry#comment-1") // .setName("This is amazing!") // .setText("I can't wait to use it on my site!") // .build(); // } // // /** // * Generates the "result" JSON object for ReserveActivity moment. // */ // private static ItemScope getReserveActivityResult() { // return new ItemScope.Builder() // .setType("http://schemas.google.com/Reservation") // .setStartDate("2012-06-28T19:00:00-08:00") // .setAttendeeCount(3) // .build(); // } // // /** // * Generates the "result" JSON object for ReviewActivity moment. // */ // private static ItemScope getReviewActivityResult() { // ItemScope rating = new ItemScope.Builder() // .setType("http://schema.org/Rating") // .setRatingValue("100") // .setBestRating("100") // .setWorstRating("0") // .build(); // // return new ItemScope.Builder() // .setType("http://schema.org/Review") // .setName("A Humble Review of Widget") // .setUrl("https://developers.google.com/+/plugins/snippet/examples/review") // .setText("It is amazingly effective") // .setReviewRating(rating) // .build(); // } // // public static JSONArray getJSONArray(String parms) throws JSONException,IOException, MalformedURLException public static JSONArray getJSONArray(String parms) { try { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); String res = getHttpData(Constants.serviceURL + parms); return new JSONArray(res); } catch (Exception ex) { return null; } } public static JSONObject getJSONObjectFromArray(String parms) { try { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); String res = getHttpData(Constants.serviceURL + parms); JSONArray jArray = new JSONArray(res); JSONObject obj = jArray.getJSONObject(0); return obj; } catch (Exception ex) { return null; } } // public static JSONObject getJSONObject(String parms) throws JSONException,IOException, MalformedURLException public static JSONObject getJSONObject(String parms) { try { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); String res = getHttpData(Constants.serviceURL + parms); return new JSONObject(res); } catch (Exception ex) { return null; } } public static void JSONAction(String parms) { try { StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); String res = getHttpData(Constants.serviceURL + parms); } catch (Exception ex) { } } public static String getHttpData(String url) throws IOException, MalformedURLException { HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); InputStream in = conn.getInputStream(); try { StringBuilder sb = new StringBuilder(); BufferedReader r = new BufferedReader(new InputStreamReader(new DoneHandlerInputStream(in))); for (String line = r.readLine(); line != null; line = r.readLine()) { sb.append(line); } r.close(); return sb.toString(); } finally { in.close(); } } }