gpt4 book ai didi

android - Java.lang.NoClassDefFoundError : Failed resolution of: Lorg/apache/http/entity/ContentType;

转载 作者:行者123 更新时间:2023-12-01 21:59:52 25 4
gpt4 key购买 nike

您好,我使用 android studio,想要将图像上传到 RoR 服务器。但是,当我按下上传按钮时,我收到此错误:

FATAL EXCEPTION: AsyncTask #1
Process: com.example.myapplicationcamera, PID: 11616
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/entity/ContentType;
at org.apache.http.entity.mime.content.FileBody.<init>(FileBody.java:89)
at com.example.myapplicationcamera.UploadActivity$UploadFileToServer.uploadFile(UploadActivity.java:176)
at com.example.myapplicationcamera.UploadActivity$UploadFileToServer.doInBackground(UploadActivity.java:153)
at com.example.myapplicationcamera.UploadActivity$UploadFileToServer.doInBackground(UploadActivity.java:131)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.ClassNotFoundException: Didn't find class "org.apache.http.entity.ContentType" on path: DexPathList[[dex file "/data/data/com.example.myapplicationcamera/files/instant-run/dex/slice-support-annotations-23.4.0_eb978a40b695cbacc3760b03a82ff4d263be66cd-classes.dex", dex file "/data/data/com.example.myapplicationcamera/files/instant-run/dex/slice-slice_9-classes.dex", dex file "/data/data/com.example.myapplicationcamera/files/instant-run/dex/slice-slice_8-classes.dex", dex file "/data/data/com.example.myapplicationcamera/files/instant-run/dex/slice-slice_7-classes.dex", dex file "/data/data/com.example.myapplicationcamera/files/instant-run/dex/slice-slice_6-classes.dex", dex file "/data/data/com.example.myapplicationcamera/files/instant-run/dex/slice-slice_5-classes.dex", dex file "/data/data/com.example.myapplicationcamera/files/instant-run/dex/slice-slice_4-classes.dex", dex file "/data/data/com.example.myapplicationcamera/files/instant-run/dex/slice-slice_3-classes.dex", dex file "/data/data/com.example.myapplicationcamera/files/instant-run/dex/slice-slice_2-classes.dex", dex file "/data/data/com.example.myapplicationcamera/files/instant-run/dex/slice-slice_1-classes.dex", dex file "/data/data/com.example.myapplicationcamera/files/instant-run/dex/slice-slice_0-classes.dex", dex file "/data/data/com.example.myapplicationcamera/files/instant-run/dex/slice-internal_impl-23.4.0_b87f1f72ec1c9dcbc144d1d0504d45070305661a-classes.dex", dex file "/data/data/com.example.myapplicationcamera/files/instant-run/dex/slice-httpmime-4.3.5_e1f92e9669775764ad0bd3fef73fa0c37469b115-classes.dex", dex file "/data/data/com.example.myapplicationcamera/files/instant-run/dex/slice-com.android.support-support-vector-drawable-23.4.0_6c8fdbed5ab41e288837ace225df060c2da9979a-classes.dex", dex file "/data/data/com.example.myapplicationcamera/files/instant-run/dex/slice-com.android.support-support-v4-23.4.0_31bc06bad02045a071c034877ff069663b43a147-classes.dex", dex file "/data/data/com.example.myapplicationcamera/files/instant-run/dex/slice-com.android.support-appcompat-v7-23.4.0_85f3ceda35b43aaa8f8d9ba7878a3e38fde53a74-classes.dex", dex file "/data/data/com.example.myapplicationcamera/files/instant-run/dex/slice-com.android.support-animated-vector-drawable-23.4.0_03c3e23d10520277fe339d24e7aea8b4848d806b-classes.dex"],nativeLibraryDirectories=[/data/app/com.example.myapplicationcamera-1/lib/arm64, /vendor/lib64, /system/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at com.android.tools.fd.runtime.IncrementalClassLoader$DelegateClassLoader.findClass(IncrementalClassLoader.java:90)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.

这是我的 Activity 代码

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import org.apache.http.entity.ContentType;

import java.io.File;
import java.io.IOException;

import com.example.myapplicationcamera.AndroidMultiPartEntity.ProgressListener;

public class UploadActivity extends Activity {
// LogCat tag
private static final String TAG = MainActivity.class.getSimpleName();

private ProgressBar progressBar;
private String filePath = null;
private TextView txtPercentage;
private ImageView imgPreview;
private VideoView vidPreview;
private Button btnUpload;
long totalSize = 0;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload);
txtPercentage = (TextView) findViewById(R.id.txtPercentage);
btnUpload = (Button) findViewById(R.id.btnUpload);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
imgPreview = (ImageView) findViewById(R.id.imgPreview);
vidPreview = (VideoView) findViewById(R.id.videoPreview);

// Changing action bar background color
// getActionBar().setBackgroundDrawable(
// new ColorDrawable(Color.parseColor(getResources().getString(
// R.string.ColorRed))));

// Receiving the data from previous activity
Intent i = getIntent();

// image or video path that is captured in previous activity
filePath = i.getStringExtra("filePath");

// boolean flag to identify the media type, image or video
boolean isImage = i.getBooleanExtra("isImage", true);

if (filePath != null) {
// Displaying the image or video on the screen
previewMedia(isImage);
} else {
Toast.makeText(getApplicationContext(),
"Sorry, file path is missing!", Toast.LENGTH_LONG).show();
}

btnUpload.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// uploading the file to server
new UploadFileToServer().execute();
}
});

}

/**
* Displaying captured image/video on the screen
* */
private void previewMedia(boolean isImage) {
// Checking whether captured media is image or video
if (isImage) {
imgPreview.setVisibility(View.VISIBLE);
vidPreview.setVisibility(View.GONE);
// bimatp factory
BitmapFactory.Options options = new BitmapFactory.Options();

// down sizing image as it throws OutOfMemory Exception for larger
// images
options.inSampleSize = 8;


final Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);

imgPreview.setImageBitmap(bitmap);
} else {
imgPreview.setVisibility(View.GONE);
vidPreview.setVisibility(View.VISIBLE);
vidPreview.setVideoPath(filePath);
// start playing
vidPreview.start();
}
}

/**
* Uploading the file to server
* */
private class UploadFileToServer extends AsyncTask<Void, Integer, String> {
@Override
protected void onPreExecute() {
// setting progress bar to zero
progressBar.setProgress(0);
super.onPreExecute();
}

@Override
protected void onProgressUpdate(Integer... progress) {
// Making progress bar visible
progressBar.setVisibility(View.VISIBLE);

// updating progress bar value
progressBar.setProgress(progress[0]);

// updating percentage value
txtPercentage.setText(String.valueOf(progress[0]) + "%");
}

@Override
protected String doInBackground(Void... params) {
return uploadFile();
}

@SuppressWarnings("deprecation")
private String uploadFile() {
String responseString = null;

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(Config.FILE_UPLOAD_URL);

try {
AndroidMultiPartEntity entity = new AndroidMultiPartEntity(
new ProgressListener() {

@Override
public void transferred(long num) {
publishProgress((int) ((num / (float) totalSize) * 100));
}
});

File sourceFile = new File(filePath);

// Adding file data to http body
entity.addPart("image", new FileBody(sourceFile));

// Extra parameters if you want to pass to server
entity.addPart("website",
new StringBody("www.androidhive.info"));
entity.addPart("email", new StringBody("abc@gmail.com"));

totalSize = entity.getContentLength();
httppost.setEntity(entity);

// Making server call
HttpResponse response = httpclient.execute(httppost);
HttpEntity r_entity = response.getEntity();

int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
// Server response
responseString = EntityUtils.toString(r_entity);
} else {
responseString = "Error occurred! Http Status Code: "
+ statusCode;
}

} catch (ClientProtocolException e) {
responseString = e.toString();
} catch (IOException e) {
responseString = e.toString();
}

return responseString;

}

@Override
protected void onPostExecute(String result) {
Log.e(TAG, "Response from server: " + result);

// showing the server response in an alert dialog
showAlert(result);

super.onPostExecute(result);
}

}

/**
* Method to show alert dialog
* */
private void showAlert(String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(message).setTitle("Response from Servers")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// do nothing
}
});
AlertDialog alert = builder.create();
alert.show();
}


}

这是我的 gradle 导入:

android {
compileSdkVersion 23
buildToolsVersion "24.0.2"
useLibrary 'org.apache.http.legacy'


packagingOptions {
exclude 'META-INF/DEPENDENCIES'
}

defaultConfig {
applicationId "com.example.myapplicationcamera"
minSdkVersion 22
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
// compile('org.apache.httpcomponents:httpcore:4.3')
compile files('libs/commons-io-2.4.jar')
compile files('libs/fluent-hc-4.3.1.jar')
compile files('libs/httpcore-4.3.jar')
compile files('libs/httpmime-4.3.1.jar')
compile files('libs/picasso-2.1.1.jar')
}

我在这个问题上坐了几个小时,但互联网上没有解决方案。有人有想法吗?

最佳答案

这确实正如异常所说,类加载器找不到该类。我可以建议几件事:

  1. 关闭项目,关闭工作室,打开并重试。

  2. 看看你的 Android 安装中是否确实有 org/apache 等。如果您有 jar 文件,则可以使用 jar tvfwhatever.jar 列出内容。将是一个很长的列表,所以 jar tvfwhatever.jar | grep apache

关于android - Java.lang.NoClassDefFoundError : Failed resolution of: Lorg/apache/http/entity/ContentType;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39415076/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com