Android - 使用Volley显示自定义列表视图(使用http请求发送标头)

时间:2014-08-05 08:19:08

标签: java android listview android-volley

我对Android开发有点新,我正在尝试使用Volley库来创建一个自定义列表视图,其中包含图像列表和每个文本描述。 我需要知道的是如何使用标头发出HTTP请求,以便服务器让我获取数据。 我的代码看起来像这样: MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listView = (ListView) findViewById(R.id.list);
    adapter = new CustomListAdapter(this, restaurantList);
    listView.setAdapter(adapter);

    pDialog = new ProgressDialog(this);
    // Showing progress dialog before making http request
    pDialog.setMessage("Loading...");
    pDialog.show();

    // changing action bar color
    getActionBar().setBackgroundDrawable(
            new ColorDrawable(Color.parseColor("#1b1b1b")));

    // Creating volley request obj
    JsonArrayRequest restaurantReq = new JsonArrayRequest(url,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());
                    hidePDialog();
                    JSONObject obj = null;
                    // Parsing json
                    for (int i = 0; i < response.length(); i++) {
                        try {
                            obj = response.getJSONObject(i);
                            Restaurant restaurant = new Restaurant();
                            restaurant.setID(obj.getString("id"));
                            restaurant.setName(obj.getString("name"));
                            restaurant.setDescription(obj
                                    .getString("description"));
                            restaurant.setType(obj.getString("type"));
                            restaurant.setCategory(obj
                                    .getString("category"));
                            restaurant.setPicture(obj.getString("picture"));
                            restaurant.setLongitude(obj
                                    .getString("longitude"));
                            restaurant.setLatitude(obj
                                    .getString("latitudes"));


                        }

                        catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                    try {
                        Log.e("TAGGGGGGGGG", obj.getString("description"));
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    // notifying list adapter about data changes
                    // so that it renders the list view with updated data
                    adapter.notifyDataSetChanged();
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());
                    hidePDialog();

                }

                public Map<String, String> getHeaders()
                        throws AuthFailureError {
                    HashMap<String, String> headers = new HashMap<String, String>();
                    headers.put("ghfhfhfhfgh", "fghfhfghf");
                    headers.put("fgfghfhf", "fghfhf");
                    headers.put("gfhfghfghfghfgh", "fghfg/fghfghfhfgh");
                    return headers;
                }
            });

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(restaurantReq);
}

1 个答案:

答案 0 :(得分:1)

        }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.e("Error: ", error.toString());
                }
        }) {  // Add this

        @Override           
        public Map<String, String> getHeaders() throws AuthFailureError {
               Map<String, String> headers = new HashMap<String, String>();
               headers.put("xxx", xxx);
               return headers;
            }
       };