Last active
December 17, 2015 02:59
-
-
Save matecode/5540392 to your computer and use it in GitHub Desktop.
ACRA Sender for Bugify ( http://bugify.com ) ( Doc below the file as comment )
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.net.URL; | |
import java.util.HashMap; | |
import java.util.Map; | |
import org.acra.ACRA; | |
import org.acra.ReportField; | |
import org.acra.collector.CrashReportData; | |
import org.acra.sender.ReportSender; | |
import org.acra.sender.ReportSenderException; | |
import org.acra.util.HttpRequest; | |
import android.net.Uri; | |
import android.util.Log; | |
public class BugifySender implements ReportSender { | |
private static final String LOGTAG = ACRA.LOG_TAG; | |
private static final String PATH_POST_ISSUE = "issues.json"; | |
private static final String FIELD_SUBJECT = "subject"; | |
private static final String FIELD_DESCRIPTION = "description"; | |
private static final String FIELD_PROJECT = "project"; | |
private static final String FIELD_CATEGORY = "category"; | |
private Uri apiUrl; | |
private String apiKey; | |
private int project; | |
private int category; | |
public BugifySender(String apiUrl, String apiKey, int project, int category) { | |
this.apiUrl = Uri.parse(apiUrl); | |
this.apiKey = apiKey; | |
this.project = project; | |
this.category = category; | |
} | |
public BugifySender(String apiUrl, String apiKey) { | |
this(apiUrl,apiKey,0,0); | |
} | |
public BugifySender(String apiUrl, String apiKey, int project) { | |
this(apiUrl,apiKey,project,0); | |
} | |
private URL getUrl(String path) throws IOException | |
{ | |
try { | |
String apiUrlString = apiUrl.toString(); | |
if (!apiUrlString.endsWith("/")) | |
apiUrlString += "/"; | |
return new URL(apiUrlString+path); | |
} catch (Exception e) { | |
throw new IOException("Error creating report url", e); | |
} | |
} | |
@Override | |
public void send(CrashReportData report) throws ReportSenderException { | |
try { | |
final Map<String, String> finalReport = getHTTPRequestFields(report); | |
final URL reportUrl = getUrl(PATH_POST_ISSUE); | |
Log.i(LOGTAG,"sending CrashReport to "+reportUrl.toString()); | |
HttpRequest request = new HttpRequest(); | |
request.setConnectionTimeOut(ACRA.getConfig().connectionTimeout()); | |
request.setSocketTimeOut(ACRA.getConfig().socketTimeout()); | |
request.setMaxNrRetries(ACRA.getConfig().maxNumberOfRequestRetries()); | |
request.setLogin(apiKey); | |
request.sendPost(reportUrl, finalReport); | |
} catch (IOException e) { | |
throw new ReportSenderException("Error while sending report to Bugify", e); | |
} | |
} | |
private Map<String, String> getHTTPRequestFields(CrashReportData data) { | |
final Map<String, String> finalReport = new HashMap<String, String>(); | |
finalReport.put(FIELD_SUBJECT, buildSubject(data)); | |
finalReport.put(FIELD_DESCRIPTION, buildDescription(data)); | |
if (project != 0) | |
finalReport.put(FIELD_PROJECT, String.valueOf(project)); | |
if (category != 0) | |
finalReport.put(FIELD_CATEGORY, String.valueOf(category)); | |
return finalReport; | |
} | |
private String buildSubject(CrashReportData data) | |
{ | |
StringBuilder sb = new StringBuilder(); | |
sb.append(data.get(ReportField.PACKAGE_NAME)); | |
sb.append(" ").append(data.get(ReportField.APP_VERSION_NAME)); | |
sb.append(" ("); | |
sb.append(data.get(ReportField.ANDROID_VERSION)); | |
sb.append(" ").append(data.get(ReportField.PHONE_MODEL)); | |
sb.append(") Chrash Report"); | |
if (data.get(ReportField.USER_COMMENT) != null) | |
{ | |
if (!data.get(ReportField.USER_COMMENT).equals("")) | |
sb.append(" with user comment"); | |
} | |
return sb.toString(); | |
} | |
private String buildDescription(CrashReportData data) | |
{ | |
ReportField[] fields = ACRA.getConfig().customReportContent(); | |
if (fields.length == 0) { | |
fields = ACRA.DEFAULT_REPORT_FIELDS; | |
} | |
StringBuilder sb = new StringBuilder(); | |
for (ReportField field : fields) { | |
Log.d(LOGTAG,"Adding field: "+field.toString()); | |
sb.append("##").append(field.toString()).append("\n\n"); | |
String fieldData = data.get(field); | |
if (fieldData == null) | |
fieldData = ""; | |
if (fieldData.equals("") ) | |
sb.append("_not set_\n"); | |
else | |
{ | |
String fieldDataLines[] = fieldData.split("\n"); | |
for (int i = 0; i<fieldDataLines.length;i++) | |
{ | |
StringBuilder fieldDataLineBuilder = new StringBuilder(" "); | |
fieldDataLineBuilder.append(fieldDataLines[i]).append("\n"); | |
sb.append(fieldDataLineBuilder.toString()); | |
} | |
} | |
sb.append('\n'); | |
} | |
return sb.toString(); | |
} | |
} |
This doesn't work anymore. You should change
request.sendPost(reportUrl, finalReport);
to
request.send(reportUrl, Method.POST, reportAsString, Type.FORM);
and add
String reportAsString = HttpRequest.getParamsAsFormString(finalReport);
above it.
Moreover, you should change
ACRA.DEFAULT_REPORT_FIELDS
to
ACRAConstants.DEFAULT_REPORT_FIELDS
(and of course import everything correctly).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ACRA Sender for Bugify ( http://bugify.com )
Just use it like described in ACRA documentation ( https://github.com/ACRA/acra/wiki/AdvancedUsage#wiki-Implementing_your_own_sender )