【问题标题】:Android Volley with GSON Success but return null valuesGSON成功但返回空值的Android Volley
【发布时间】:2015-06-24 02:30:54
【问题描述】:

我在使用带有 GSON 的 Volley 时遇到问题。没有错误/异常,但 webservice 返回的结果始终为 null。

这是我的网络服务设置:

Status
200 OK Show explanation Loading time: 59
Request headers 
CSP: active
Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36
Content-Type: application/x-www-form-urlencoded 
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8,id;q=0.6
Response headers 
Date: Wed, 24 Jun 2015 02:37:06 GMT 
Server: Apache/2.4.10 (Win32) OpenSSL/1.0.1i PHP/5.5.19 
X-Powered-By: PHP/5.5.19
Content-Length: 81 
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: application/json; charset=utf-8  

我确定 web 服务工作正常,所以问题出在 Java 代码中。

这是我的自定义请求,取自here:

@Override
protected Response<T> parseNetworkResponse(NetworkResponse response) {
    try {
        Log.e("gson", "test");
        String json = new String(
                response.data,
                HttpHeaderParser.parseCharset(response.headers));
        Response result = Response.success(
                gson.fromJson(json, gsonClass),
                HttpHeaderParser.parseCacheHeaders(response));
        return result;
    } catch (UnsupportedEncodingException e) {
        Log.e("gson", e.getLocalizedMessage()); //never printed
        return Response.error(new ParseError(e));
    } catch (JsonSyntaxException e) {
        Log.e("gson", e.getLocalizedMessage()); //never printed
        return Response.error(new ParseError(e));
    }
}

这就是我如何称呼 Volley,onResponse 中的客户为空:

        RequestQueue queue = Volley.newRequestQueue(getActivity());
        String url = "https://sister-8.tafinance.com/cust_gathering/index.php/customer/get/format/json";
        final Map<String,String> header = new HashMap<String, String>();
        //header.put("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
        header.put("name", "test1");
        header.put("born_date", "1970-06-15 00:00:00.000");

        GsonRequest request = new GsonRequest(Request.Method.POST, url, Customer.class, header,
                new Response.Listener<Customer>()
                {
                    @Override
                    public void onResponse(Customer customer) {
                        txtTest.setText(customer.getName());
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        txtTest.setText("" + volleyError.getMessage());
                    }
                }
        );

        queue.add(request);

请帮助我。非常感谢您的帮助。

【问题讨论】:

    标签: java android web-services gson android-volley


    【解决方案1】:

    您使用哪种网络服务器?通常不接受下划线作为 http 标头字段名称。 (出生日期(x),出生日期(o) Why underscores are forbidden in HTTP header names, https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Field_names)

    您还可以更改 Web 服务器的设置以接受 http 标头字段名称的下划线。 (参考http://nginx.org/en/docs/http/ngx_http_core_module.html#underscores_in_headers)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-20
      • 2011-05-30
      • 2016-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多