简体   繁体   中英

Android - Using Volley to display a custom listview (send headers with the http request)

I am a bit new to android development, and i am trying to use Volley library to create a custom listview that contains list of images and a text description for each. What i need to know is how to make HTTP request with headers so the server let me get data. My code looks like this: 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);
}
        }, 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;
            }
       };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM