通过HTTP表单上传文件,通过MultipartEntityBuilder - 在create()调用时出错

时间:2013-11-25 07:50:57

标签: android http file-upload multipartform-data multipartentity

我正在尝试使用apache http库和MultipartEntityBuilder来编写上传AsyncTask,我正在使用一段代码from this post

在项目库中,我添加了

httpclient-4.3.1.jar
httpclient-cache-4.3.1.jar
httpcore-4.3.jar
httpmime-4.3.1.jar

这是清单摘录

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="19" />

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CALL_PHONE" />

在AsyncTask中,我添加了库,并按如下方式包含它们:

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;

我有一些Log.d来识别错误的位置,它似乎来自MultipartEntityBuilder builder = MultipartEntityBuilder.create();行,错误描述为VFY: unable to resolve static method 5641: Lorg/apache/http/entity/mime/MultipartEntityBuilder;.create ()...

任何建议?

这是我的代码:

public class AsyncAdPoster extends AsyncTask<Void, Void, String> {

    // Exception Management
    private Exception exception;

    @Override
    protected String doInBackground(Void... arg0) {
        // TODO Auto-generated method stub
        try {
            Log.d("DEBUG", "START [...]");

            String resultPost;
            resultPost = postAd(MainActivity.postTitle,
                MainActivity.postDescription,
                MainActivity.bitmapFullresPhoto);
            return resultPost;

        } catch (Exception e) {
            this.setException(e);
            return null;
        }
    }

    // Prepare the interface before starting the task
    protected void onPreExecute() {

    }

    // Update the view with the updated data from the adsList
    protected void onPostExecute(String result) {
        Log.d("DEBUG", "RESULT [" + result + "]");
    }

    public Exception getException() {
        return exception;
    }

    public void setException(Exception exception) {
        this.exception = exception;
    }

    /**
     * Upload management
     */
    public static String postAd(String myTitle,
                String myDescription,
                Bitmap myPhoto ) throws Exception {

        Log.d("DEBUG", "INSIDE [1]");

        HttpClient client = new DefaultHttpClient();
        Log.d("DEBUG", "INSIDE [1a]");
        HttpPost post = new HttpPost(MainActivity.POST_URL);
        Log.d("DEBUG", "INSIDE [1b]");
        MultipartEntityBuilder builder = MultipartEntityBuilder.create(); // ERROR HERE
        Log.d("DEBUG", "INSIDE [1c]");
        builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

        //  fetch the picture in MainActivity.
        //final File file = new File(fileName);
        //FileBody fb = new FileBody(file);
        //builder.addPart("file", fb);
        Log.d("DEBUG", "INSIDE [2]");

        if( myPhoto != null ) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            myPhoto.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            byte[] imageBytes = baos.toByteArray();
            String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
            builder.addTextBody("photo", encodedImage);
        }
        Log.d("DEBUG", "INSIDE [3]");

        builder.addTextBody("title", myTitle);
        builder.addTextBody("description", myDescription);
        Log.d("DEBUG", "INSIDE [4]");

        final HttpEntity yourEntity = builder.build();

        class ProgressiveEntity implements HttpEntity {
            @Override
            public void consumeContent() throws IOException {
                yourEntity.consumeContent();                
            }
            @Override
            public InputStream getContent() throws IOException,
                    IllegalStateException {
                return yourEntity.getContent();
            }
            @Override
            public Header getContentEncoding() {             
                return yourEntity.getContentEncoding();
            }
            @Override
            public long getContentLength() {
                return yourEntity.getContentLength();
            }
            @Override
            public Header getContentType() {
                return yourEntity.getContentType();
            }
            @Override
            public boolean isChunked() {             
                return yourEntity.isChunked();
            }
            @Override
            public boolean isRepeatable() {
                return yourEntity.isRepeatable();
            }
            @Override
            public boolean isStreaming() {             
                return yourEntity.isStreaming();
            } // CONSIDER put a _real_ delegator into here!

            @Override
            public void writeTo(OutputStream outstream) throws IOException {

                class ProxyOutputStream extends FilterOutputStream {

                    public ProxyOutputStream(OutputStream proxy) {
                        super(proxy);    
                    }
                    public void write(int idx) throws IOException {
                        out.write(idx);
                    }
                    public void write(byte[] bts) throws IOException {
                        out.write(bts);
                    }
                    public void write(byte[] bts, int st, int end) throws IOException {
                        out.write(bts, st, end);
                    }
                    public void flush() throws IOException {
                        out.flush();
                    }
                    public void close() throws IOException {
                        out.close();
                    }
                } // CONSIDER import this class (and risk more Jar File Hell)

                class ProgressiveOutputStream extends ProxyOutputStream {
                    public ProgressiveOutputStream(OutputStream proxy) {
                        super(proxy);
                    }
                    public void write(byte[] bts, int st, int end) throws IOException {

                        // FIXME  Put your progress bar stuff here!

                        out.write(bts, st, end);
                    }
                }

                yourEntity.writeTo(new ProgressiveOutputStream(outstream));
            }

        };

        ProgressiveEntity myEntity = new ProgressiveEntity();
        Log.d("DEBUG", "INSIDE [5]");

        post.setEntity(myEntity);
        HttpResponse response = client.execute(post);        

        return getContent(response);

        */

        return "";

    } 

    public static String getContent(HttpResponse response) throws IOException {
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        String body = "";
        String content = "";

        while ((body = rd.readLine()) != null) 
        {
            content += body + "\n";
        }
        return content.trim();
    }


}

0 个答案:

没有答案