Android/Java:找不到MultipartEntity类,尽管将其添加到构建路径中



我正在编写一个应用程序,可以让用户选择手机上已有的照片并将其上传到服务器。不幸的是,当我点击按钮将照片发送到服务器时,在logcat中我看到:

Could not find class 'org.apache.http.entity.mime.MultipartEntity', referenced from method com.example.crescentcameraapp.MainActivity$ImageUploadTask.doInBackground

我已经看到了所有类似的问题存在于堆栈溢出:

  • Android使用multiparentity向服务器发布图像

  • http post没有响应

  • Dalvik无法通过反射在外部JAR中找到class

还有一些问题,但关键是,它们都没有一个好的答案。解决方案包括:

  • "从http://hc.apache.org/downloads.cgi并将它们添加到Java Build中Path in Eclipse。"**不工作。我试过了,洗过了项目,关闭/打开Eclipse。没有。

  • "将所有jar文件添加到项目的libs文件夹中,Android会这样做休息。"**还是不行。我不知道为什么。

由于某种原因,即使它将此错误发布到logcat,它仍然继续运行,直到后来当我尝试使用我在代码中创建的multiparentity对象时才崩溃。下面是日志的其余部分:

06-17 21:21:34.396: E/dalvikvm(14917): Could not find class 'org.apache.http.entity.mime.MultipartEntity', referenced from method com.example.crescentcameraapp.MainActivity$ImageUploadTask.doInBackground
06-17 21:21:34.396: W/dalvikvm(14917): VFY: unable to resolve new-instance 652 (Lorg/apache/http/entity/mime/MultipartEntity;) in Lcom/example/crescentcameraapp/MainActivity$ImageUploadTask;
06-17 21:21:34.396: D/dalvikvm(14917): VFY: replacing opcode 0x22 at 0x002b
06-17 21:21:34.396: I/dalvikvm(14917): DexOpt: unable to optimize static field ref 0x0422 at 0x2d in Lcom/example/crescentcameraapp/MainActivity$ImageUploadTask;.doInBackground
06-17 21:21:34.396: D/dalvikvm(14917): DexOpt: unable to opt direct call 0x10e8 at 0x2f in Lcom/example/crescentcameraapp/MainActivity$ImageUploadTask;.doInBackground
06-17 21:21:34.396: D/dalvikvm(14917): DexOpt: unable to opt direct call 0x10eb at 0x56 in Lcom/example/crescentcameraapp/MainActivity$ImageUploadTask;.doInBackground
06-17 21:21:34.396: D/dalvikvm(14917): DexOpt: unable to opt direct call 0x10eb at 0x62 in Lcom/example/crescentcameraapp/MainActivity$ImageUploadTask;.doInBackground
06-17 21:21:34.396: D/dalvikvm(14917): DexOpt: unable to opt direct call 0x10ea at 0x6e in Lcom/example/crescentcameraapp/MainActivity$ImageUploadTask;.doInBackground
06-17 21:21:34.396: D/dalvikvm(14917): DexOpt: unable to opt direct call 0x10eb at 0x85 in Lcom/example/crescentcameraapp/MainActivity$ImageUploadTask;.doInBackground
06-17 21:21:34.401: W/dalvikvm(14917): threadid=11: thread exiting with uncaught exception (group=0x412192a0)
06-17 21:21:34.406: E/AndroidRuntime(14917): FATAL EXCEPTION: AsyncTask #1
06-17 21:21:34.406: E/AndroidRuntime(14917): java.lang.RuntimeException: An error occured while executing doInBackground()
06-17 21:21:34.406: E/AndroidRuntime(14917):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
06-17 21:21:34.406: E/AndroidRuntime(14917):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
06-17 21:21:34.406: E/AndroidRuntime(14917):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
06-17 21:21:34.406: E/AndroidRuntime(14917):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
06-17 21:21:34.406: E/AndroidRuntime(14917):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-17 21:21:34.406: E/AndroidRuntime(14917):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
06-17 21:21:34.406: E/AndroidRuntime(14917):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
06-17 21:21:34.406: E/AndroidRuntime(14917):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
06-17 21:21:34.406: E/AndroidRuntime(14917):    at java.lang.Thread.run(Thread.java:856)
06-17 21:21:34.406: E/AndroidRuntime(14917): Caused by: java.lang.NoClassDefFoundError: org.apache.http.entity.mime.MultipartEntity
06-17 21:21:34.406: E/AndroidRuntime(14917):    at com.example.crescentcameraapp.MainActivity$ImageUploadTask.doInBackground(MainActivity.java:244)
06-17 21:21:34.406: E/AndroidRuntime(14917):    at com.example.crescentcameraapp.MainActivity$ImageUploadTask.doInBackground(MainActivity.java:1)
06-17 21:21:34.406: E/AndroidRuntime(14917):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-17 21:21:34.406: E/AndroidRuntime(14917):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
06-17 21:21:34.406: E/AndroidRuntime(14917):    ... 5 more
06-17 21:21:43.731: I/Choreographer(14917): Skipped 534 frames!  The application may be doing too much work on its main thread.
06-17 21:21:44.856: E/WindowManager(14917): Activity com.example.crescentcameraapp.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41c93710 that was originally added here
06-17 21:21:44.856: E/WindowManager(14917): android.view.WindowLeaked: Activity com.example.crescentcameraapp.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41c93710 that was originally added here

还有更多,但你懂的。下面是相关代码。ImageUploadTask是一个内部类:

class ImageUploadTask extends AsyncTask <Void, Void, String>{
    @SuppressWarnings("deprecation")
    @Override
    protected String doInBackground(Void... unsued) {
        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpContext localContext = new BasicHttpContext();
                HttpPost httpPost = new HttpPost(
                        getString(R.string.web_url_text)
                                + "/test.php"
                );
            MultipartEntity entity = new MultipartEntity(
                    HttpMultipartMode.BROWSER_COMPATIBLE);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            imageBMP.compress(CompressFormat.JPEG, 100, bos);
            byte[] data = bos.toByteArray();
            entity.addPart("photoId", new StringBody(getIntent()
                    .getStringExtra("photoId")));
            entity.addPart("returnformat", new StringBody("json"));
            entity.addPart("uploaded", new ByteArrayBody(data,
                    "myImage.jpg"));
            String captionText = findViewById(R.string.caption_text).toString();
            entity.addPart("photoCaption", new StringBody(captionText));
            httpPost.setEntity(entity);
            HttpResponse response = httpClient.execute(httpPost,
                    localContext);
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(
                            response.getEntity().getContent(), "UTF-8"));
            String sResponse = reader.readLine();
            return sResponse;
        } catch (Exception e) {
            if (dialog.isShowing())
                dialog.dismiss();
            Toast.makeText(getApplicationContext(),
                    "Error sending file to server...",
                    Toast.LENGTH_LONG).show();
            Log.e(e.getClass().getName(), e.getMessage(), e);
            return null;
        }//end of catch
    }
    protected void onPostExecute(String sResponse) {
        Toast.makeText(getApplicationContext(), "Upload completed.", Toast.LENGTH_LONG).show();
    }//onPostExecute()
}//end of class ImageUploadTask

谢谢。

我找到了答案,部分是基于我链接到的相关问题提供的解决方案。我清理了项目,重新启动eclipse,然后下载并添加了mime4j到我的构建路径:

http://james.apache.org/download.cgi Apache_Mime4J

上面的页面是官方下载,但它不适合我。试试sourceforge的替代方案:

http://sourceforge.net/projects/mime4j/?source=dlp

注意:

添加这个几乎肯定会导致项目中的bin/dexedlib出现问题。如果是这样,你可能会遇到一场噩梦。您将获得关于跨多个jar定义的某些对象/类/异常的控制台错误消息。这是因为dexedLibs文件夹表面上包含libs文件夹中jar的副本。只需从项目中删除原始副本(libs文件夹中的副本),清理解决方案,然后重新构建它。您可能会在控制台中收到关于多个定义类的新抱怨,但这只是意味着您有更多的jar需要删除。这就是我解决这个问题的方法。

我知道至少在Eclipse中有一个Android插件,如果你在gen (generated)文件夹中粘贴一个类,带有"R"和BuildConfig类,它可能会被删除。

相关内容

  • 没有找到相关文章

最新更新