如何从Android的JSON发送到PHP? [英] How to send json from android to php?

查看:272
本文介绍了如何从Android的JSON发送到PHP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要发布在Android JSON到PHP,我用排枪库 StringRequest 对象。

To post json from android to php, i used Volley library StringRequest object.

StringRequest sr = new StringRequest(Request.Method.POST,url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                // some code
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //some code
            }
        }){
            @Override
            protected Map<String,String> getParams(){
                Map<String, String> params = new HashMap<String, String>();
                ArrayList<Command> commands = MyApplication.readFromPreferences(getActivity(), Constants.COMMAND);
                String jsonCommands = new Gson().toJson(commands);
                params.put("commands", jsonCommands);
                return params;
            }
        };

和赶在PHP中的数据,并验证它是否是correcttly发,我用这个

And to catch the data in php and verify if it was sent correcttly, I used this

echo $_POST["commands"]; 

输出:

[{\"product\":{\"category_id\":1,\"created_at\":\"2015-06-13 17:49:58\",\"description\":\"CF77 COIN FINDER\",\"url_image\":\"IMG_76ECDC-707E7E-70AC81-0A1248-4675F3-F0F783.jpg\",\"name\":\"CF77 COIN FINDER\",\"pid\":12,\"price\":500.0},\"product_quantity\":3},{\"product\":{\"category_id\":1,\"created_at\":\"2015-06-13 17:49:58\",\"description\":\"JEOSONAR 3D DUAL SYSTEM\",\"url_image\":\"IMG_2D9DF0-2EB7E9-ED26C0-2C833B-B6A5C5-5C7C02.jpg\",\"name\":\"JEOSONAR 3D DUAL SYSTEM\",\"pid\":15,\"price\":500.0},\"product_quantity\":1},{\"product\":{\"category_id\":1,\"created_at\":\"2015-06-13 17:49:58\",\"description\":\"MAKRO POINTER\",\"url_image\":\"IMG_Macro.jpg\",\"name\":\"MAKRO POINTER\",\"pid\":18,\"price\":500.0},\"product_quantity\":3}]

我已经注意到,用排枪库POST方法发送JSON字符串时,很多反斜线已被添加到逃脱双引号。

I have noticed that when sending the json string with POST Method using Volley library, a lot of anti-slashes have been added to escape double quotes.

所以,这才是我的问题:

我要脱code JSON在PHP中对象的数组,所以我用

I want to decode json to an array of objects in php, so i used

$commands = json_decode( $_POST["commands"],true);

但它总是返回因为上述JSON的invalide(由反斜杠)。

But it always returns an empty array because of the invalide above json (caused by the anti-slashes).

有没有在PHP或Java SDK的提供用于发送和接收JSON无需此类问题合同的方法?或者我应该重新格式化的JSON在PHP,并删除所有反斜线?

Is there a method in php or in java SDK providing a contract for sending and receiving json without having this kind of problems? Or should i reformat the json in php and delete all the anti-slashes?

推荐答案

最后,我用一个自定义的 json_de code 方法,以清理解决我的问题解码之前,JSON字符串。

Finally, I solved my problem using a custom json_decode method in order to clean the json string before decoding it.

function json_clean_decode($json, $assoc = false, $depth = 512, $options = 0) {
    // search and remove comments like /* */ and //
    $json = preg_replace("#(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|([\s\t]//.*)|(^//.*)#", '', $json);
    // search and remove all backslashes
    $json = str_replace("\\","", $json);

    if(version_compare(phpversion(), '5.4.0', '>=')) {
        $json = json_decode($json, $assoc, $depth, $options);
    }
    elseif(version_compare(phpversion(), '5.3.0', '>=')) {
        $json = json_decode($json, $assoc, $depth);
    }
    else {
        $json = json_decode($json, $assoc);
    }

    return $json;
}

这篇关于如何从Android的JSON发送到PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆