empty crash file when using ACRA

2019-08-02 03:23发布

i am using the code below to generate a crash file, the file is created at parse.com but it is empty. any idea why?

Another problem is that "ACRA.DEFAULT_REPORT_FIELDS;" has an error, default report fields is not available.

public class LocalSender implements ReportSender {  
      private final Map<ReportField, String> mMapping = new HashMap<ReportField, String>() ;  
      private FileOutputStream crashReport = null;  
      private Context ctx;  
      public LocalSender(Context ct) {  
           ctx = ct;  
      }  
      public void send(CrashReportData report) throws ReportSenderException {  
           final Map<String, String> finalReport = remap(report);  
           ByteArrayOutputStream buf = new ByteArrayOutputStream();  
           Log.i("hcsh","Report send");  
           try {  
                Set set = finalReport.entrySet();  
                Iterator i = set.iterator();  
                String tmp;  
                while (i.hasNext()) {  
                     Map.Entry<String,String> me = (Map.Entry) i.next();  
                     tmp = "[" + me.getKey() + "]=" + me.getValue();  
                     buf.write(tmp.getBytes());  
                }  
                ParseFile myFile = new ParseFile("crash.txt", buf.toByteArray());  
                myFile.save();  
                ParseObject jobApplication = new ParseObject("AppCrash");  
                jobApplication.put("MyCrash", "app name");  
                jobApplication.put("applicantResumeFile", myFile);  
                try {  
                     jobApplication.save();  
                } catch (ParseException e) {  
                     // TODO Auto-generated catch block  
                     e.printStackTrace();  
                }  
           }catch (FileNotFoundException e) {  
                Log.e("TAG", "IO ERROR",e);  
           }  
           catch (IOException e) {  
                Log.e("TAG", "IO ERROR",e);  
           } catch (ParseException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
           }  
 }  
 private Map<String, String> remap(Map<ReportField, String> report) {  
      ReportField[] fields = ACRA.getConfig().customReportContent();  
      if (fields.length == 0) {  
           fields = ACRA.DEFAULT_REPORT_FIELDS;
         }  
      final Map<String, String> finalReport = new HashMap<String, String>(  
                report.size());  
      for (ReportField field : fields) {  
           if (mMapping == null || mMapping.get(field) == null) {  
                finalReport.put(field.toString(), report.get(field));  
           } else {  
                finalReport.put(mMapping.get(field), report.get(field));  
           }  
      }  
      return finalReport;  
 }

 } 

标签: android acra
2条回答
看我几分像从前
2楼-- · 2019-08-02 03:54

ACRA.DEFAULT_REPORT_FIELDS constant value is in acra-4.3.0 version ...

if you are using acra-4.5.0 version ACRA you will get this error "DEFAULT_REPORT_FIELDS cannot be resolved or is not a field" .

try to use acra-4.5.0 version and then use following code

// Extract the required data out of the crash report.

String reportBody = createCrashReport(report);

/** Extract the required data out of the crash report. */

private String createCrashReport(CrashReportData report) {

    // I've extracted only basic information.
    // U can add loads more data using the enum ReportField. See below.
    StringBuilder body = new StringBuilder();
    body.append(
            "Device : " + report.getProperty(ReportField.BRAND) + "-"
                    + report.getProperty(ReportField.PHONE_MODEL))
            .append("\n")
            .append("Android Version :"
                    + report.getProperty(ReportField.ANDROID_VERSION))
            .append("\n")
            .append("App Version : "
                    + report.getProperty(ReportField.APP_VERSION_CODE))
            .append("\n")
            .append("STACK TRACE : \n"
                    + report.getProperty(ReportField.STACK_TRACE));

    return body.toString();
}

Don't use following code ////////////////////////

final String reportBody = buildBody(arg0);

private String buildBody(CrashReportData errorContent) {

    ReportField[] fields = ACRA.getConfig().customReportContent();
    if (fields.length == 0) {
        // fields = ACRA.DEFAULT_MAIL_REPORT_FIELDS;
        fields = ACRA.DEFAULT_REPORT_FIELDS;
    }

    final StringBuilder builder = new StringBuilder();
    for (ReportField field : fields) {
        builder.append(field.toString()).append("=");
        builder.append(errorContent.get(field));
        builder.append('\n');
    }
    return builder.toString();
}

Happy Coding....

查看更多
聊天终结者
3楼-- · 2019-08-02 03:55

There is no such constant as ACRA.DEFAULT_REPORT_FIELDS. You are looking for ACRAConstants.DEFAULT_REPORT_FIELDS

查看更多
登录 后发表回答