如何使用volley库将参数传递给Web服务并获取json数组响应?

时间:2017-02-14 06:51:54

标签: android json android-volley

在我的应用程序中,我使用volley库来获取json数据。 下面是我将参数传递给Web服务的代码:

private void getProductName(final String id) {

    pDialog.setMessage("Please wait..");
    pDialog.setTitle("Loading");
    showDialog();

    JsonArrayRequest req = new JsonArrayRequest(Constants.DARSHAN_URL+Constants.PRODUCT_NAME,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());

                    try {
                        // Parsing json array response
                        // loop through each json object
                        for (int i = 0; i < response.length(); i++) {

                            JSONObject person = (JSONObject) response.get(i);

                            String material_id = person.getString("material_id");
                            String material_desc = person.getString("material_desc");

                            HashMap<String, String> maim_category_array = new HashMap<>();

                            maim_category_array.put("material_id", material_id);
                            maim_category_array.put("material_desc", material_desc);

                            productName.add(maim_category_array);

                        }

                        adapter = new GridviewAdapter(ProductNameActivity.this, productName);
                        grid.setAdapter(adapter);
                        grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {

                                HashMap<String, String> map = (HashMap<String, String>)arg0.getItemAtPosition(position);

                                Toast.makeText(ProductNameActivity.this,  map.get("material_id"), Toast.LENGTH_LONG).show();
                                Intent i = new Intent(ProductNameActivity.this, SubProductActivity.class);
                                i.putExtra("BREADCRUMB1",  map.get("material_desc"));
                                i.putExtra("MATERIAL_ID",  map.get("material_id"));
                                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                startActivity(i);
                            }
                        });

                        // txtResponse.setText(jsonResponse);

                    } catch (JSONException e) {
                        e.printStackTrace();
                        Toast.makeText(getApplicationContext(),
                                "Error: " + e.getMessage(),
                                Toast.LENGTH_LONG).show();
                    }

                    hideDialog();
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            Toast.makeText(getApplicationContext(),
                    error.getMessage(), Toast.LENGTH_SHORT).show();
            hideDialog();
        }
    }) {

        @Override
        protected Map<String, String> getParams() {
            // Posting parameters to login url
            Map<String, String> params = new HashMap<String, String>();
            params.put("material_id", id);

            return params;
        }

    };

    // Adding request to request queue
    DarshanApplication.getInstance().addToRequestQueue(req);
}

我将material_id传递给url。但是以 com.android.volley.servererror

获得响应

如何解决问题? 请帮助!!

1 个答案:

答案 0 :(得分:0)

我不知道你为什么要使用截击,但还有另一种方法。我使用java和android方法构建来获取JSON并显示它们。 你可以看看这里

»JsonFetch