繁体   English   中英

无法将Json对象从android发送到php

[英]Unable to send Json object from android to php

我知道这个问题曾被问过。 请不要将其标记为重复或阻止我的帐户。

我正在尝试使用齐射将Json对象发送到php。 所以,我使用Customrequest ,我已经找到了一个计算器解决方案,但它没有工作见本

JsonObject response = new JsonObject();
response.add("Description", a1 );/*a1 is Json array of multiple values*/
response.add("Amount", a2 );/*a2 is Json array of multiple values*/
System.out.println("Json:"+response);/*Both the list is saved inside this Json object*/

Map<String,String> hm = new HashMap();
hm.put("Values",String.valueOf(response));/*Trying to pass the json object to server respose is the Json object*/

CustomRequest req = new 
CustomRequest(Request.Method.POST,ip,hm, this.createRequestSuccessListener(), this.createRequestErrorListener()); /*I have used the solution here*/

Mysingleton.getInstance(this).addTorequestque(req);/*This is a singleton class*/
public Response.Listener<JSONObject> createRequestSuccessListener(){
    return new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            AlertDialog.Builder al = new AlertDialog.Builder(Database.this)
                .setTitle("Message")
                .setMessage("Sent")
                .setPositiveButton("OK",null);
            AlertDialog al1 = al.create();
            al.show();
        }
    };
}

public Response.ErrorListener createRequestErrorListener(){
    return new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            AlertDialog.Builder al = new AlertDialog.Builder(Database.this)
                .setTitle("Error")
                .setMessage("Something went wrong")
                .setPositiveButton("OK",null);
            AlertDialog al1 = al.create();
            al.show();
        }
    };
}

我的PHP源代码在这里

if(!empty($_POST['Values']){
    $all_arraylist = json_decode(Values,true);
    $ result = print_r($all_arraylist,true);
    echo $result;
}
else{
    echo "Empty";
}

至少会弹出错误消息,但我什至无法做到这一点。 那是我没有得到任何回应。 请帮忙

关于您的Java代码我什么也没说,但是您的PHP代码根本无效。 这通常会导致空白屏幕完全没有任何内容提供给客户端。

if(!empty($_POST['Values']){
    $all_arraylist = json_decode(Values,true);

您可能是说json_decode($_POST['Values'], true); 在这里,因为仅凭Values本身没有任何意义。

    $ result = print_r($all_arraylist,true);

$后面的空格会使PHP感到困惑,因为这不是有效的变量名字符(并且您也不会在下一行使用$ result )。

(并且可以使用print_r($all_arraylist)使其打印出所有内容,而不必使用trueprint_r返回值,然后echo返回的值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM