gpt4 book ai didi

java - Android - 使用 Volley 显示自定义 ListView (使用 http 请求发送 header )

转载 作者:行者123 更新时间:2023-11-30 02:45:21 24 4
gpt4 key购买 nike

我对 android 开发有点陌生,我正在尝试使用 Volley 库创建一个自定义 ListView ,其中包含图像列表和每个图像的文本描述。我需要知道的是如何使用 header 发出 HTTP 请求,以便服务器让我获取数据。我的代码如下所示:主 Activity .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;
}
};

关于java - Android - 使用 Volley 显示自定义 ListView (使用 http 请求发送 header ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25134132/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com