3

I know how to use Volley GET method for login purposes but what if I need to specify a specific "id"? I am passing the "id" of a list item from a remote database to another activity using intent.putExtra. In the next activity, how would I utilize Volleys' GET method to parse and display data from another table using that passed over "id"?

In a browser I can use the following to get a response with a specified url and parameter:

http://localhost/demoapp/fetch.php?pid=2

But I don't know how to pass that url to a Volley request and display the response in a listview. Something like this:

"http://localhost/demoapp/fetch.php?pid=" + pid

Where "pid" would be the passed over string of the corresponding id.

EDIT:PHP Code

    <?php
include_once("config.php");

$query=mysqli_query($con,"SELECT * FROM comments WHERE pid=".$_GET['pid']);

$array;
while($result=mysqli_fetch_assoc($query)){

$array[]=$result;
}

echo json_encode($array);
?>

EDIT 2: JAVA Code Main Activity

    JsonArrayRequest request = new JsonArrayRequest(url+url_file,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d(TAG, response.toString()); try {
                            for(int i=0;i<response.length();i++){
                                String pid=response.getJSONObject(i).getString("pid");
                                String name=response.getJSONObject(i).getString("product_name");
                                String img;
                                String thumb = response.getJSONObject(i).getString("product_thumb");
                                String detail = response.getJSONObject(i).getString("product_detail");
                                String rating = response.getJSONObject(i).getString("product_rating");

                                img = response.getJSONObject(i).getString("product_pic");

                                rowdata.add(new ProductRowData(pid,name,img,thumb,detail,rating));
                            }
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        adapter=new ProductsAdapter(MainActivity.this, rowdata);
                        list.setAdapter(adapter);
                        dialog.dismiss();
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d(TAG, "Error: " + error.getMessage());
                dialog.dismiss();
            }
        });

        VolleyController.getInstance().addToRequestQueue(request, tag_json_arry);

When list item is clicked, rest of data is passed over to detail activity using intent.putExtra. What I am then trying to do is, upon clicking on FAB, a list of comments from a different table with that products id should be shown.

Steve C.
  • 1,333
  • 3
  • 19
  • 50
  • What problem are you facing with GET method? You seem to be following the right procedure. – Jyotman Singh Mar 07 '16 at 09:18
  • @JyotmanSingh Trying to figure out how to retrieve that json response within an Android app. Please see my next edit. – Steve C. Mar 07 '16 at 09:22
  • 1
    You mentioned you know how to use Volley GET method. Use it in a similar way that you used it for login. Use `JsonObjectRequest` to get the json data and then parse it and show it in your app. – Jyotman Singh Mar 07 '16 at 09:23

2 Answers2

5

If you want to use GET, you could create the request with url parameters:

String uri = String.format("http://localhost/demoapp/fetch.php?pid=%1$s", pid);

StringRequest myReq = new StringRequest(Method.GET,
                                    uri,
                                    createMyReqSuccessListener(),
                                    createMyReqErrorListener());

Or better, you could use POST:

StringRequest stringRequest = new StringRequest(Request.Method.POST, "http://localhost/demoapp/fetch.php", new Response.Listener<String>()
{
    @Override
    public void onResponse(String s)
    {
    }
}, new Response.ErrorListener()
{
    @Override
    public void onErrorResponse(VolleyError volleyError)
    {
    }
})
{
    @Override
    protected Map<String, String> getParams()
    {
        Map<String, String> params = new HashMap<String, String>();
        params.put("id", pid);
        return params;
    }
};

Taken from: Volley - POST/GET parameters

Community
  • 1
  • 1
dan
  • 149
  • 1
  • 12
  • if I went with the POST method, would I have to change to PHP code to `.$_POST['pid']` instead of `.$_GET['pid']`? – Steve C. Mar 07 '16 at 09:13
  • Yes, you have to change your backend. – dan Mar 07 '16 at 09:17
  • If I change the method in my PHP and try to use the same url with the added parameter I get an undefined index on the id – Steve C. Mar 07 '16 at 09:20
  • If you are using POST, you don't need to add the parameters manually. I will edit that. – dan Mar 07 '16 at 09:24
  • I've tried running my app per your answer but am getting an error. I could use your help rather than posting another question with the same issue. Please let me know if you're available. I need to fix this as soon as possible. Thank you. – Steve C. Mar 13 '16 at 03:13
  • I wasn't sure if you were going to reply so I posted a new SO question. Heres my error [link](http://stackoverflow.com/questions/35967871/error-trying-to-post-specified-id-to-server-for-json-response-with-android-vol) – Steve C. Mar 13 '16 at 21:18
0
        @Override
        protected Map<String, String> getParams() {
            HashMap<String,String> params = new HashMap<>();
            params.put("deviseType","1");
            params.put("langId","1");
            params.put("userType","2");
            return params;
        }
Meysam Keshvari
  • 1,141
  • 12
  • 14