1

Specifically I am looking to upload a file with extension .xls

I have a project that tries to do this, but the following error exists

 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) 

My code is:

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;
    }

AndroidManifest..

 <?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>

and dependences

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'

get and upload to Dropbox the xls file that is in the "Backup" path with a single button and not from the file manager. Thank you all

Innuendo
  • 11
  • 4
  • I'm not sure what's causing that specific `NoClassDefFoundError`, but based on your code it looks like you're attempting to use the old Dropbox SDK for the now retired Dropbox API v1. To use Dropbox API v2 on Android, you should instead use the Dropbox Java SDK found here: https://github.com/dropbox/dropbox-sdk-java (There's an Android example here: https://github.com/dropbox/dropbox-sdk-java/tree/master/examples/android ) – Greg Aug 01 '19 at 14:50
  • Hi Greg, yes but I have no idea how to go from v1 to v2, specifically for my requirement ): – Innuendo Aug 01 '19 at 20:58
  • You can use Maven or Gradle to install the Dropbox API v2 Java SDK, documented here: https://github.com/dropbox/dropbox-sdk-java#setup And the upload methods are documented here: https://dropbox.github.io/dropbox-sdk-java/api-docs/v3.1.x/com/dropbox/core/v2/files/DbxUserFilesRequests.html#upload-java.lang.String- – Greg Aug 02 '19 at 13:43

0 Answers0