I am trying to upload a image file to server and get back a JSON
Object.

But an IOException was thrown out. Following is my codes, can anybody
have a look at them?

Thanks.

private static String httpPostFileUpload(HttpClient client,
                        String fileName, String uploadUrl) {
                String result = "";
                File file = new File(Environment.getExternalStorageDirectory() 
+ "/"
+ fileName);
                Log.d(TAG, file.getName());
                //FileEntity reqEntity = new FileEntity(file, "jpg");
                MultipartEntity
                ContentBody bin = new FileBody(file);
                try {
                        reqEntity.addPart("string_field",
                                        new StringBody("field value"));
                } catch (UnsupportedEncodingException e) {
                        Log.e(TAG, e.toString());
                }
                reqEntity.addPart("attached_file", bin);
                HttpPost httpPost = new HttpPost(uploadUrl);
                httpPost.setHeader("Content-Type", "multipart/form-data");
                // Entity to hold request
                httpPost.setEntity(reqEntity);
                HttpResponse response = null;
                // Entity to hold response
                HttpEntity respEntity = null;
                try {
                        response = client.execute(httpPost);
                        respEntity = response.getEntity();
                        if (respEntity != null) {
                                InputStream inStream = respEntity.getContent();
                                result = convertStreamToString(inStream);
                        }
                } catch (ClientProtocolException e) {
                        Log.e(TAG, "ClientProtocolException: " + 
e.getMessage());
                } catch (IOException e) {
                        Log.e(TAG, "IOException: " + e.getMessage());
                }
                return result;
        }

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to