android - 将照片上传到服务器android时出错

标签 android http

我的错误:

java.lang.NoSuchFieldError: org.apache.http.message.BasicHeaderValueFormatter.INSTANCE

我的代码:

public static String POST(String path, JSONObject params, String fileName)
            throws ClientProtocolException, IOException {
        HttpPost request = new HttpPost(url + path);

        MultipartEntity multipartEntity = new MultipartEntity();

        FileBody bin = new FileBody(new File(fileName), "image/jpeg");
        multipartEntity.addPart("photo", bin);
        for (Object obj : params.entrySet()) {
            Entry e = (Entry)obj;
            multipartEntity.addPart((String)e.getKey(), new StringBody((String)e.getValue()));
        }
        request.setEntity(multipartEntity);
        HttpResponse response = httpClient.execute(request);

        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        String result = "";
        String line = "";
        while ((line = bufferedReader.readLine()) != null)
            result += line;
        bufferedReader.close();
        return result;
    }

我的毕业典礼:

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile 'com.andreabaccega:android-form-edittext:1.2.1@aar'
    compile files('libs/gson-2.2.1.jar')
    compile files('libs/json-simple-1.1.1.jar')
    compile files('libs/httpmime-4.5.1.jar')
    compile files('libs/httpcore-4.4.3.jar')
    compile files('libs/httpclient-4.5.1.jar')
    compile files('libs/commons-lang3-3.1.jar')
    compile files('libs/commons-logging-1.1.2.jar')
}

我想使用 HTTP POST 将文件(特别是图像)上传到服务器。 如何解决这个问题呢?

最佳答案

我认为您需要使用 android port version of http client from apache
尝试将您的 gradle 依赖项更改为此:

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile 'com.andreabaccega:android-form-edittext:1.2.1@aar'
    compile files('libs/gson-2.2.1.jar')
    compile files('libs/json-simple-1.1.1.jar')
    compile('org.apache.httpcomponents:httpmime:4.5.1') {
        exclude module: 'httpclient'
    }
    compile 'org.apache.httpcomponents:httpclient-android:4.3.5.1'
    compile files('libs/httpcore-4.4.3.jar')
    compile files('libs/commons-lang3-3.1.jar')
    compile files('libs/commons-logging-1.1.2.jar')
}

关于android - 将照片上传到服务器android时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33511890/

相关文章:

ruby-on-rails - Rails 高级注册实现如何?

security - 检查 Mule http :listener 中的安全 header

安卓.view.InflateException : Binary XML file line #6: Error inflating class <unknown> while initializing progress bar

android - 如何在android中使用mpg123

android - Monkeyrunner 脚本不运行,但命令有效

http - 那么为什么我们应该使用 POST 而不是 GET 来发布数据呢?

javascript - Node HTTP 解析

javascript - 对服务发出 http 请求,在 Controller 中获取响应

android - 如何将 Kotlin 错误链接到 Android Studio 中的源代码?

android - 如何在android中的Gridview中实现图片库?