JSONArray中的POST字符串和响应

时间:2016-10-10 04:19:04

标签: php android json http-post android-volley

实际上我遵循了许多与我的问题相关的stackoverflow个答案,但没有一个答案是有效的,我只是发布一个字符串,作为回报,我想要一个json数组

注意:当我运行我的硬编码脚本时,它非常好但在脚本中POST值显示为null 这是我的Android代码:

 private void getData(){
    Bundle extras=getIntent().getExtras();
    final String id = extras.getString("value").toString().trim();
    JSONObject obj =new JSONObject();

    final ProgressDialog loading = ProgressDialog.show(Categories.this, "Please wait...","Fetching data...",false,false);
    Volley.newRequestQueue(this).add(new JsonRequest<JSONArray>(Request.Method.POST, CATEGORIES_URL, obj.toString(),
                    new Response.Listener<JSONArray>() {
                        @Override
                        public void onResponse(JSONArray response) {
                            loading.dismiss();
                            showList(response);
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    loading.dismiss();
                    Toast.makeText(getApplicationContext(),
                            "Ooops!,Internet Connection Problem", Toast.LENGTH_LONG).show();

                }
            }) {
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String, String> params = new HashMap<String, String>();
                    params.put(KEY_ID,id);
                    return super.getParams();
                }

                @Override
                protected Response<JSONArray> parseNetworkResponse(
                        NetworkResponse response) {
                    try {
                        String jsonString = new String(response.data,
                                HttpHeaderParser
                                        .parseCharset(response.headers));
                        return Response.success(new JSONArray(jsonString),
                                HttpHeaderParser
                                        .parseCacheHeaders(response));
                    } catch (UnsupportedEncodingException e) {
                        return Response.error(new ParseError(e));
                    } catch (JSONException je) {
                        return Response.error(new ParseError(je));
                    }
                }
            });

 //   RequestQueue requestQueue = Volley.newRequestQueue(this);
   // requestQueue.add(jsonArrayRequest);


    Toast.makeText(Categories.this,id,Toast.LENGTH_LONG ).show();


}

在服务器端我使用$id=$_POST['id']; 但它显示为null 我不知道问题是什么

我的PHP脚本:

  <?php
require_once('dbConnect.php');
$json = file_get_contents('php://input');
$stripe_json= json_decode($json, TRUE);
$ida=$stripe_json->id;

$sql= "select title,description,image,price,cid FROM products a where a.cid='".$ida."'";
$res=mysqli_query($con,$sql);
$result = array();

while ($row=mysqli_fetch_array($res)){
        array_push($result,array('title'=>$row['0'],
        'description'=>$row['1'],
        'image'=>$row['2'],
        'price'=>$row['3'],

    ));
    }
    echo json_encode(($result));

    mysqli_close($con);

?>

2 个答案:

答案 0 :(得分:0)

可能你可以使用HTTPPost。可能是因为cookie的东西,只需要添加这个:

    CookieManager cookieManager = new CookieManager();
    cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
    httpPost.setCookieHandler(cookieManager);

希望它有所帮助!

答案 1 :(得分:0)

经过这么多的努力和指导,我的php现在正在运作

<强>码

    <?php
require_once('dbConnect.php');
$json = file_get_contents('php://input');
$stripe_json= json_decode($json);
$ida=$stripe_json->id;

$sql= "select title,description,image,price,cid FROM products a where a.cid='".$ida."'";
$res=mysqli_query($con,$sql);
$result = array();

while ($row=mysqli_fetch_array($res)){
        array_push($result,array('title'=>$row['0'],
        'description'=>$row['1'],
        'image'=>$row['2'],
        'price'=>$row['3'],

    ));
    }
    echo json_encode(($result));

    mysqli_close($con);

?>