3

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

?>
Community
  • 1
  • 1
techDigi
  • 251
  • 3
  • 18
  • `i simply post a string`. No. You are not doing that. You post json. – greenapps Oct 10 '16 at 04:48
  • `and in return i want a json Array`. You can want a lot. But are you shure the server will response in json? – greenapps Oct 10 '16 at 04:50
  • `when i run my script it's perfectly well`. Script? Do you mean your java code? And nice it runs perfect. – greenapps Oct 10 '16 at 04:51
  • `but in the script the POST vaue show null`. That is not perfect. But what do you mean exactly? Why are you sending a null value? Dont understand a word. – greenapps Oct 10 '16 at 04:53
  • `i use $id=$_POST['id']; but it show null `. O that is what you mean. Yes that is non existent. You did not sent that value in the normal way. Instead you sent json. Dont use json to send your parameters. – greenapps Oct 10 '16 at 04:56
  • `when i run my script `. You are not running your php script. Your android app posts data to a webserver which will forwatd it to your php script. – greenapps Oct 10 '16 at 04:59
  • yes i am sure my server return Json for eg: [{"id":"1","title":"Soups","icon":"httpsomeurl.jpg"}] – techDigi Oct 10 '16 at 05:01
  • so how can i use the json in php or sql query – techDigi Oct 10 '16 at 05:02
  • Why would you send your id parameter wrapped in json? – greenapps Oct 10 '16 at 05:03
  • i already mention i follow some tutorials but my intention is to send the String so i can further use in my php – techDigi Oct 10 '16 at 05:05
  • Your android code and php code do not match. So conclude for your self. – greenapps Oct 10 '16 at 05:06
  • i know sir that's why i need some help – techDigi Oct 10 '16 at 05:08
  • I already told you what you have to do in my first comment. Send the id parameter in the normal way. Do not wrap it in json. – greenapps Oct 10 '16 at 05:09
  • can i use $id =json_decode($_POST['id']); ??? – techDigi Oct 10 '16 at 05:17
  • You said that $_POST['id'] was null. In fact it does not exist. So how could there be decoded something? – greenapps Oct 10 '16 at 05:24
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/125306/discussion-between-techdigi-and-greenapps). – techDigi Oct 10 '16 at 05:25
  • Google for json_decode(php://input) or so (forgot... Maybe you have to reverse it). – greenapps Oct 10 '16 at 05:26
  • oky Sir............. – techDigi Oct 10 '16 at 05:37
  • i modify my php according to the input json but still not working $stripe_json = json_decode($_POST['id']); $ida=$stripe_json->id; $sql= "select title,description,image,price,cid FROM products a where a.cid='".$ida."'"; – techDigi Oct 10 '16 at 10:42
  • @techDigi I think you should either override getParams() method or post obj in request as per your requirement, if you send jsonObject as requestParam then you should not override getParams() method, just make a jsonObject with id and send it in request...or remove obj.toString() from params and only override getparams() as you have done – Deven Singh Oct 10 '16 at 10:54
  • please check php @greenapps – techDigi Oct 10 '16 at 11:05
  • For some proper debugging: Change your php script to consist of only the following two statements: `$json = file_get_contents('php://input'); echo($json);` Then report what you get as response. Log the text en tell please. – greenapps Oct 10 '16 at 11:12
  • OMG now it's work... @greenapps – techDigi Oct 10 '16 at 11:14
  • Thanq..........@greenapps – techDigi Oct 10 '16 at 11:20

2 Answers2

0

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

  • it's not working is der any way to send the string and in return got a JSONArray response – techDigi Oct 10 '16 at 04:35
  • im not quite sure what you want --> http://stackoverflow.com/questions/15609306/convert-string-to-json-array but hopefully this one helps. – Andrew Indayang Oct 10 '16 at 04:37
  • my motive is to send the id(string) to my server, and use this id in my sql query and return the json encode result(JSONArray) – techDigi Oct 10 '16 at 05:20
0

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

?>
techDigi
  • 251
  • 3
  • 18