dongyihao1099
dongyihao1099
2016-11-01 09:10
浏览 87

Volley发送给PHP的解析请求

I want to send a request to database using Volly in android. This is my request

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();
        }
    }

}

It sends request into php webservice but I can not handle it,I used this code in my php file

 $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 return PHP Warning: json_decode() expects parameter 1 to be string, array given in and then return status code of 200.I want to know How can I handle posted data in php file. Thanks

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanjue2576
    duanjue2576 2016-11-01 09:25
    已采纳

    This is because json_decode accept a string as JSON , you're trying to convert all superglobal var wich is an array containing more information than just the data you're sending. Try to do instead : $objd = json_decode($json,true);

    Notices the true arg meaning to cast array instead of stdObject. Also you can var_dump($POST) and you will see how is the structure of it.

    点赞 评论

相关推荐