无法将Android的Base64字符串发送到php

时间:2015-04-21 12:23:08

标签: php android base64

我有一个AsyncTask,看起来像这样:

protected Void doInBackground(Void... notUsed) {
        /************ Make Post Call To Web Server ***********/
        BufferedReader reader = null;

        // Send data
        try {
            // Defined URL  where to send data
            URL url = new URL("http://www.maarten-vandeperre.be/demo/server_lokeren_POST.php");
            // Send POST data request
            String data ="&" + URLEncoder.encode("data", "UTF-8") + "=";

            URLConnection conn = url.openConnection();
            conn.setDoOutput(true);
            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
            wr.write(data);
            writeNotifications(wr);
            //wr.write( data + "data" );
            //writeNotifications(wr);
            wr.flush();

            // Get the server response

            reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            StringBuilder sb = new StringBuilder();
            String line = null;

            // Read Server Response
            while((line = reader.readLine()) != null) {
                // Append server response in string
                sb.append(line);
            }

            // Append Server Response To Content String
            Log.i(TAG, "server response: " + sb.toString());
            action.doAction();
        }
        catch(Exception e){
            Log.e(TAG, Log.getStackTraceString(e));
            error = e.getMessage();
        }
        finally{
            try{
                reader.close();
            }
            catch(Exception ex) {}
        }
        /*****************************************************/
        return null;
    }

和writer方法,其中base64编码的方法如下:

private void writeNotificationData(JsonWriter writer) throws IOException {
        writer.beginObject();
        writer.name("comment").value(notification.getComment());
        writer.name("category").value(getCategory(notification.getCategoryId()));
        writer.name("urgency").value(getUrgency(notification.getUrgencyId()));
        writer.name("street").value(notification.getAddress().getStreet());
        writer.name("houseNumber").value(notification.getAddress().getHouseNumber());
        writer.name("latitude").value(notification.getAddress().getPosition().latitude);
        writer.name("longitude").value(notification.getAddress().getPosition().longitude);
        Log.i(TAG, "write image urls: " + notification.getImageUrls());
        if(notification.getImageUrls() != null && notification.getImageUrls().size() > 0) {
            String base64 = encodeToBase64(notification.getImageUrls().get(0));
            Log.i(TAG, "image to base64: " + base64);
            writer.name("image").value(base64);
        }
        writer.endObject();
    }

    private String encodeToBase64(final String url) {
        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        Bitmap bitmap = BitmapFactory.decodeFile(url);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bao);
        byte [] ba = bao.toByteArray();
        String ba1= Base64.encodeToString(ba, Base64.NO_WRAP);
        return ba1;
    }

当我不发送base64字符串,但是随机发送一个字符串(RandomStringUtils.randomAlphabetic(2000))时,我会在服务器日志中看到它。 当我使用上面发布的代码时,没有任何内容弹出。 我在某处错过了编码/解码吗?

这是服务器收到的字符串:

MSOImVAPfCBGNDXJNuHtFGhmtiUqQXJSlstpcEaGUYyyVeOezyyJsqeJxvlorvFMDxGtbIPMXRyvDSmUZuQadaAFzGdVFBfObiTHLTkuvweKpEylfWEBUhCqaCpBUQUjdTNbXzXTJWqLWDanhHimJoaPNtthENmMUYbxVCKUYVQdqVdbZyewRIArgcCHvLhioCAjqUswiZNxoXwzVQCwGSHpvBRLKEcsLhfFUHcyAtUQmHHQUzZBapcNhLpxJAuasXuAuzaxqBiaQhiKGTvNOGSeNNZRBAYCLIosrzkIYZElFgRiVbYqebKmBEXxBDxbCFdIIXPEZhruKVLLIBfsImlFevGaWouUYSbMITTDPVlcEmsTPDekEVwCOmpHfmUwFQlFDYzUenRAxBHgLIUqRgETQexKViWyNMCycRJLUehSWLNGSNGpOUIrPILfXVkUDfEQHkOKwcOkoTAWGZvjivNJNRkipecNcLTOYazuhKOLODmYFgkfQhpzImQQLBVBelRtMXtaSFLCeMXptiYrMSQqkEYocPKYBSizkzTmXYQTARzqmvGdwQbLiMiNKWNkJQkYtsCZAzCZbVHFmHcdtQCbgbypBZadzIAaSFgoikiVnRVmshxoikqKnulgPPPgJkwTIlRwsGkLFbZTPCPXkCvpRMYZiNnzpTuAbEKtmwgLkukPjjeDPiHuvVbkgDgHYQffGJoZirxRaRblJNkVGfSNWJsETcXYRrdySFlyclnlzzMmAdLKKIbvjVaxrzaBiUujbEyFESqpvUBAMpLzisMIsekJuhIDnKpdhDsnmvHYhmaFlKzkDDUHAkTAtfzbUGXIu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zHJ5vlf6CIIP+XzqwP17c9eASQTzYjtmvrK2juY8ywQDTvPGPtdsMt/p/XnqMjt6klqq3ZuZQTZHbMb4XHGD/AKF/ZzA/y/Mn0Jrda8MU88ITyRYwD/T8Zz/xMmI6t7aeepJBYZ5JrqoUFQTino7fg5O703d/81d3McvwCs9kkovVL/p4ttW0229W73Tbs0pQxWC2SR2xSWW3lxcT/wCjnNzekt7c5AHPUfNjJGTIk8NpFHELqKYXv+kYgAFpwdW988447nJ5IBrGjSC8bUbiW2lE8k4uJ7mA6gTyX5wDjpnr7Y5DCrOloczeaPOkWcZgt+M3m98ep688H07Nzp7Nd3966f1/w5zUaVf2zw+8ltzW0+LRteWu97yfM0k27mjXdjbwtJ5ctsROLeU5A6ls6h/yB/8Aiaf24PX0GCCDVqGRP3zobriACET259ZMc/qceoBJIJMiQXC6bPGZcp/oVtb9rT/kIn/iYdSOgz7HHJJJNf7TcJC19H9ktJZvsNx+/g7ZcD09Gx3znJ4yeCrQw/vW01WqS7SVumnu3a3u9nY78GnR5nK65rKzutE53eqb6qy0d77KR0EmpRkRW9vcWsIhmvLiY/aP9E+8R6/hjnr1PNc3PFhY57f/AF2o/wCv7WZssv7+4xnPXPqaoaWgWe8uI/8ATIuMQTwEcf2ew44/T/d5OCa1kPm6dJJeS2sYl2+dcdLPqf7OGD1+6evOCepwa2wdH2Kmt20rvXu13e6tby3u22aYer7au01po09ndOS2Tenw21vo7uTbtFa2dzZwTafqsksVpLOBB+4FoTnI6Y7f2Yf++h1wTUyIq2M0MVndXVr

我现在坚持这个,而stackoverflow上的其他答案似乎无法解决问题......

1 个答案:

答案 0 :(得分:0)

使用“encodeToBase64”的这个实现解决了它。正如greenapps建议的那样,我们不应该使用位图来获取图像文件的字节。

private String encodeToBase64(final String url){
        try {
            File file = new File(url);
            byte[] bFile = new byte[(int) file.length()];
            FileInputStream inputStream = new FileInputStream(url);
            inputStream.read(bFile);
            inputStream.close();
            return Base64.encodeToString(bFile, Base64.NO_WRAP);
        } catch (IOException e) {
            Log.e(TAG, Log.getStackTraceString(e));
        }
        return null;
    }