从android上传到服务器

时间:2016-03-22 06:56:18

标签: php android android-volley slim

使用put-volley-request向运行php / slim的服务器发送base64编码的图像时遇到一些问题。

当我在发送数据之前输出数据看起来不错,但是一旦我在服务器上获取它,数据就会被破坏/削减。

我发送图片的Android代码如下所示:

private void updateImage(Bitmap myImage) {
    String tag_string_req = "req_updateimage";

    String updateUrl = String.format(API_DOMAIN + AppConfig.URL_UPDATE_IMAGE);
    StringRequest strReq = new StringRequest(Request.Method.PUT, updateUrl, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {

            try {
                //do some stuff
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "Error: " + error.getMessage());
        }

    }){

        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            params.put("Content-Type", "application/json; charset=UTF-8");

            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            myImage.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
            byte[] byteArray = byteArrayOutputStream.toByteArray();
            String base64Image = Base64.encodeToString(byteArray, Base64.DEFAULT);
            params.put("image", base64Image);

            System.out.println(params);
            return params;
        }
    };

    AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}

在我的服务器上,我有一个类似的功能:

$app->put('/image', function() use ($app){
    $image = $app->request->put('image');
    $response['error'] = false;
    $response['image'] = $image;
    echoResponse(200, $response);
});

我的android-function中的输出看起来像一个正确的base64编码图像。 在我的服务器上,整个图像只有大约30%。你能帮助我吗?我的Bug在哪里?有什么问题?

提前致谢,

RiPr

1 个答案:

答案 0 :(得分:0)

您似乎没有设置正确的内容类型。

你的Java代码中的

......

params.put(“Content-Type”,“application / json; charset = UTF-8”);

告诉你的苗条应用,你试图发送的数据是应用程序/ json格式......它不是。

尝试更改编码以匹配您要发送的数据结构。