Java 如何在android studio中使用api v2 dropbox上传文件?

Java 如何在android studio中使用api v2 dropbox上传文件?,java,android-studio,dropbox-api,file-manager,dropbox-sdk,Java,Android Studio,Dropbox Api,File Manager,Dropbox Sdk,特别是我想上传一个扩展名为.xls的文件 我有一个项目试图这样做,但存在以下错误 E/AndroidRuntime: FATAL EXCEPTION: Thread-6 Process: com.example.excelpromodel, PID: 12651 java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/client/methods/HttpPut; at com

特别是我想上传一个扩展名为.xls的文件

我有一个项目试图这样做,但存在以下错误

 E/AndroidRuntime: FATAL EXCEPTION: Thread-6
    Process: com.example.excelpromodel, PID: 12651
    java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/client/methods/HttpPut;
        at com.dropbox.client2.DropboxAPI.putFileRequest(DropboxAPI.java:2390)
        at com.dropbox.client2.DropboxAPI.putFileOverwriteRequest(DropboxAPI.java:1760)
        at com.dropbox.client2.DropboxAPI.putFileOverwrite(DropboxAPI.java:1726)
        at com.example.excelpromodel.SQLite2ExcelActivity$4.run(SQLite2ExcelActivity.java:267)
        at java.lang.Thread.run(Thread.java:764)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "org.apache.http.client.methods.HttpPut" on path: DexPathList[[zip file "/data/app/com.example.excelpromodel-pu6SR7F9dUiyu9bWnHwX8w==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.excelpromodel-pu6SR7F9dUiyu9bWnHwX8w==/lib/arm, /system/lib, /system/vendor/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at com.dropbox.client2.DropboxAPI.putFileRequest(DropboxAPI.java:2390) 
        at com.dropbox.client2.DropboxAPI.putFileOverwriteRequest(DropboxAPI.java:1760) 
        at com.dropbox.client2.DropboxAPI.putFileOverwrite(DropboxAPI.java:1726) 
        at com.example.excelpromodel.SQLite2ExcelActivity$4.run(SQLite2ExcelActivity.java:267) 
        at java.lang.Thread.run(Thread.java:764) 
我的代码是:

public class SQLite2ExcelActivity extends AppCompatActivity {
public static Button button1;
public static String path = Environment.getExternalStorageDirectory().getPath() + "/Backup/";
public static File Dir = new File(path);

public void onClickBut1(View v) {
    UploadToDropboxFromPath(path + "test.txt", "excel/test.txt");
}
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sqlite_2_xl);
        button1 = findViewById(R.id.button1);
        initViews();
        AndroidAuthSession session = buildSession();
        dropboxAPI = new DropboxAPI<AndroidAuthSession>(session);
}
static DropboxAPI<AndroidAuthSession> dropboxAPI;
    private static final String APP_KEY = "";
    private static final String APP_SECRET = "";
    private static final String ACCESSTOKEN = "";
    private DropboxAPI.UploadRequest request;
    private AndroidAuthSession buildSession()
    {
        AppKeyPair appKeyPair = new AppKeyPair(APP_KEY, APP_SECRET);
        AndroidAuthSession session = new AndroidAuthSession(appKeyPair);
        session.setOAuth2AccessToken(ACCESSTOKEN);
        return session;
    }
    static final int UploadFromSelectApp = 9501;
    static final int UploadFromFilemanager = 9502;
    public static String DropboxUploadPathFrom = "";
    public static String DropboxUploadName = "";
    public static String DropboxDownloadPathFrom = "";
    public static String DropboxDownloadPathTo = "";

    private void UploadToDropboxFromPath (String uploadPathFrom, String uploadPathTo)
    {
        Toast.makeText(getApplicationContext(), "Upload file ...", Toast.LENGTH_SHORT).show();
        final String uploadPathF = uploadPathFrom;
        final String uploadPathT = uploadPathTo;
        Thread th = new Thread(new Runnable()
        {
            public void run()
            {
                File tmpFile = null;
                try
                {
                    tmpFile = new File(uploadPathF);
                }
                catch (Exception e) {e.printStackTrace();}
                FileInputStream fis = null;
                try
                {
                    fis = new FileInputStream(tmpFile);
                }
                catch (FileNotFoundException e) {e.printStackTrace();}
                try
                {
                    dropboxAPI.putFileOverwrite(uploadPathT, fis, tmpFile.length(), null);
                }
                catch (Exception e) {}
                getMain().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(), "File successfully uploaded.", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
        th.start();
    }



@Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent)
    {
        if (requestCode == UploadFromFilemanager)
        {
            final Uri currFileURI = intent.getData();
            final String pathFrom = currFileURI.getPath();
            Toast.makeText(getApplicationContext(), "Upload file ...", Toast.LENGTH_SHORT).show();
            Thread th = new Thread(new Runnable()
            {
                public void run()
                {
                    getMain().runOnUiThread(new Runnable()
                    {
                        @Override
                        public void run()
                        {
                            UploadToDropboxFromPath(pathFrom, "/db-test/" + DropboxUploadName + pathFrom.substring(pathFrom.lastIndexOf('.')));
                            Toast.makeText(getApplicationContext(), "File successfully uploaded.", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            });
            th.start();
        }...
}


public String getPath(Context context, Uri contentUri) {
        Cursor cursor = null;
        try {
            String[] proj = { MediaStore.Images.Media.DATA, MediaStore.Video.Media.DATA, MediaStore.Audio.Media.DATA };
            cursor = context.getContentResolver().query(contentUri,  proj, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            String s = cursor.getString(column_index);
            if(s!=null) {
                cursor.close();
                return s;
            }
        }
        catch(Exception e){}
        try {
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
            cursor.moveToFirst();
            String s = cursor.getString(column_index);
            if(s!=null) {
                cursor.close();
                return s;
            }
        }
        catch(Exception e){}
        try {
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
            cursor.moveToFirst();
            String s = cursor.getString(column_index);
            cursor.close();
            return s;
        }
        finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }

    public SQLite2ExcelActivity getMain()
    {
        return this;
    }

只需一个按钮,而不是从文件管理器获取并上传到Dropbox“备份”路径中的xls文件。谢谢大家

我不确定是什么原因导致了特定的
NoClassDefFoundError
,但根据您的代码,您似乎正在尝试为现已退役的Dropbox API v1使用旧的Dropbox SDK。要在Android上使用Dropbox API v2,您应该使用以下Dropbox Java SDK:(这里有一个Android示例:)Hi Greg,是的,但我不知道如何从v1到v2,特别是针对我的要求):您可以使用Maven或Gradle安装Dropbox API v2 Java SDK,这里有文档记录:上传方法在这里有文档记录:我不确定是什么导致了特定的
NoClassDefFoundError
,但根据您的代码,您似乎正在尝试将旧的Dropbox SDK用于现已退役的Dropbox API v1。要在Android上使用Dropbox API v2,您应该使用以下Dropbox Java SDK:(这里有一个Android示例:)Hi Greg,是的,但我不知道如何从v1到v2,特别是为了满足我的要求):您可以使用Maven或Gradle安装Dropbox API v2 Java SDK,记录在这里:上传方法记录在这里:
 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.excelpromodel">

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.dropbox.client2.android.AuthActivity"
            android:configChanges="orientation|keyboard"
            android:launchMode="singleTask" >
            <intent-filter>

                <data android:scheme="db-akg125xzg45a9gc" />

                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.BROWSABLE"/>
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SQLite2ExcelActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" />
        <activity
            android:name=".Excel2SQLiteActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" />
    </application>

</manifest>
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.0.0'
implementation 'com.ajts.androidmads.SQLite2Excel:library:1.0.4'
implementation 'com.ajts.androidmads.sqliteimpex:library:1.0.0'
implementation files('libs/dropbox-android-sdk-1.6.3.jar')
implementation files('libs/json_simple-1.1.jar')
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'