Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
305 views
in Technique[技术] by (71.8m points)

android - Upload Photo using HttpPost MultiPartEntityBuilder

I am trying to upload the taken photo to server. this is what i do:

public JSONObject makePostFileRequest(String url, String photoFile) {
    try {
        // photoFile = /path/tofile/pic.jpg
        DefaultHttpClient httpClient = GlobalData.httpClient;
        HttpPost httpPost = new HttpPost(url);

        File file = new File(photoFile);
        FileBody fileBody = new FileBody(file); // here is line 221

        MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();

        multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
        multipartEntity.addPart("PhotoMessage", fileBody);

        httpPost.setEntity(multipartEntity.build());

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();

I get this error:

11-29 13:12:14.924: E/AndroidRuntime(15781): Caused by: java.lang.NoClassDefFoundError: org.apache.http.entity.ContentType 11-29 13:12:14.924: E/AndroidRuntime(15781): at org.apache.http.entity.mime.content.FileBody.(FileBody.java:89) 11-29 13:12:14.924: E/AndroidRuntime(15781): at com.petcial.petopen.custom.JSONParser.makePostFileRequest(JSONParser.java:221)

What am I doing wrong?


Update

InputStream inputStream;
inputStream = new FileInputStream(new File(photoFile));
byte[] data;
data = IOUtils.toByteArray(inputStream);

httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT,
                        System.getProperty("http.agent"));
InputStreamBody inputStreamBody = new InputStreamBody(new ByteArrayInputStream(data), "Pic.jpg");

MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();

multipartEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.addPart("PhotoMessage", inputStreamBody);

httpPost.setEntity(multipartEntity.build());

HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();

here is the error:

11-29 14:00:33.364: E/AndroidRuntime(19478): Caused by: java.lang.NoClassDefFoundError: org.apache.http.util.Args 11-29 14:00:33.364: E/AndroidRuntime(19478): at org.apache.http.entity.mime.content.AbstractContentBody.(AbstractContentBody.java:48) 11-29 14:00:33.364: E/AndroidRuntime(19478): at org.apache.http.entity.mime.content.InputStreamBody.(InputStreamBody.java:69) 11-29 14:00:33.364: E/AndroidRuntime(19478): at org.apache.http.entity.mime.content.InputStreamBody.(InputStreamBody.java:62) 11-29 14:00:33.364: E/AndroidRuntime(19478): at com.petcial.petopen.custom.JSONParser.makePostFileRequest(JSONParser.java:233)

these libraries solved my issue:

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

enter image description here Check that jar in Order and Export tab and run.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...