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.

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

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?