net.hockeyapp.android.utils.HttpURLConnectionBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of HockeySDK Show documentation
Show all versions of HockeySDK Show documentation
HockeySDK-Android implements support for using HockeyApp in your Android application. The following features are currently supported:
Collect crash reports:If your app crashes, a crash log is written to the device's storage. If the user starts the app again, they will be asked asked to submit the crash report to HockeyApp. This works for both beta and live apps, i.e. those submitted to Google Play or other app stores. Crash logs contain viable information for you to help resolve the issue. Furthermore, you as a developer can add additional information to the report as well.
Update Alpha/Beta apps: The app will check with HockeyApp if a new version for your alpha/beta build is available. If yes, it will show a dialog to users and let them see the release notes, the version history and start the installation process right away. You can even force the installation of certain updates.
User Metrics: Understand user behavior to improve your app. Track usage through daily and monthly active users. Monitor crash impacted users. Measure customer engagement through session count. Add custom tracking calls to learn which features your users are actually using. This feature requires a minimum API level of 14 (Android 4.x Ice Cream Sandwich).
Feedback: Besides crash reports, collecting feedback from your users from within your app is a great option to help with improving your app. You act on and answer feedback directly from the HockeyApp backend.
Authenticate: Identify and authenticate users against your registered testers with the HockeyApp backend.
package net.hockeyapp.android.utils;
import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Description
*
* Builder class for HttpURLConnection.
*
* License
*
*
* Copyright (c) 2011-2015 Bit Stadium GmbH
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*
* @author Matthias Wenz
**/
public class HttpURLConnectionBuilder {
private static final int DEFAULT_TIMEOUT = 2 * 60 * 1000;
public static final String DEFAULT_CHARSET = "UTF-8";
private final String mUrlString;
private String mRequestMethod;
private String mRequestBody;
private SimpleMultipartEntity mMultipartEntity;
private int mTimeout = DEFAULT_TIMEOUT;
private final Map mHeaders;
public HttpURLConnectionBuilder(String urlString) {
mUrlString = urlString;
mHeaders = new HashMap();
}
public HttpURLConnectionBuilder setRequestMethod(String requestMethod) {
mRequestMethod = requestMethod;
return this;
}
public HttpURLConnectionBuilder setRequestBody(String requestBody) {
mRequestBody = requestBody;
return this;
}
public HttpURLConnectionBuilder writeFormFields(Map fields) {
try {
String formString = getFormString(fields, DEFAULT_CHARSET);
setHeader("Content-Type", "application/x-www-form-urlencoded");
setRequestBody(formString);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
return this;
}
public HttpURLConnectionBuilder writeMultipartData(Map fields, Context context, List attachmentUris) {
try {
mMultipartEntity = new SimpleMultipartEntity();
mMultipartEntity.writeFirstBoundaryIfNeeds();
for (String key : fields.keySet()) {
mMultipartEntity.addPart(key, fields.get(key));
}
for (int i = 0; i < attachmentUris.size(); i++) {
Uri attachmentUri = attachmentUris.get(i);
boolean lastFile = (i == attachmentUris.size() - 1);
InputStream input = context.getContentResolver().openInputStream(attachmentUri);
String filename = attachmentUri.getLastPathSegment();
mMultipartEntity.addPart("attachment" + i, filename, input, lastFile);
}
mMultipartEntity.writeLastBoundaryIfNeeds();
setHeader("Content-Type", "multipart/form-data; boundary=" + mMultipartEntity.getBoundary());
} catch (IOException e) {
throw new RuntimeException(e);
}
return this;
}
public HttpURLConnectionBuilder setTimeout(int timeout) {
if (timeout < 0) {
throw new IllegalArgumentException("Timeout has to be positive.");
}
mTimeout = timeout;
return this;
}
public HttpURLConnectionBuilder setHeader(String name, String value) {
mHeaders.put(name, value);
return this;
}
public HttpURLConnectionBuilder setBasicAuthorization(String username, String password) {
String authString = "Basic " + net.hockeyapp.android.utils.Base64.encodeToString(
(username + ":" + password).getBytes(), android.util.Base64.NO_WRAP);
setHeader("Authorization", authString);
return this;
}
public HttpURLConnection build() {
HttpURLConnection connection;
try {
URL url = new URL(mUrlString);
connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(mTimeout);
connection.setReadTimeout(mTimeout);
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD) {
connection.setRequestProperty("Connection", "close");
}
if (!TextUtils.isEmpty(mRequestMethod)) {
connection.setRequestMethod(mRequestMethod);
if (!TextUtils.isEmpty(mRequestBody) || mRequestMethod.equalsIgnoreCase("POST") || mRequestMethod.equalsIgnoreCase("PUT")) {
connection.setDoOutput(true);
}
}
for (String name : mHeaders.keySet()) {
connection.setRequestProperty(name, mHeaders.get(name));
}
if (!TextUtils.isEmpty(mRequestBody)) {
OutputStream outputStream = connection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, DEFAULT_CHARSET));
writer.write(mRequestBody);
writer.flush();
writer.close();
}
if (mMultipartEntity != null) {
connection.setRequestProperty("Content-Length", String.valueOf(mMultipartEntity.getContentLength()));
BufferedOutputStream outputStream = new BufferedOutputStream(connection.getOutputStream());
outputStream.write(mMultipartEntity.getOutputStream().toByteArray());
outputStream.flush();
outputStream.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return connection;
}
private static String getFormString(Map params, String charset) throws UnsupportedEncodingException {
List protoList = new ArrayList();
for (String key : params.keySet()) {
String value = params.get(key);
key = URLEncoder.encode(key, charset);
value = URLEncoder.encode(value, charset);
protoList.add(key + "=" + value);
}
return TextUtils.join("&", protoList);
}
}