package org.myLazyClock.calendarApi;/* * myLazyClock * * Copyright (C) 2014 * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ import com.google.gson.JsonElement; import com.google.gson.JsonParser; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.Map; /** * Created on 30/11/14. * * @author dralagen */ public class JsonConverter { private static String urlSmartEdt = "http://smart-edt.fr/android/data.php"; public static JsonElement getJson(Map<String, String> params) throws IOException { URL url = new URL(urlSmartEdt); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setDoInput(true); conn.setDoOutput(true); OutputStream os = conn.getOutputStream(); BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(os, "UTF-8")); writer.write(getQuery(params)); writer.flush(); writer.close(); os.close(); conn.connect(); JsonParser jp = new JsonParser(); return jp.parse(new InputStreamReader((InputStream) conn.getContent())); } public static String getQuery(Map<String, String> params) throws UnsupportedEncodingException { StringBuilder result = new StringBuilder(); boolean first = true; for (Map.Entry<String, String> pair : params.entrySet()) { if (first) first = false; else result.append("&"); result.append(URLEncoder.encode(pair.getKey(), "UTF-8")); result.append("="); result.append(URLEncoder.encode(pair.getValue(), "UTF-8")); } return result.toString(); } }