Java tutorial
/* * * Copyright 2016 Michael A Updike * * 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.weebly.opus1269.copyeverywhere.cloud; import android.support.v4.util.SimpleArrayMap; import android.util.Log; import com.weebly.opus1269.copyeverywhere.BuildConfig; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; /** * Class representing an HTTP request */ @SuppressWarnings("MagicCharacter") public class HttpRequest { private static final String TAG = "HttpRequest"; public static final String HEADER_CONTENT_TYPE = "Content-Type"; public static final String HEADER_ACCEPT = "Accept"; public static final String HEADER_PROJECT_ID = "project_id"; public static final String HEADER_AUTHORIZATION = "Authorization"; public static final String CONTENT_TYPE_FORM_ENCODED = "application/x-www-form-urlencoded"; public static final String CONTENT_TYPE_JSON = "application/json"; private static final String UTF_8 = "UTF-8"; private final SimpleArrayMap<String, String> mHeaders = new SimpleArrayMap<>(); private int mResponseCode = 404; private String mResponseBody = ""; /** * Add a request header * * @param name the header's name * @param value the header's value */ public void setHeader(String name, String value) { mHeaders.put(name, value); } /** * @return this request's response code */ public int getResponseCode() { return mResponseCode; } /** * @return this request's response body */ public String getResponseBody() { return mResponseBody; } /** * Post the request * * @param url where to post to * @param requestBody the body of the request * @throws IOException */ public void doPost(String url, String requestBody) throws IOException { if (BuildConfig.DEBUG) { Log.i(TAG, "HTTP request. body: " + requestBody); } final HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); conn.setDoOutput(true); conn.setUseCaches(false); conn.setFixedLengthStreamingMode(requestBody.getBytes(UTF_8).length); conn.setRequestMethod("POST"); for (int i = 0; i < mHeaders.size(); i++) { conn.setRequestProperty(mHeaders.keyAt(i), mHeaders.valueAt(i)); } OutputStream out = null; try { out = conn.getOutputStream(); out.write(requestBody.getBytes(UTF_8)); } finally { if (out != null) { try { out.close(); } catch (final IOException ignored) { // Ignore. } } } mResponseCode = conn.getResponseCode(); InputStream inputStream = null; try { if (mResponseCode == 200) { inputStream = conn.getInputStream(); } else { inputStream = conn.getErrorStream(); } mResponseBody = getString(inputStream); } finally { if (inputStream != null) { try { inputStream.close(); } catch (final IOException ignored) { // Ignore. } } } if (BuildConfig.DEBUG) { Log.i(TAG, "HTTP response. body: " + mResponseBody); } conn.disconnect(); } /** * Convenience method to convert an InputStream to a String. * <p/> * If the stream ends in a newline character, it will be stripped. * <p/> * If the stream is {@literal null}, returns an empty string. */ private static String getString(InputStream stream) throws IOException { if (stream == null) { return ""; } final StringBuilder content = new StringBuilder(0); final BufferedReader reader = new BufferedReader(new InputStreamReader(stream, UTF_8)); try { String newLine; do { newLine = reader.readLine(); if (newLine != null) { content.append(newLine).append('\n'); } } while (newLine != null); } finally { reader.close(); } if (content.length() > 0) { // strip last newline content.setLength(content.length() - 1); } return content.toString(); } }