Volley发送给PHP的解析请求

时间:2016-11-01 09:10:37

标签: php android android-volley

我想在android中使用Volly向数据库发送请求。这是我的要求

private class MyAsyncTask extends AsyncTask<String, Integer, Double>{

    @Override
    protected Double doInBackground(String... params) {
        // TODO Auto-generated method stub
        postData(params[0]);
        return null;
    }

    protected void onPostExecute(Double result){
        pb.setVisibility(View.GONE);
        Toast.makeText(getApplicationContext(), "Code Sent", Toast.LENGTH_LONG).show();
    }
    protected void onProgressUpdate(Integer... progress){
        pb.setProgress(progress[0]);
    }

    public void postData(String valueIWantToSend) {
        try {
            RequestQueue requestQueue = Volley.newRequestQueue(mContexy);
            String URL = Config.Get_FeedBackURL;
            JSONObject jsonBody = new JSONObject();
            jsonBody.put("email", SharedPrefs.getString(mContexy, SharedPrefs.KEY_email," "));
            jsonBody.put("ID", "ir.gfpishro.mobile");
            jsonBody.put("Author", SharedPrefs.getString(mContexy, SharedPrefs.KEY_name," "));
            jsonBody.put("comment", valueIWantToSend);
            final String mRequestBody = jsonBody.toString();

            StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Log.i("VOLLEY", response);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("VOLLEY", error.toString());
                }
            }) {
                @Override
                public String getBodyContentType() {
                    return "application/json; charset=utf-8";
                }

                @Override
                public byte[] getBody() throws AuthFailureError {
                    try {
                        return mRequestBody == null ? null : mRequestBody.getBytes("utf-8");
                    } catch (UnsupportedEncodingException uee) {
                        VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", mRequestBody, "utf-8");
                        return null;
                    }
                }

                @Override
                protected Response<String> parseNetworkResponse(NetworkResponse response) {
                    String responseString = "";
                    if (response != null) {
                        responseString = String.valueOf(response.statusCode);
                        responseString = String.valueOf(response.data);

                        // can get more details such as response.headers
                    }
                    return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
                }
            };

            requestQueue.add(stringRequest);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}

它将请求发送到php webservice但是我无法处理它,我在我的php文件中使用了这段代码

 $json = file_get_contents('php://input');
$objd = json_decode($_POST);

echo $objd["ID"];
//"ID", "ir.gfpishro.mobile"
     if ($objd["ID"]== "ir.gfpishro.mobile") {

        echo $obj["email"];
        echo $obj["Author"];
        echo $obj["comment"];

        //fetch product id from ibecon
        $stmt = $this->db->prepare("INSERT INTO `userComments`(`useremail`, `username`, `text`) VALUES ('as','dd','dddd')");

        $stmt->execute(array($obj["email"],$obj["Author"],$obj["comment"]));

        sendResponse(200, json_encode($obj));         

        return true;

    }else if(isset($_GET["companyid"])){


        sendResponse(203, json_encode($result));          

        return false;

    }



}

Php返回PHP Warning: json_decode() expects parameter 1 to be string, array given in然后返回200的状态代码。我想知道如何处理php文件中的发布数据。 感谢

1 个答案:

答案 0 :(得分:1)

这是因为json_decode接受一个字符串作为JSON,你试图转换所有超全局var,它是一个包含更多信息的数组,而不仅仅是你发送的数据。 尝试改为: $objd = json_decode($json,true);

注意到转换数组而不是stdObject的真正arg含义。 你也可以var_dump($ POST),你会看到它的结构如何。