Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从android截击中获取标题值_Android_Json_Header_Android Volley - Fatal编程技术网

从android截击中获取标题值

从android截击中获取标题值,android,json,header,android-volley,Android,Json,Header,Android Volley,有人能告诉我如何从url中获取标题值吗。我已经参考了很多教程,但是我找不到任何教程可以从json获取头值。任何帮助都将不胜感激 这是我的密码: JsonArrayRequest obreq = new JsonArrayRequest(Request.Method.GET, JsonURL, // The third parameter Listener overrides the method onResponse() and passes

有人能告诉我如何从url中获取标题值吗。我已经参考了很多教程,但是我找不到任何教程可以从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);
JsonArrayRequest obreq=新的JsonArrayRequest(Request.Method.GET,JsonURL,
//第三个参数Listener重写onResponse()方法并传递
//JSONObject作为参数
新的Response.Listener(){
//获取JSON请求的响应
@凌驾
公共void onResponse(JSONArray响应){
pbHeaderProgress.setVisibility(View.GONE);
试一试{
//从中检索标记为“colorName”和“description”的字符串
//响应JSON对象
//并将它们转换为javascript对象
对于(int i=0;i
请检查djodjos的答案

如果您的意思是希望获取返回的json的头值,而不是请求本身的头,那么您可以使用这个库,它非常快速、可靠且易于使用

-------好的,那么作为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));
    }    }
@覆盖
受保护的响应parseNetworkResponse(NetworkResponse响应){
试一试{
String jsonString=新字符串(response.data,
HttpHeaderParser.parseCharset(response.headers,PROTOCOL_CHARSET));
JSONObject jsonResponse=新的JSONObject(jsonString);
put(“headers”,新的JSONObject(response.headers));
返回Response.success(jsonResponse,
HttpHeaderParser.parseCacheHeaders(响应));
}捕获(不支持的编码异常e){
返回Response.error(新的ParseError(e));
}捕获(JSONException je){
返回Response.error(newparseerror(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);
        }
    };
StringRequest-request=新的StringRequest(获取“url”、侦听器、errorListener){
@凌驾
受保护的响应parseNetworkResponse(NetworkResponse响应){
字符串totalPages=responseHeaders.get(“X-WP-totalPages”);
返回super.parseNetworkResponse(response);
}
};

请检查djodjos的答案

如果您的意思是希望获取返回的json的头值,而不是请求本身的头,那么您可以使用这个库,它非常快速、可靠且易于使用

-------好的,那么作为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));
    }    }
@覆盖
受保护的响应parseNetworkResponse(NetworkResponse响应){
试一试{
String jsonString=新字符串(response.data,
HttpHeaderParser.parseCharset(response.headers,PROTOCOL_CHARSET));
JSONObject