POST string and response in JSONArray

故事扮演 提交于 2019-12-10 18:22:12

问题


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

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


}

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

MY PHP Script :

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

?>

回答1:


Probably you can use HTTPPost for that. There is probably because of the cookie stuff and just need to add this:

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

hopefully it helps!.




回答2:


After so many efforts and guidance my php is now working

code:

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

?>


来源:https://stackoverflow.com/questions/39951257/post-string-and-response-in-jsonarray

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!