1

我正在尝试使用 multipart 将多个值从 android 发布到服务器

我所做的 - 我能够将一张图像(来自 android drawable)发送到服务器。

我现在的代码:

try
        {
            Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher); 

            HttpClient httpClient = new DefaultHttpClient();
            HttpPost postRequest = new HttpPost("http://10.0.2.2:7002/Details/");
            MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
            try{
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                bitmapOrg.compress(CompressFormat.JPEG, 75, bos);
                byte[] data = bos.toByteArray();
                ByteArrayBody bab = new ByteArrayBody(data, "forest.jpg");
                reqEntity.addPart("key", bab);
            }
            catch(Exception e){
                //Log.v("Exception in Image", ""+e);
                reqEntity.addPart("picture", new StringBody(""));
            }
            postRequest.setEntity(reqEntity);       
            HttpResponse response = httpClient.execute(postRequest);
            BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
            String sResponse;
            StringBuilder s = new StringBuilder();
            while ((sResponse = reader.readLine()) != null) {
                s = s.append(sResponse);
            }
        }catch(Exception e){
            e.getStackTrace();
        }

如何发送两张图片::这是我提出的模型

try
        {
                        Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
                        Bitmap bitmapOrg2 = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher2);   

            HttpClient httpClient = new DefaultHttpClient();
            HttpPost postRequest = new        HttpPost("http://10.0.2.2:7002/Details/");
            MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
            try{
                ByteArrayOutputStream bos = new ByteArrayOutputStream();

                                bitmapOrg.compress(CompressFormat.JPEG, 75, bos);       
                                bitmapOrg2.compress(CompressFormat.JPEG, 75, bos);

                                byte[] data = bos.toByteArray();

                    ByteArrayBody bab = new ByteArrayBody(data, "forest.jpg");
                                ByteArrayBody bab2 = new ByteArrayBody(data, "earth.jpg");

                                reqEntity.addPart("key", bab);
                                reqEntity.addPart("key1", bab2);
            }
            catch(Exception e){
                //Log.v("Exception in Image", ""+e);
                reqEntity.addPart("picture", new StringBody(""));
            }
            postRequest.setEntity(reqEntity);       
            HttpResponse response = httpClient.execute(postRequest);
            BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
            String sResponse;
            StringBuilder s = new StringBuilder();
            while ((sResponse = reader.readLine()) != null) {
                s = s.append(sResponse);
            }
        }catch(Exception e){
            e.getStackTrace();
        }

我的问题:: 我是否遵循正确的方法来传递上面的多个图像,还是有更好的方法来做到这一点?

4

2 回答 2

1

是的,有更好的方法来上传或下载多个文件。首先,您必须定义一个 AsyncTask,并且在他的doInBackground函数中,您必须编写代码来上传/下载文件。

这样,首先您的视图在下载/上传时不会冻结。多个文件将被异步午餐。你可以像下面这样简单地调用它:

new MyAsyncTask(fileUrl1).execute();
new MyAsyncTask(fileUrl2).execute();

为了使用 HTTP 请求,这是一种更好且更易于管理的方法。

希望我对你有所帮助。祝你好运

于 2013-12-09T10:35:25.497 回答
0

我这样做的方法:

public String getStringImage(Bitmap bmp) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.JPEG, 40, baos);
        byte[] imageBytes = baos.toByteArray();
        return Base64.encodeToString(imageBytes, Base64.DEFAULT);
    }

并将图像作为字符串发送

在服务器中(使用 Slim 框架):

$image = $app->request()->post('img_p');

file_put_contents($my_path, base64_decode($image));

我不确定这是更好的方法,但它确实有效。

于 2017-01-17T21:24:00.623 回答