有人可以指导我如何从网址获取标头值。我已经参考了许多教程,但我找不到任何从json获取头部值的教程。任何帮助都将受到高度赞赏。
这是我的代码:
JsonArrayRequest obreq = new JsonArrayRequest(Request.Method.GET, JsonURL,
// The third parameter Listener overrides the method onResponse() and passes
//JSONObject as a parameter
new Response.Listener<JSONArray>() {
// Takes the response from the JSON request
@Override
public void onResponse(JSONArray response) {
pbHeaderProgress.setVisibility(View.GONE);
try {
// Retrieves the string labeled "colorName" and "description" from
//the response JSON Object
//and converts them into javascript objects
for (int i = 0; i < response.length(); i++) {
JSONObject jresponse = response.getJSONObject(i);
String id = jresponse.getString("id");
Id.add(id);
String auth = jresponse.getString("DJ_author_name");
Author.add(auth);
String date = jresponse.getString("date_gmt");
SimpleDateFormat form = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
java.util.Date date4 = null;
try {
date4 = form.parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
Date.add(newDateStr);
JSONObject title = jresponse.getJSONObject("title");
String tit = title.getString("rendered");
Title.add(tit);
JSONObject img = jresponse.getJSONObject("better_featured_image");
String pic = img.getString("source_url");
Image.add(pic);
}
// Adds strings from object to the "data" string
linear.setVisibility(RelativeLayout.VISIBLE);
// Adds the data string to the TextView "results"
adapter.notifyDataSetChanged();
}
// Try and catch are included to handle any errors due to JSON
catch (JSONException e) {
// If an error occurs, this prints the error to the log
e.printStackTrace();
}
}
},
// The final parameter overrides the method onErrorResponse() and passes VolleyError
//as a parameter
new Response.ErrorListener() {
@Override
// Handles errors that occur due to Volley
public void onErrorResponse(VolleyError error) {
pbHeaderProgress.setVisibility(View.GONE);
Toast.makeText(Home.this, "error!!! =)",
Toast.LENGTH_SHORT).show();
noi.setVisibility(RelativeLayout.VISIBLE);
Log.e("Volley", "Error");
}
}
);
obreq.setRetryPolicy(new DefaultRetryPolicy(
30000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
// Adds the JSON object request "obreq" to the request queue
requestQueue.add(obreq);
答案 0 :(得分:0)
请检查djodjos回答here
如果您想要获取自身返回的json的标头值,而不是请求本身的标头,您可以使用Gson库,它的快速可靠且非常易于使用< / p>
-------好吧然后djodjos回答
@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
try {
String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET));
JSONObject jsonResponse = new JSONObject(jsonString);
jsonResponse.put("headers", new JSONObject(response.headers));
return Response.success(jsonResponse,
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException je) {
return Response.error(new ParseError(je));
} }
在创建新的JsonArrayRequest(obreq)时需要覆盖parseNetworkResponse 例如
StringRequest request=new StringRequest(GET,"url",listener,errorListener){
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
String totalPages=responseHeaders.get("X-WP-TotalPages");
return super.parseNetworkResponse(response);
}
};
答案 1 :(得分:0)
您可以继承Request(或其任何子类)并覆盖parseNetworkResponse方法:
@Override
protected Response<Bitmap> parseNetworkResponse(NetworkResponse response)
{
Map<String, String> responseHeaders = response.headers;
}