dongyihao1099 2016-11-01 09:10
浏览 88
已采纳

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 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化