相关疑难解决方法(0)

如何使用Android的MultipartEntity将多个图像发送到服务器

您好我正在使用以下代码将图像和文本发送到php webservice.

try {


            HttpClient httpClient = new DefaultHttpClient();
            HttpContext localContext = new BasicHttpContext();
            HttpPost httpPost = new HttpPost(URL);

            MultipartEntity entity = new MultipartEntity(
                    HttpMultipartMode.BROWSER_COMPATIBLE);

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            bitmap.compress(CompressFormat.JPEG, 75, bos);
            byte[] data = bos.toByteArray();

            entity.addPart("files[]",
                    new ByteArrayBody(data, "myImage.jpg"));

            entity.addPart("message0", new StringBody(caption.getText()
                    .toString()));

            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(ImageUpload.this, e.getMessage(),
                    Toast.LENGTH_LONG).show();
            Log.e(e.getClass().getName(), e.getMessage(), e); …
Run Code Online (Sandbox Code Playgroud)

android

7
推荐指数
1
解决办法
2万
查看次数

标签 统计

android ×1