@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();*/
当我使用 StringRequest 时,String 被解析为 pHp。我不能使用 StringRequest,因为我想获得一个 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);
}
这是我的 php 文件。从 getParams 解析的字符串应该分配给 $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);
?>