将文件上传到Dropbox

时间:2013-09-11 20:38:59

标签: android dropbox dropbox-api

我正在尝试将文件上传到Dropbox。我从官方网站上关注了教程,但现在我的应用程序崩溃了,我在logCat中得到了这个:

09-11 22:19:13.519: E/dalvikvm(2856): Could not find class 'org.json.simple.JSONArray', referenced from method com.dropbox.client2.DropboxAPI.revisions
09-11 22:19:13.519: W/dalvikvm(2856): VFY: unable to resolve check-cast 296 (Lorg/json/simple/JSONArray;) in Lcom/dropbox/client2/DropboxAPI;
09-11 22:19:13.519: D/dalvikvm(2856): VFY: replacing opcode 0x1f at 0x0053
09-11 22:19:13.519: E/dalvikvm(2856): Could not find class 'org.json.simple.JSONArray', referenced from method com.dropbox.client2.DropboxAPI.search
09-11 22:19:13.519: W/dalvikvm(2856): VFY: unable to resolve instanceof 296 (Lorg/json/simple/JSONArray;) in Lcom/dropbox/client2/DropboxAPI;
09-11 22:19:13.519: D/dalvikvm(2856): VFY: replacing opcode 0x20 at 0x006e
09-11 22:19:13.639: E/dalvikvm(2856): Could not find class 'org.json.simple.parser.JSONParser', referenced from method com.dropbox.client2.RESTUtility.parseAsJSON
09-11 22:19:13.639: W/dalvikvm(2856): VFY: unable to resolve new-instance 298 (Lorg/json/simple/parser/JSONParser;) in Lcom/dropbox/client2/RESTUtility;
09-11 22:19:13.639: D/dalvikvm(2856): VFY: replacing opcode 0x22 at 0x001d
09-11 22:19:13.639: W/dalvikvm(2856): VFY: unable to resolve exception class 299 (Lorg/json/simple/parser/ParseException;)
09-11 22:19:13.639: W/dalvikvm(2856): VFY: unable to find exception handler at addr 0x50
09-11 22:19:13.639: W/dalvikvm(2856): VFY:  rejected Lcom/dropbox/client2/RESTUtility;.parseAsJSON (Lorg/apache/http/HttpResponse;)Ljava/lang/Object;
09-11 22:19:13.639: W/dalvikvm(2856): VFY:  rejecting opcode 0x0d at 0x0050
09-11 22:19:13.639: W/dalvikvm(2856): VFY:  rejected Lcom/dropbox/client2/RESTUtility;.parseAsJSON (Lorg/apache/http/HttpResponse;)Ljava/lang/Object;
09-11 22:19:13.639: W/dalvikvm(2856): Verifier rejected class Lcom/dropbox/client2/RESTUtility;
09-11 22:19:13.639: D/AndroidRuntime(2856): Shutting down VM
09-11 22:19:13.639: W/dalvikvm(2856): threadid=1: thread exiting with uncaught exception (group=0x40a361f8)
09-11 22:19:13.649: E/AndroidRuntime(2856): FATAL EXCEPTION: main
09-11 22:19:13.649: E/AndroidRuntime(2856): java.lang.VerifyError: com/dropbox/client2/RESTUtility
09-11 22:19:13.649: E/AndroidRuntime(2856):     at com.dropbox.client2.android.AuthActivity.startWebAuth(AuthActivity.java:421)
09-11 22:19:13.649: E/AndroidRuntime(2856):     at com.dropbox.client2.android.AuthActivity.onResume(AuthActivity.java:289)
09-11 22:19:13.649: E/AndroidRuntime(2856):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1154)
09-11 22:19:13.649: E/AndroidRuntime(2856):     at android.app.Activity.performResume(Activity.java:4539)
09-11 22:19:13.649: E/AndroidRuntime(2856):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2434)
09-11 22:19:13.649: E/AndroidRuntime(2856):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2472)
09-11 22:19:13.649: E/AndroidRuntime(2856):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1986)
09-11 22:19:13.649: E/AndroidRuntime(2856):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
09-11 22:19:13.649: E/AndroidRuntime(2856):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
09-11 22:19:13.649: E/AndroidRuntime(2856):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-11 22:19:13.649: E/AndroidRuntime(2856):     at android.os.Looper.loop(Looper.java:137)
09-11 22:19:13.649: E/AndroidRuntime(2856):     at android.app.ActivityThread.main(ActivityThread.java:4424)
09-11 22:19:13.649: E/AndroidRuntime(2856):     at java.lang.reflect.Method.invokeNative(Native Method)
09-11 22:19:13.649: E/AndroidRuntime(2856):     at java.lang.reflect.Method.invoke(Method.java:511)
09-11 22:19:13.649: E/AndroidRuntime(2856):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-11 22:19:13.649: E/AndroidRuntime(2856):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-11 22:19:13.649: E/AndroidRuntime(2856):     at dalvik.system.NativeStart.main(Native Method)

这是我的代码:

public class DataExport extends Activity implements OnClickListener {
    private Button back, save, exportAll, clientExport, dateExport;
    private DatabaseHelper db;
    private DatePicker datepicker;
    private ArrayList<ChildRecord> allChildRecords;
    private String childName;
    // In the class declaration section:

    final static private String APP_KEY = "mykey";
    final static private String APP_SECRET = "secret";
    // final static private AccessType ACCESS_TYPE =
    // AccessType.INSERT_APP_ACCESS_TYPE;
    final static private AccessType ACCESS_TYPE = AccessType.APP_FOLDER;

    // In the class declaration section:
    private DropboxAPI<AndroidAuthSession> mDBApi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.data_export);
        initWidgets();
        // And later in some initialization function:
        AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET);
        AndroidAuthSession session = new AndroidAuthSession(appKeys,
                ACCESS_TYPE);
        mDBApi = new DropboxAPI<AndroidAuthSession>(session);

        // MyActivity below should be your activity class name
        mDBApi.getSession().startAuthentication(this);
    }

    protected void onResume() {
        super.onResume();

        if (mDBApi.getSession().authenticationSuccessful()) {
            try {
                // Required to complete auth, sets the access token on the
                // session
                mDBApi.getSession().finishAuthentication();
                AccessTokenPair tokens = mDBApi.getSession()
                        .getAccessTokenPair();
            } catch (IllegalStateException e) {
                Log.i("DbAuthLog", "Error authenticating", e);
            }
        }
    }

    private void initWidgets() {
        allChildRecords = new ArrayList<ChildRecord>();
        db = new DatabaseHelper(this);
        back = (Button) findViewById(R.id.data_export_back);
        back.setOnClickListener(this);
        save = (Button) findViewById(R.id.data_export_save);
        save.setOnClickListener(this);
        exportAll = (Button) findViewById(R.id.data_export_all);
        exportAll.setOnClickListener(this);
        clientExport = (Button) findViewById(R.id.data_export_child_export);
        clientExport.setOnClickListener(this);
        dateExport = (Button) findViewById(R.id.data_export_date_export);
        dateExport.setOnClickListener(this);
        datepicker = (DatePicker) findViewById(R.id.data_export_datepicker);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.data_export_back:
            finish();
            break;
        case R.id.data_export_child_export:
            // export for selected child
            displayChildDialog();
            break;
        case R.id.data_export_date_export:
            // export for selected date
            db.openDB();
            try {
                String dateChoosen = datepicker.getDayOfMonth() + "/"
                        + (datepicker.getMonth() + 1) + "/"
                        + datepicker.getYear();
                exportDateInCSV(
                        "Export-for-date-" + dateChoosen.replace("/", "-"),
                        db.getAssessmentsForDate(dateChoosen), false);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            db.closeDB();

            break;
        case R.id.data_export_all:
            // export all data
            try {
                ArrayList<Assessment> allAssessments = new ArrayList<Assessment>();
                DatabaseHelper helper = new DatabaseHelper(this);
                helper.openDB();
                allAssessments.addAll(helper.getAllAsssessments());

                allAssessments = helper.getAllAsssessments();
                exportDateInCSV("AllDateExport", allAssessments, true);
                helper.closeDB();
            } catch (IOException e) {
                e.printStackTrace();
            }
            break;
        }
    }

    private void displayChildDialog() {
        final AlertDialog builder = new AlertDialog.Builder(this).create();

        builder.setMessage(getString(R.string.insert_password_to_continue));

        final ListView input = new ListView(this);
        input.setId(android.R.id.list);
        builder.setView(input);
        db.openDB();
        allChildRecords = db.getAllChildren();

        db.closeDB();
        SimpleArrayAdapter adapter = new SimpleArrayAdapter(this,
                allChildRecords);
        input.setAdapter(adapter);
        input.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                    int position, long arg3) {
                childName = allChildRecords.get(position).getName();
                db.openDB();
                try {
                    exportDateInCSV(
                            childName,
                            db.getAssessmentsforChild(allChildRecords.get(
                                    position).getChildID()), true);
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                db.closeDB();
                builder.dismiss();
            }
        });

        builder.show();
    }

    public void exportDateInCSV(final String file_name,
            final ArrayList<Assessment> assessments, final boolean dropBox)
            throws IOException {

        File folder = new File(Environment.getExternalStorageDirectory()
                + "/Connell");

        boolean var = false;
        if (!folder.exists())
            var = folder.mkdir();

        System.out.println("" + var);

        final String filename = folder.toString() + "/" + file_name + ".csv";

        // show waiting screen
        CharSequence contentTitle = getString(R.string.app_name);
        final ProgressDialog progDailog = ProgressDialog.show(DataExport.this,
                contentTitle, "Exporting data...", true);// please
                                                            // wait

        new Thread() {
            public void run() {
                try {

                    FileWriter fw = new FileWriter(filename);

                    fw.append("ChildID");
                    fw.append(',');

                    fw.append("Parent1Name");
                    fw.append(',');

                    fw.append("Parent2Name");
                    fw.append(',');

                    fw.append("ChildName");
                    fw.append(',');

                    fw.append("ChildDOB");
                    fw.append(',');

                    fw.append("ChildGender");
                    fw.append(',');

                    fw.append("Child Height");
                    fw.append(',');

                    fw.append("Weight lbs");
                    fw.append(',');

                    fw.append("Ethnicity");
                    fw.append(',');

                    fw.append("ChildType");
                    fw.append(',');

                    fw.append("AssessmentID");
                    fw.append(',');

                    // AssessmentType DoctorName AssessmentDate Q1 Q2
                    fw.append("AssessmentType");
                    fw.append(',');

                    fw.append("DoctorName");
                    fw.append(',');

                    fw.append("AssessmentDate");
                    fw.append(',');

                    for (int n = 0; n < assessments.get(0).getQuestions()
                            .size(); n++) {
                        fw.append("Q" + (n + 1));
                        fw.append(',');
                    }

                    fw.append("RL");
                    fw.append(',');
                    fw.append("ATR");
                    fw.append(',');
                    fw.append("Anxiety");
                    fw.append(',');
                    fw.append("Depression");
                    fw.append(',');
                    fw.append("Anger");
                    fw.append(',');
                    fw.append("PTSD-Instrusion");
                    fw.append(',');
                    fw.append("PTSD-Avoidance");
                    fw.append(',');
                    fw.append("PTSD-Arousal");
                    fw.append(',');
                    fw.append("PTSD-Total");
                    fw.append(',');
                    fw.append("Dissociation");
                    fw.append(',');
                    fw.append("SexualConcerns");
                    fw.append(',');

                    fw.append('\n');

                    //
                    ChildRecord child;

                    for (int i = 0; i < assessments.size(); i++) {

                        db = new DatabaseHelper(DataExport.this);
                        db.openDB();
                        child = db.getChild(assessments.get(i).getChildID());

                        fw.append(assessments.get(i).getChildID() + "");
                        fw.append(",");
                        fw.append(child.getParent1());
                        fw.append(",");
                        fw.append(child.getParent2());
                        fw.append(",");
                        fw.append(child.getName());
                        fw.append(",");
                        fw.append(child.getDob());
                        fw.append(",");
                        switch (child.getGender()) {
                        case 1:
                            fw.append("Male");
                            break;
                        case 2:
                            fw.append("Female");
                            break;
                        }

                        fw.append(",");
                        String[] fullHeight = child.getHeight().split("\"");
                        fw.append(fullHeight[0].replace("\"", "") + "in" + " "
                                + fullHeight[1] + "ft");
                        fw.append(",");
                        fw.append(child.getWeight() + " lbs");
                        fw.append(",");
                        fw.append(getStringFromArray(R.array.ethnicity_array,
                                child.getEthnicity()));
                        fw.append(",");
                        switch (child.getType()) {
                        case 1:
                            fw.append("Biologycal child");
                            break;
                        case 2:
                            fw.append("Foster child");
                            break;
                        case 3:
                            fw.append("Other");
                            break;

                        }
                        fw.append(",");
                        fw.append(assessments.get(i).getId() + "");
                        fw.append(",");
                        fw.append("TSCYC");// edit later//add option for //
                                            // types
                        fw.append(",");

                        fw.append(db.getDoctorNameById(assessments.get(i)
                                .getDoctorID()));
                        fw.append(",");
                        fw.append(assessments.get(i).getDate());
                        fw.append(",");
                        int RL = 0;
                        ArrayList<Question> questions = new ArrayList<Question>();
                        for (int n = 0; n < assessments.get(i).getQuestions()
                                .size(); n++) {

                            questions = assessments.get(i).getQuestions();
                            int answer = questions.get(n).getAnswer();
                            if (answer == 1) {
                                RL++;
                            }
                            fw.append(answer + "");
                            fw.append(",");
                        }
                        fw.append(RL + "");
                        fw.append(",");
                        int ATR = questions.get(8).getAnswer()
                                + questions.get(29).getAnswer()
                                + questions.get(36).getAnswer()
                                + questions.get(39).getAnswer()
                                + questions.get(50).getAnswer()
                                + questions.get(59).getAnswer()
                                + questions.get(63).getAnswer()
                                + questions.get(76).getAnswer()
                                + questions.get(78).getAnswer();
                        fw.append(ATR + "");
                        fw.append(",");
                        // Q7+Q21+Q31+Q32+Q42+Q44+Q57+Q67+Q76
                        int anxiety = questions.get(6).getAnswer()
                                + questions.get(20).getAnswer()
                                + questions.get(30).getAnswer()
                                + questions.get(31).getAnswer()
                                + questions.get(41).getAnswer()
                                + questions.get(43).getAnswer()
                                + questions.get(56).getAnswer()
                                + questions.get(66).getAnswer()
                                + questions.get(75).getAnswer();
                        fw.append(anxiety + "");
                        fw.append(",");
                        // Q2+Q18+Q41+Q54+Q61+Q68+Q71+Q84+Q88
                        int depression = questions.get(1).getAnswer()
                                + questions.get(17).getAnswer()
                                + questions.get(40).getAnswer()
                                + questions.get(53).getAnswer()
                                + questions.get(60).getAnswer()
                                + questions.get(67).getAnswer()
                                + questions.get(70).getAnswer()
                                + questions.get(83).getAnswer()
                                + questions.get(87).getAnswer();
                        fw.append(depression + "");
                        fw.append(",");
                        // Q1+Q15+Q23+Q34+Q43+Q58+Q62+Q87+Q90
                        int anger = questions.get(0).getAnswer()
                                + questions.get(14).getAnswer()
                                + questions.get(22).getAnswer()
                                + questions.get(33).getAnswer()
                                + questions.get(42).getAnswer()
                                + questions.get(57).getAnswer()
                                + questions.get(61).getAnswer()
                                + questions.get(86).getAnswer()
                                + questions.get(89).getAnswer();
                        fw.append(anger + "");
                        fw.append(",");
                        // Q4+Q11+Q19+Q24+Q27+Q36+Q63+Q69+Q80
                        int intrusion = questions.get(3).getAnswer()
                                + questions.get(10).getAnswer()
                                + questions.get(18).getAnswer()
                                + questions.get(23).getAnswer()
                                + questions.get(26).getAnswer()
                                + questions.get(35).getAnswer()
                                + questions.get(62).getAnswer()
                                + questions.get(68).getAnswer()
                                + questions.get(79).getAnswer();
                        fw.append(intrusion + "");
                        fw.append(",");
                        // Q8+Q13+Q29+Q39+Q49+Q55+Q70+Q72+Q81
                        int avoidance = questions.get(7).getAnswer()
                                + questions.get(12).getAnswer()
                                + questions.get(28).getAnswer()
                                + questions.get(38).getAnswer()
                                + questions.get(48).getAnswer()
                                + questions.get(54).getAnswer()
                                + questions.get(69).getAnswer()
                                + questions.get(71).getAnswer()
                                + questions.get(80).getAnswer();
                        fw.append(avoidance + "");
                        fw.append(",");
                        // Q10+Q17+Q26+Q45+Q47+Q48+Q56+Q74+Q82
                        int arousal = questions.get(9).getAnswer()
                                + questions.get(16).getAnswer()
                                + questions.get(25).getAnswer()
                                + questions.get(44).getAnswer()
                                + questions.get(46).getAnswer()
                                + questions.get(55).getAnswer()
                                + questions.get(73).getAnswer()
                                + questions.get(81).getAnswer();
                        fw.append(arousal + "");
                        fw.append(",");

                        int total = intrusion - avoidance + arousal;
                        fw.append(total + "");
                        fw.append(",");
                        // Q5+Q25+Q28+Q33+Q38+Q46+Q52+Q78+Q85
                        int dissociation = questions.get(4).getAnswer()
                                + questions.get(24).getAnswer()
                                + questions.get(27).getAnswer()
                                + questions.get(32).getAnswer()
                                + questions.get(37).getAnswer()
                                + questions.get(45).getAnswer()
                                + questions.get(51).getAnswer()
                                + questions.get(77).getAnswer()
                                + questions.get(84).getAnswer();
                        fw.append(dissociation + "");
                        fw.append(",");

                        // Q6+Q12+Q16+Q20+Q35+Q50+Q59+Q65+Q75
                        int sexualConcerns = questions.get(5).getAnswer()
                                + questions.get(11).getAnswer()
                                + questions.get(15).getAnswer()
                                + questions.get(19).getAnswer()
                                + questions.get(34).getAnswer()
                                + questions.get(49).getAnswer()
                                + questions.get(58).getAnswer()
                                + questions.get(64).getAnswer()
                                + questions.get(74).getAnswer();
                        fw.append(sexualConcerns + "");
                        fw.append(",");
                        fw.append("\n");
                        db.closeDB();
                    }
                    fw.close();
                    if (dropBox) {
                        File file = new File("working-draft.txt");
                        FileInputStream inputStream = new FileInputStream(file);
                        Entry response = mDBApi.putFile(file_name + ".csv",
                                inputStream, file.length(), null, null);
                        Log.v("--", response.toString() + "   #!@!");
                        // Log.i("DbExampleLog", "The uploaded file's rev is: "
                        // + newEntry.rev);

                    }
                } catch (Exception e) {
                }
                progDailog.dismiss();
            }
        }.start();

    }

    private String getStringFromArray(int id, int index) {
        String[] bases = getResources().getStringArray(id);
        return bases[index];

    }
}

很抱歉长代码,但我认为这是描述问题的最佳方式。所以最后任何人都可以帮我解决这个问题。感谢

3 个答案:

答案 0 :(得分:4)

看起来你有依赖问题。您的错误中有几行代表它无法找到类:

Could not find class 'org.json.simple.JSONArray', referenced from method com.dropbox.client2.DropboxAPI.revisions
Could not find class 'org.json.simple.JSONArray', referenced from method com.dropbox.client2.DropboxAPI.search
Could not find class 'org.json.simple.parser.JSONParser', referenced from method com.dropbox.client2.RESTUtility.parseAsJSON

您是否将包含这些类的JAR作为项目的一部分?如果是这样,它们是否在你的构建路径上?这可以解释你所看到的。

答案 1 :(得分:2)

可能你只是包含了&#34; dropbox-android-sdk-1.6.3.jar&#34;在你的libs目录

enter image description here

包含显示此图表的所有.jar文件

enter image description here

enter image description here

不要包含commons-logging-1.1.1.jar,因为你会遇到问题。

希望得到这个帮助。

答案 2 :(得分:0)

最终的解决方案应该是: 1.打开两个jar文件; dropbox-sync-sdk-android.jar和dropbox-android-sdk-1.6.1.jar 2.删除前sdk中的client2文件夹,并将其替换为后者的client2 3.然后下载json_simple-1.1.jar的副本。 4.将json_simple-1.1.jar和dropbox-sync-sdk-android.jar复制到libs文件夹中,并将这两个包含在项目属性中;这有助于避免将来出现错误

注意:你不需要在任何地方都包含dropbox-android-sdk-1.6.1.jar。