1

I did the same thing on what my professor taught us, but when i try to do it in my own computer (which i did in our school computers) I'm having a error with AsyncTask that is related with java.lang.NoClassDefFoundError, But I'm a little bit beginner in android development using java, Cause I'm using react-native in mobile development.

I have not tried anything else because I'm a little bit noob in java and android studio. But I've found out that my system and in school have some different specs or version of software.

Softare/Tool   |  My_SYSTEM | School_SYSTEM
Android Studio | 3.0+       | 2.1
Java           |  8         | 8
Javac          |  8         | 11

Is there a problem with my software or tools that causes me the error

This is the doInBackground Method

@Override
        protected String doInBackground(String... args0) {

            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("lastname", lname));

            JSONObject json = jsonParser.makeHttpRequest(url_sample1, "POST", params);
            Log.d("Create Response", json.toString());

            try {
                int success = json.getInt(TAG_SUCCESS);
                if (success == 1) {
                    //success
                    String success_res = "Success";
                    Log.d("Success Response", success_res);

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(MainActivity.this, "Successfully Saved!",
                                    Toast.LENGTH_LONG).show();
                        }
                    });

                } else {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(MainActivity.this, "Error",
                                    Toast.LENGTH_LONG).show();
                        }
                    });
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

This is my PHP file. I have double check it on my browser still no error for it

<?php
include("connection.php");

$response = array();

if(isset($_POST['lastname'])){
    $last = $_POST['lastname'];
    $result = mysqli_query($con, "INSERT INTO tblsample1('lname') VALUES ('$last')");

    if($result){
        $response["success"] = 1;
        $response["message"] = "successful";
        echo json_encode($response);
    }else{
        $response["success"] = 0;
        $response["message"] = "error";
        echo json_encode($response);
    }
}else{
    $response["success"] = 0;
        $response["message"] = "Please input lastname";
        echo json_encode($response);
}


?>

I also included the permission tag on AndroidManifest so I could use internet on my android app.

And this is my logcat whenever I press the submit button

2019-05-27 15:06:25.887 13296-13330/com.example.prjsample1 E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
    Process: com.example.prjsample1, PID: 13296
    java.lang.RuntimeException: An error occurred while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:354)
        at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
        at java.util.concurrent.FutureTask.run(FutureTask.java:271)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:764)
     Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/commons/logging/LogFactory;
        at org.apache.http.impl.client.AbstractHttpClient.<init>(AbstractHttpClient.java:187)
        at org.apache.http.impl.client.DefaultHttpClient.<init>(DefaultHttpClient.java:146)
        at com.example.prjsample1.JSONParser.makeHttpRequest(JSONParser.java:42)
        at com.example.prjsample1.MainActivity$sample1.doInBackground(MainActivity.java:69)
        at com.example.prjsample1.MainActivity$sample1.doInBackground(MainActivity.java:51)
        at android.os.AsyncTask$2.call(AsyncTask.java:333)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
        at java.lang.Thread.run(Thread.java:764) 
     Caused by: java.lang.ClassNotFoundException: Didn't find class "org.apache.commons.logging.LogFactory" on path: DexPathList[[zip file "/data/app/com.example.prjsample1-87DfVwPm8J-fG1nxPkKpFQ==/base.apk", zip file "/data/app/com.example.prjsample1-87DfVwPm8J-fG1nxPkKpFQ==/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.prjsample1-87DfVwPm8J-fG1nxPkKpFQ==/split_lib_resources_apk.apk", zip file "/data/app/com.example.prjsample1-87DfVwPm8J-fG1nxPkKpFQ==/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.prjsample1-87DfVwPm8J-fG1nxPkKpFQ==/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.prjsample1-87DfVwPm8J-fG1nxPkKpFQ==/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.prjsample1-87DfVwPm8J-fG1nxPkKpFQ==/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.prjsample1-87DfVwPm8J-fG1nxPkKpFQ==/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.prjsample1-87DfVwPm8J-fG1nxPkKpFQ==/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.prjsample1-87DfVwPm8J-fG1nxPkKpFQ==/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.prjsample1-87DfVwPm8J-fG1nxPkKpFQ==/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.prjsample1-87DfVwPm8J-fG1nxPkKpFQ==/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.prjsample1-87DfVwPm8J-fG1nxPkKpFQ==/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.prjsample1-87DfVwPm8J-fG1nxPkKpFQ==/lib/x86, /system/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 org.apache.http.impl.client.AbstractHttpClient.<init>(AbstractHttpClient.java:187) 
        at org.apache.http.impl.client.DefaultHttpClient.<init>(DefaultHttpClient.java:146) 
        at com.example.prjsample1.JSONParser.makeHttpRequest(JSONParser.java:42) 
        at com.example.prjsample1.MainActivity$sample1.doInBackground(MainActivity.java:69) 
        at com.example.prjsample1.MainActivity$sample1.doInBackground(MainActivity.java:51) 
        at android.os.AsyncTask$2.call(AsyncTask.java:333) 
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) 
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
        at java.lang.Thread.run(Thread.java:764) 
        Suppressed: java.io.IOException: No original dex files found for dex location /data/app/com.example.prjsample1-87DfVwPm8J-fG1nxPkKpFQ==/split_lib_resources_apk.apk
        at dalvik.system.DexFile.openDexFileNative(Native Method)
        at dalvik.system.DexFile.openDexFile(DexFile.java:354)
        at dalvik.system.DexFile.<init>(DexFile.java:101)
        at dalvik.system.DexFile.<init>(DexFile.java:75)
        at dalvik.system.DexPathList.loadDexFile(DexPathList.java:394)
        at dalvik.system.DexPathList.makeDexElements(DexPathList.java:354)
        at dalvik.system.DexPathList.<init>(DexPathList.java:164)
        at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java:74)
        at dalvik.system.BaseDexClassLoader.<init>(BaseDexClassLoader.java:65)
        at dalvik.system.PathClassLoader.<init>(PathClassLoader.java:64)
        at com.android.internal.os.ClassLoaderFactory.createClassLoader(ClassLoaderFactory.java:73)
        at com.android.internal.os.ClassLoaderFactory.createClassLoader(ClassLoaderFactory.java:88)
2019-05-27 15:06:25.890 13296-13330/com.example.prjsample1 E/AndroidRuntime:     at android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:74)
        at android.app.ApplicationLoaders.getClassLoader(ApplicationLoaders.java:40)
        at android.app.LoadedApk.createOrUpdateClassLoaderLocked(LoadedApk.java:727)
        at android.app.LoadedApk.getClassLoader(LoadedApk.java:810)
        at android.app.LoadedApk.getResources(LoadedApk.java:1032)
        at android.app.ContextImpl.createAppContext(ContextImpl.java:2345)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5749)
        at android.app.ActivityThread.access$1100(ActivityThread.java:199)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

I should have a toastmaketest when I press the submit button which it actually did when we do it on the schoool computer

Zoe
  • 27,060
  • 21
  • 118
  • 148
Peter Festejo
  • 97
  • 2
  • 2
  • 9
  • Your project seems to be missing a library (*Didn't find class "org.apache.commons.logging.LogFactory" on path: (...)*). Maybe the project on the school computer has it but it isn't on your current machine or the paths are not the same. – deHaar May 27 '19 at 07:38
  • @deHaar idk why, but i copy all the files that our professor give to us. – Peter Festejo May 27 '19 at 08:21
  • @deHaar and load it in our libs folder – Peter Festejo May 27 '19 at 08:21
  • Then you will have to get familiar with the organisation of java and android projects. What files has your professor provided? How do you *copy* them into the android studio (project)? Where exactly do you find something about this library in the files provided by your professor? – deHaar May 27 '19 at 08:37

1 Answers1

0

The crash says Didn't find class "org.apache.commons.logging.LogFactory" on path.

It means, there is a missing dependency of slf4j. Add slf4j to the module level build.gradle file.

// https://mvnrepository.com/artifact/org.slf4j/slf4j-android
implementation group: 'org.slf4j', name: 'slf4j-android', version: '1.7.26'

How to add the dependency to the build.gradle file.

  1. Open the project tools window (Shortcut: Windows: Alt + 1 or Mac: Command + 1). [You'll see it in the top left corner of the Android Studio.]

  2. Expand the app folder to find the build.gradle file. [There will be multiple build.gradle files. Make sure the file is inside the app folder alongside src, build folder and not outside it.]

  3. Open the build.gradle file and scroll down to find the dependencies block. [Looks something like below.]

    dependencies {
        implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlinVersion"
        implementation "com.android.support:appcompat-v7:$supportVersion"
        implementation "com.android.support.constraint:constraint-layout:$constraintVersion"
    ...
        implementation group: 'org.slf4j', name: 'slf4j-android', version: '1.7.26'
    }
    
  4. Add the dependency to the block (like above).

Srikar Reddy
  • 3,650
  • 4
  • 36
  • 58
  • Sorry for my noobness, I dont know how to add this depency. Can you teach me how? Should I copy paste this in the file? – Peter Festejo May 27 '19 at 08:26
  • Yes, you have to copy that line to the said folder. Check the updated answer and let me know if you're still having issues adding the line. – Srikar Reddy May 27 '19 at 09:36