簡體   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