@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);
?>
You have to use
CustomJSONRequest
as 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.And you will call the
Custom Request
asOriginal Post: https://stackoverflow.com/a/19945676/3027124
Hope this helps.
UPDATE
Hope this helps