Android-Volley:为JsonArrayRequest设置HTTP标头 [英] Android-Volley : set HTTP Header for JsonArrayRequest

查看:315
本文介绍了Android-Volley:为JsonArrayRequest设置HTTP标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我看过几个JsonObjectRequests的例子,其中添加了这个代码

so I've seen a couple of examples for JsonObjectRequests where either this code has been added

        @Override
        public String getBodyContentType() {
            return "application/json";
        }

有时使用此代码

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Content-Type", "application/json");
            return headers;
        }

无论哪种方式,它总是与JsonObjectRequest结合使用。我真的不知道在JsonArrayRequest中添加 @Override 的方法。这就是我的JsonArrayRequest的样子

either way, it has always been used in combination with a JsonObjectRequest. I don't really know where to add the @Override of the methods inside my JsonArrayRequest. This is how my JsonArrayRequest looks like

JsonArrayRequest localJReq = new JsonArrayRequest(localURL,
      new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response){
            try{
                for (int i=0; i < response.length(); i++)
                {
                    JSONObject jsonObject = response.getJSONObject(i);
                    String title = jsonObject.getString("Title");
                    data += "Beschreibung: "+title+"\n\n\n";
                }
                output.setText(data);
            }catch (JSONException e){
                e.printStackTrace();
            }

        }
    },
            new Response.ErrorListener(){
                @Override
                public void onErrorResponse(VolleyError error){
                    error.printStackTrace();
                    Toast.makeText(MainActivity.this, "No more Items Available",
                          Toast.LENGTH_LONG).show();
                }
    });

我试图将其添加到 {} 紧跟在最后一次结束之后,但这不起作用。我尝试将它放在 Response.Listener 中,这也无效。所以我有点困惑。

I've tried to add it in {} just behind the last closing ) but that didn't work. I tried putting it inside the Response.Listener which also didn't work. So I'm a little bit confused.

无论如何我觉得我做JsonArrayRequest真的很奇怪但是因为我的REST API正在接受HTML&作为响应的JSON,Volley似乎得到了HTML响应而不是JSON。这让我在我的应用程序中没有任何项目。这不是很奇怪吗?既然它是一个JsonRequest,那么Request不应该将内容类型设置为application / json吗?我希望通过将content-type设置为application / json,我将获得正确的响应类型。

Anyways I think it is really strange that I am making a JsonArrayRequest but since my REST API is accepting HTML & JSON as a response, Volley seems to get the HTML response instead of the JSON. Which is leaving me with no items inside my App. Isn't that kind of weird ? Since it is a JsonRequest shouldn't the Request set the content-type to application/json by its self? I'm hoping that by setting the content-type to application/json that I will get the correct response type.

推荐答案

您可以在请求结束时调用这些方法并在;

You can call those method at the end of the request and brefore;

    JsonArrayRequest localJReq = new JsonArrayRequest(localURL,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                }
           }) {//here before semicolon ; and use { }.
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            return super.getHeaders();
        }

        @Override
        public String getBodyContentType() {
            return super.getBodyContentType();
        }
    };

这篇关于Android-Volley:为JsonArrayRequest设置HTTP标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆