0
    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View view = inflater.inflate(R.layout.fragment_product, container, false);
            recyclerView = (RecyclerView) view.findViewById(R.id.productRecyclerView);
            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            pAdapter = new ProductAdapter(getActivity());
            recyclerView.setAdapter(pAdapter);
            getJsonRequest();
            return view;
        }   

 private void getJsonRequest() {

            /* JSONObject jsonBody = new JSONObject();//this didn't work
            try {
                jsonBody.put("matGroup", "FG0001");
             } catch (JSONException e) {
                //Do nothing.
              }*/

           /* Uri.Builder b = Uri.parse(AppConfig.URL_JSON_PRODUCT_NEW).buildUpon(); //this didn't work either
            b.appendQueryParameter("matGroup", "FG0001");
            String url = b.build().toString();*/

when i am using StringRequest the String is parse to the pHp.I can't use StringRequest because i want to get a jsonObject

            JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, AppConfig.URL_JSON_PRODUCT, null, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    productList = parseJsonResponse(response);
                    pAdapter.setProductList(productList);
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            }) {
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    HashMap<String, String> hashMap = new HashMap<String, String>();
                    hashMap.put("matGroup", "FG0001");
                    return hashMap;
                }
            };
            requestQueue.add(request);
        }

this is my php file. the string that parsing from the getParams should assign to the $matGroup

<?php
require_once 'include/Config.php';
//header('Content-Type: application/json');

$con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die("connection failed");
    mysql_select_db(DB_DATABASE,$con) or die("db selection failed");


//if(isset($_POST['matGroup']))

    $matGroup = $_GET['matGroup'];

    $r=mysql_query("select tbl_inventory.*,tbl_mas_material.des,tbl_mas_material.matgrp from tbl_inventory INNER JOIN tbl_mas_material on tbl_inventory.material=tbl_mas_material.material where tbl_inventory.qty>'0' and matgrp = '$matGroup'");

    $result = array();

        while($row=mysql_fetch_array($r)){
        array_push($result,
        array('material'=>$row[0],
       'plant'=>$row[1],'qty'=>$row[3],'ton'=>$row[4],'val'=>$row[5],'des'=>$row[6],'matgrp'=>$row[7]));}

        echo json_encode(array("feed"=>$result));


    mysql_close($con);


?>

1 Answers1

2

You have to use CustomJSONRequestas the getParams() doesn't invoke because JsonObjectRequest extended JsonRequest which invoke getBody() directly to encoding the constructor second parameter(call requestBody) as contentType, that's why it ignore your getParam() method.

public class CustomRequest extends Request<JSONObject> {

    private Listener<JSONObject> listener;
    private Map<String, String> params;

    public CustomRequest(String url, Map<String, String> params,
            Listener<JSONObject> reponseListener, ErrorListener errorListener) {
        super(Method.GET, url, errorListener);
        this.listener = reponseListener;
        this.params = params;
    }

    public CustomRequest(int method, String url, Map<String, String> params,
            Listener<JSONObject> reponseListener, ErrorListener errorListener) {
        super(method, url, errorListener);
        this.listener = reponseListener;
        this.params = params;
    }

    protected Map<String, String> getParams()
            throws com.android.volley.AuthFailureError {
        return params;
    };

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

    @Override
    protected void deliverResponse(JSONObject response) {
        // TODO Auto-generated method stub
        listener.onResponse(response);
    }
}

And you will call the Custom Request as

RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
CustomRequest jsObjRequest = new CustomRequest(Method.POST, url, params, this.createRequestSuccessListener(), this.createRequestErrorListener());

requestQueue.add(jsObjRequest);

Original Post: https://stackoverflow.com/a/19945676/3027124

Hope this helps.

UPDATE

StringRequest request = new StringRequest(Request.Method.POST,
                AppUrls.URL_SAVE_DATA, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {
                try {
                    JSONObject json = new JSONObject(response);
                    String status = "";
                    if(json != null && json.has(JSONParsor.STATUS))
                        status = json.getString(JSONParsor.STATUS);
                    if(status != null && status.length() > 0 && status.equalsIgnoreCase("success")) {                    }
                } catch (JSONException e) {
                    Log.d("JSON Exception", e.toString());
                }
            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if(error == null) {

                }
            }
        })

        {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put(KEY_PICTURE_NAME, picture.getFileName());
                params.put(KEY_SECTION_ID, picture.getSectionId() + "");
                return params;

            }

        };

        request.setTag(SAVE_DATA);
        addToRequestQueue(request);

Hope this helps

Community
  • 1
  • 1
Mustansar Saeed
  • 2,730
  • 2
  • 22
  • 46
  • I saw this before and i tried but what are these "params, this.createRequestSuccessListener(), this.createRequestErrorListener()" here. I don't know how to implement those. If you can please provide me the full code for calling the Custom Request. Thank you –  Feb 15 '16 at 07:04
  • `params` is the hasmap of your paramters which you were trying to pass through getParam() and requestSuccessListener is your ResponseListener – Mustansar Saeed Feb 15 '16 at 07:44
  • I can't put @override method "getParam()" hashmap "params" to the parameter list in CustomRequest –  Feb 15 '16 at 10:01
  • CustomJsonObjectRequest request = new CustomJsonObjectRequest(Request.Method.POST, AppConfig.URL_JSON_PRODUCT, hashMap, new Response.Listener() –  Feb 15 '16 at 11:06
  • remove Override annotatoin and then try – Mustansar Saeed Feb 15 '16 at 11:09
  • dear friend still not working... this is after edited.. please check whether my php file is ok... http://stackoverflow.com/questions/35407836/getparams-with-customrequest –  Feb 15 '16 at 11:17
  • I did it in another way, but thank you for your responses. It helped me very much. :) –  Feb 16 '16 at 04:26