【问题标题】:Unable to send Json object from android to php无法将 Json 对象从 android 发送到 php
【发布时间】:2019-06-26 22:18:42
【问题描述】:

我知道以前有人问过这个问题。请不要标记为重复或阻止我的帐户。

我正在尝试使用 volley 将 Json 对象发送到 php。所以我正在使用Customrequest,我在stackoverflow中找到了一个解决方案,但它不起作用See this

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";
}

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

【问题讨论】:

  • 如果之前有人问过这个问题,那为什么还要问呢?
  • json_decode(Values,true) 应该是什么?这是json_decode($_POST['Values'],true) 的错字吗?

标签: java php android


【解决方案1】:

关于您的 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)。

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

【讨论】:

  • 确定我已收到该消息,我只是将语句设置为“echo”Received”;而不是 $all_arraylist = json_decode(Values,true); $ result = print_r($all_arraylist, true); echo $result; 但它仍然无法正常工作
  • 在哪里你在寻找那个输出?您还可以使用 print_r($_POST);var_dump($_POST); 查看作为 POST 请求的一部分发送的所有内容。
  • 我正在查看 android 中的输出而不是 php 中的输出。
  • 如果我的Json对象发送成功,那么它必须在android中打印“Received”
  • 据我所知,您的 Android 代码中没有任何内容表明 Received 将随时输出。您可以使用 Wireshark 之类的工具来查看模拟器和您的 PHP 服务之间发生的情况。
猜你喜欢
  • 1970-01-01
  • 2015-07-07
  • 2014-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多