【问题标题】:Android - Using Volley to display a custom listview (send headers with the http request)Android - 使用 Volley 显示自定义列表视图(使用 http 请求发送标头)
【发布时间】:2014-09-27 20:08:01
【问题描述】:

我对 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);
}

【问题讨论】:

  • 在 getHeaders() 你已经设置了标题!?也不要发布您的 api 密钥..
  • 抱歉搞错了。我在 Eclipse 中收到一条消息“来自 New response.ErrorListener(){ 类型的方法 getHeaders 从未在本地使用”,并且我没有得到任何数据。

标签: java android listview android-volley


【解决方案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;
            }
       };

【讨论】:

  • E/Volley(10750): [1] 2.onErrorResponse: 错误:
  • new Response.ErrorListener(){ 类型的方法 getHeaders() 必须覆盖或实现超类型方法快速修复:删除 '@override' 注释
  • 你删除了最后一个 ) 吗?
  • 我忘了删除最后一个,现在没事了。现在我没有得到服务器的响应,所以我正在尝试检查发生了什么。
  • 还有一个问题:我的 JSON 数据如下所示,我认为我应该将 JsonArrayRequest restaurantReq 替换为 JsonObjectRequest restaurantReq 对吗?如果是,新代码应该是什么样的? JSON:{数据:[...26]-}
猜你喜欢
  • 2014-03-08
  • 1970-01-01
  • 2014-09-25
  • 1970-01-01
  • 2017-06-20
  • 2017-06-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
相关资源
最近更新 更多