-1
我需要將文件從Android應用上傳到服務器。我使用Volley
和創建的類,這有助於與multipart
幫助發送數據:使用Apache發送Android Volley文件:NoClassDefFoundError:ContentType失敗解決方法
public class MultipartRequest extends Request<String> {
MultipartEntityBuilder entity = MultipartEntityBuilder.create();
HttpEntity httpentity;
private static final String FILE_PART_NAME = "image ";
private final Response.Listener<String> mListener;
private final File mFilePart;
private final Map<String, String> mStringPart;
public MultipartRequest(String url, Response.ErrorListener errorListener,
Response.Listener<String> listener, File file,
Map<String, String> mStringPart) {
super(Method.POST, url, errorListener);
mListener = listener;
mFilePart = file;
this.mStringPart = mStringPart;
entity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
buildMultipartEntity();
}
public void addStringBody(String param, String value) {
mStringPart.put(param, value);
}
private void buildMultipartEntity() {
entity.addPart(FILE_PART_NAME, new FileBody(mFilePart));
for (Map.Entry<String, String> entry : mStringPart.entrySet()) {
entity.addTextBody(entry.getKey(), entry.getValue());
}
}
@Override
public String getBodyContentType() {
return httpentity.getContentType().getValue();
}
@Override
public byte[] getBody() throws AuthFailureError {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
httpentity = (HttpEntity) entity.build();
httpentity.writeTo(bos);
} catch (IOException e) {
VolleyLog.e("IOException writing to ByteArrayOutputStream");
}
return bos.toByteArray();
}
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
return Response.success("Uploaded", getCacheEntry());
}
@Override
protected void deliverResponse(String response) {
mListener.onResponse(response);
}
}
我使用Gradle
,所以我的依賴關係:
compile 'org.apache.httpcomponents:httpmime:4.5.1'
試圖實現libs文件夾中的庫,但在項目構建期間遇到問題。 當我做出這樣的要求,我收到:
java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/entity/ContentType;
Caused by: java.lang.ClassNotFoundException: Didn't find class "org.apache.http.entity.ContentType" on path: DexPathList[[zip file "/data/app/android.demigos.comouts-1/base.apk"],
nativeLibraryDirectories=[/system/lib, /vendor/lib, system/vendor/lib, system/vendor/lib/egl, system/lib/hw]]
我的搖籃文件:
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
useLibrary 'org.apache.http.legacy'
defaultConfig {
applicationId "android.demigos.comouts"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
......
compile "org.apache.httpcomponents:httpcore:4.2.4"
}
已經做到了。但後來我無法導入MultipartEntityBuilder –
確定嘗試添加此依賴項:編譯「org.apache.httpcomponents:httpcore:4.2.4」 –
並將您的build.gradle文件添加到您的問題 –