JSONArray中的POST字符串和响应 [英] POST string and response in JSONArray

查看:142
本文介绍了JSONArray中的POST字符串和响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Actually i follow many stackoverflow answers related my question but none of answer is working and i simply post a string and in return i want a json Array

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

NOTE: when i run my Hard code script it's perfectly well but in the script the POST value shows null Here is my Android code:

 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
i不知道问题是什么

and on the Server Side i use $id=$_POST['id']; but it show null i don't know what's the problem

我的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);

?>


推荐答案

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

After so many efforts and guidance my php is now working

代码:

    <?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);

?>

这篇关于JSONArray中的POST字符串和响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆