我正在尝试将Cloudinary与我的android应用程序一起使用,我构建了一个测试类来尝试上传一个文件,但是出现以下错误:

java.lang.ClassNotFoundException:org.apache.http.client.methods.HttpRequestBase

这是我的课:

import com.cloudinary.Cloudinary;
import com.cloudinary.Transformation;
import com.cloudinary.utils.ObjectUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by 16110066 on 21/07/2017.
 */

public class cloudUtil {
    public static void main(String args[]) throws IOException {
        try {
            configCloud();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void configCloud() throws IOException {
        Map config = new HashMap<>();
        config.put("cloud_name","euqueropucrs");
        config.put("api_key","219569379885948");
        config.put("api_secret","My secret was here");
        Cloudinary cloudinary = new Cloudinary(config);
        Map imageResult = cloudinary.uploader().upload("C:\\git\\Trocas\\app\\src\\main\\res\\drawable\\ic_menu_gallery.xml",ObjectUtils.emptyMap());
        System.out.println(imageResult.toString());

    }
}


这是我的困境:

apply plugin: 'com.android.application'


android {
    signingConfigs {
        config {
            keyAlias '15109336'
            keyPassword '15109336'
            storeFile file('/home/ricardo/.android/debug.keystore')
            storePassword '15109336'
        }
    }
    compileSdkVersion 26
    buildToolsVersion "26.0.0"
    defaultConfig {
        applicationId "com.example.cassiotrindade.trocas"
        minSdkVersion 21
        multiDexEnabled true
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support:support-v4:26.+'
    compile 'com.android.support:design:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.support:recyclerview-v7:26.+'
    compile 'com.google.code.findbugs:jsr305:2.0.1'
    compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.3'
    compile 'com.google.android.gms:play-services:11.0.2'
    compile 'com.google.android.gms:play-services-auth:11.0.2'
    compile group: 'com.google.code.gson', name: 'gson', version: '2.8.1'
    compile 'com.squareup.okhttp3:okhttp:3.8.1'
    compile (group: 'com.cloudinary', name: 'cloudinary-http44', version: '1.14.0')
    compile group: 'com.cloudinary', name: 'cloudinary-core', version: '1.13.0'
    //Imagepicker
    compile 'com.github.bumptech.glide:glide:3.7.0'
    testCompile 'junit:junit:4.12'
}
apply plugin: 'com.google.gms.google-services'


这是完整的例外:

    Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/client/methods/HttpRequestBase
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:264)
        at com.cloudinary.strategies.StrategyLoader.load(StrategyLoader.java:11)
        at com.cloudinary.strategies.StrategyLoader.find(StrategyLoader.java:20)
        at com.cloudinary.Cloudinary.loadStrategies(Cloudinary.java:78)
        at com.cloudinary.Cloudinary.<init>(Cloudinary.java:92)
        at com.example.cassiotrindade.trocas.utility.cloudUtil.configCloud(cloudUtil.java:29)
        at com.example.cassiotrindade.trocas.utility.cloudUtil.main(cloudUtil.java:18)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
    Caused by: java.lang.ClassNotFoundException: org.apache.http.client.methods.HttpRequestBase
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 13 more


编辑:

我在gradle中又添加了一个编译:

      compile group: 'org.apache.httpcomponents' , name: 'httpclient-android' , version: '4.3.5.1'


现在我得到这个错误:

Exception in thread "main" java.lang.UnknownError: Can't find Cloudinary platform adapter [com.cloudinary.android.UploaderStrategy,com.cloudinary.http42.UploaderStrategy,com.cloudinary.http43.UploaderStrategy,com.cloudinary.http44.UploaderStrategy]
    at com.cloudinary.Cloudinary.loadStrategies(Cloudinary.java:81)
    at com.cloudinary.Cloudinary.<init>(Cloudinary.java:92)
    at com.example.cassiotrindade.trocas.utility.cloudUtil.configCloud(cloudUtil.java:29)
    at com.example.cassiotrindade.trocas.utility.cloudUtil.main(cloudUtil.java:18)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

Process finished with exit code 1


找到了解决方案:

我输入了错误的cloudinary:

    compile group: 'com.cloudinary', name: 'cloudinary-android', version: '1.13.0'


这很好。

最佳答案

找到了解决方案:

我输入了错误的cloudinary:

compile group: 'com.cloudinary', name: 'cloudinary-android', version: '1.13.0'

关于java - 在Android上使用Cloudinary的Apache异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45287117/

10-11 20:46