【问题标题】:volley returns null in onresponsevolley 在响应中返回 null
【发布时间】:2017-10-25 19:00:30
【问题描述】:

我使用 asp .net 创建了这个 api,但是当我调用它来获取 firstname 和 lastname 的值时,它返回 null

[
    {
        "id": 1,
        "firstName": "Male",
        "lastName": "Hastings",
        "gender": "Male",
        "salary": 60000
    },
    {
        "id": 2,
        "firstName": "Male",
        "lastName": "Hastings",
        "gender": "Male",
        "salary": 60000
    },
    {
        "id": 3,
        "firstName": "Ben",
        "lastName": "Hoskins",
        "gender": "Male",
        "salary": 70000
    }
]

这是我调用 api 的 android 代码,我只需要获取没有 ID 和性别的名字和姓氏的值,看起来它读取了 api 但它无法解析数据

 if (isConnectingToInternet(getApplicationContext())) {
                JsonArrayRequest itemrequest = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSON

Array response) {
                    Log.d(TAG, response.toString());
                    hidePDialog();

                    //SharedPreferences pref = getApplicationContext().getSharedPreferences("mypref",0);
                    // SharedPreferences.Editor editor = pref.edit();

                    for (int i = 0; i < response.length(); i++) {
                        try {
                            JSONObject obj = response.getJSONObject(i);
                            Items items = new Items();
                            items.setName(obj.optString("FirstName"));
                            items.setDescription(obj.optString("LastName"));
                            itemsList.add(items);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    adapter.notifyDataSetChanged();
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());
                    hidePDialog();
                }
            }) {
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String, String> params = new HashMap<String, String>();
                    return params;
                }

                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    Map<String, String> headerMap = new HashMap<String, String>();
                    String credentials = "" + ":" + "";
                    String encodedCredentials = Base64.encodeToString(credentials.getBytes(), Base64.URL_SAFE | Base64.NO_WRAP);
                    headerMap.put("Authorization", "Basic " + encodedCredentials);
                    try {
                        headerMap.putAll(super.getHeaders());
                    } catch (AuthFailureError authFailureError) {
                        authFailureError.printStackTrace();
                    }
                    return headerMap;
                }
            };
             //requestQueue.add(itemrequest);
            AppController.getInstance().addToRequestQueue(itemrequest);
        }
    }

【问题讨论】:

标签: android json asp.net-web-api


【解决方案1】:

解析器区分大小写,它适用于键值对,其中键区分大小写

从以下几行更改

items.setName(obj.optString("FirstName"));
items.setDescription(obj.optString("LastName"));

items.setName(obj.optString("firstName"));
items.setDescription(obj.optString("lastName"));

【讨论】:

  • loool,我不敢相信。我怎么会错过这个。谢谢
【解决方案2】:

您确定您的请求数据正确且为 JSONObject 格式吗?

如果你确定,用断点检查这些: 1-您的请求数据 2-你的标题 我认为您的请求格式不正确。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-01
    相关资源
    最近更新 更多