无法在异步任务

时间:2015-09-24 09:23:32

标签: android android-asynctask multipartentity

我正在尝试使用mime.MultipartEntityBuilder上传视频文件,但我在logcat中收到以下错误

09-24 14:38:59.673: E/AndroidRuntime(3072): FATAL EXCEPTION: AsyncTask #1
09-24 14:38:59.673: E/AndroidRuntime(3072): java.lang.RuntimeException: An error occured while executing doInBackground()
09-24 14:38:59.673: E/AndroidRuntime(3072):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
09-24 14:38:59.673: E/AndroidRuntime(3072):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
09-24 14:38:59.673: E/AndroidRuntime(3072):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
09-24 14:38:59.673: E/AndroidRuntime(3072):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
09-24 14:38:59.673: E/AndroidRuntime(3072):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-24 14:38:59.673: E/AndroidRuntime(3072):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
09-24 14:38:59.673: E/AndroidRuntime(3072):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-24 14:38:59.673: E/AndroidRuntime(3072):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-24 14:38:59.673: E/AndroidRuntime(3072):     at java.lang.Thread.run(Thread.java:856)
09-24 14:38:59.673: E/AndroidRuntime(3072): Caused by: java.lang.NoClassDefFoundError: org.apache.http.entity.ContentType
09-24 14:38:59.673: E/AndroidRuntime(3072):     at org.apache.http.entity.mime.content.FileBody.<init>(FileBody.java:89)
09-24 14:38:59.673: E/AndroidRuntime(3072):     at a.today.a.async.UploadVideoAsync.doInBackground(UploadVideoAsync.java:53)
09-24 14:38:59.673: E/AndroidRuntime(3072):     at a.today.a.async.UploadVideoAsync.doInBackground(UploadVideoAsync.java:1)
09-24 14:38:59.673: E/AndroidRuntime(3072):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
09-24 14:38:59.673: E/AndroidRuntime(3072):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-24 14:38:59.673: E/AndroidRuntime(3072):     ... 5 more

这是我的代码库:

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.util.EntityUtils;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.entity.mime.content.FileBody;

import android.os.AsyncTask;
import android.util.Log;

public class UploadVideoAsync extends AsyncTask<String, String, Object[]> {


    @Override
    protected Object[] doInBackground(String... params) {

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(params[0].toString());
        final File video = new File(params[1].toString());
        //String video = params[1].toString();
        ContentBody  cb = new FileBody(video);
        try{
            MultipartEntityBuilder entityBuilder  = MultipartEntityBuilder.create();
            entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
            //entityBuilder.addPart("video", cb);
            entityBuilder.addBinaryBody("video", video);
            entityBuilder.addTextBody("sid", params[2].toString());
            HttpEntity entity = entityBuilder.build();
            httppost.setEntity(entity);
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity httpEntity = response.getEntity();

            Log.v("result", EntityUtils.toString(httpEntity));

            return new Object[]{response, new BasicResponseHandler().handleResponse(response)};
        }catch (ClientProtocolException e) {

            e.printStackTrace();
       }
        return new Object[0];

    }


}

我无法找到确切的问题。有人能说出代码有什么问题吗?

1 个答案:

答案 0 :(得分:0)

好的,这个解决了这个问题:

  

项目 - &gt;属性 - &gt; Java构建路径 - &gt;订单和出口 - &gt;校验   jar文件 - &gt;行

这是截图:

Java build path checked jars