Skip to content

Instantly share code, notes, and snippets.

@ashtom
Created March 1, 2012 09:26
Show Gist options
  • Save ashtom/1948529 to your computer and use it in GitHub Desktop.
Save ashtom/1948529 to your computer and use it in GitHub Desktop.
Custom ReportSender for HockeyApp and ACRA
package net.hockeyapp.android.demo;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.acra.ACRA;
import org.acra.collector.CrashReportData;
import org.acra.ReportField;
import org.acra.sender.ReportSender;
import org.acra.sender.ReportSenderException;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
public class HockeySender implements ReportSender {
private static String BASE_URL = "https://rink.hockeyapp.net/api/2/apps/";
private static String CRASHES_PATH = "/crashes";
@Override
public void send(CrashReportData report) throws ReportSenderException {
String log = createCrashLog(report);
String url = BASE_URL + ACRA.getConfig().formKey() + CRASHES_PATH;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("raw", log));
parameters.add(new BasicNameValuePair("userID", report.get(ReportField.INSTALLATION_ID)));
parameters.add(new BasicNameValuePair("contact", report.get(ReportField.USER_EMAIL)));
parameters.add(new BasicNameValuePair("description", report.get(ReportField.USER_COMMENT)));
httpPost.setEntity(new UrlEncodedFormEntity(parameters, HTTP.UTF_8));
httpClient.execute(httpPost);
}
catch (Exception e) {
e.printStackTrace();
}
}
private String createCrashLog(CrashReportData report) {
Date now = new Date();
StringBuilder log = new StringBuilder();
log.append("Package: " + report.get(ReportField.PACKAGE_NAME) + "\n");
log.append("Version: " + report.get(ReportField.APP_VERSION_CODE) + "\n");
log.append("Android: " + report.get(ReportField.ANDROID_VERSION) + "\n");
log.append("Manufacturer: " + android.os.Build.MANUFACTURER + "\n");
log.append("Model: " + report.get(ReportField.PHONE_MODEL) + "\n");
log.append("Date: " + now + "\n");
log.append("\n");
log.append(report.get(ReportField.STACK_TRACE));
return log.toString();
}
}
@Leandros
Copy link

See a improved version of this Gist in the forked Gists (https://gist.github.com/4635252). Would be awesome if you would add it to this Gist.

@davcamer
Copy link

@Leandros what improvements have you made in your gist?

At least at this point, they look identical. Maybe it has been merged?

@kariem
Copy link

kariem commented Feb 19, 2014

Hockey extended their meta-data with separate version code and name (see Set both versionCode and versionName in crash logs). I have added this in my fork.

@kariem
Copy link

kariem commented Feb 19, 2014

Also, for @Leandros changes, check out the corresponding revision log.

@cypressious
Copy link

DefaultHttpclient is removed since Android 6.0

@cypressious
Copy link

Check my fork for a version with OkHTTP written in Kotlin: https://gist.github.com/cypressious/ce618458f42ac6459b14

@Frank1234
Copy link

formKey is deprecated from Acra 4.5+. Use this is your apllication class:

    @ReportsCrashes(buildConfigClass = MyApplication.class)
    public class MyApplication extends Application {
        ...

and in your HockeySender:

    @Override
    public void send(Context context, CrashReportData errorContent) throws ReportSenderException {

        String formKey = context.getString(R.string.acra_form_key);
        String url = BASE_URL + formKey + CRASHES_PATH;
        ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment