volley在onresponse中返回null

时间:2017-10-25 10:07:27

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

我使用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
    }
]

这是我的android代码,我调用api,我只需要获取没有ID和Gender的firstname和lastname的值,它看起来像是读取了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);
        }
    }

2 个答案:

答案 0 :(得分:0)

您确定您的请求数据是否正确并且是否为JSONObject形式?

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

答案 1 :(得分:0)

解析器区分大小写,它与键值对一起使用,其中键区分大小写

更改以下行
//Match gs to searched w
MATCH (w1:W {name: "****"})-[:CONTAINS]->(gs:G)
WITH w1, COLLECT(DISTINCT gs) AS gsCol, SIZE((w1)-[:CONTAINS]-()) AS gCount
OPTIONAL MATCH (w1)-[:CONTAINS]-()-[:SIMILAR*0..1]->(gs:G)
WITH w1, gsCol, gCount, COLLECT(DISTINCT gs) AS similarGs
//Match all ws that contain gs in searched w or where similar as wsCol
OPTIONAL MATCH (w1)-[c2a:CONTAINS]->(g4:G)-[c2b:CONTAINS|:SIMILAR*0..1]-(ws:W)
WHERE c2a.amount - 10 < last(c2b).amount < c2a.amount + 10
WITH w1, gsCol, similarGs, gCount, COLLECT(DISTINCT ws) AS ws2, COLLECT(DISTINCT ws) AS ws3, COLLECT(DISTINCT ws) AS ws4
//Match ws from wsCol where all gs in new matched ws are same
UNWIND ws2 as w2
OPTIONAL MATCH (w2)-[c3:CONTAINS]->(g3:G)
WITH w1, w2, ws3, ws4, gsCol, similarGs, gCount, COLLECT(g3) AS gs3, SIZE((w2)-[:CONTAINS]->()) as gCount3, SUM(c3.amount) AS c3amount
WHERE ALL(x in gs3 WHERE x IN gsCol)
WITH w1, w2, ws3, ws4, gsCol, similarGs, gCount, gCount3, c3amount
WHERE gCount3 = gCount AND c3amount = 100
WITH COLLECT(w2) ELSE ['none'] END AS ws2Col, w1, ws3, ws4, gsCol, similarGs, gCount
//Match ws with gs that are in searched or similar to searched w
UNWIND ws3 as w3
WITH w1, w3, ws4, gsCol, similarGs, gCount, ws2Col
OPTIONAL MATCH (w3)-[c4:CONTAINS]->(g4:G)
WITH w1, w3, ws4, ws2Col, gsCol, similarGs, gCount, COLLECT(g4) AS gs4, SIZE((w3)-[:CONTAINS]->()) AS gCount4, SUM(c4.amount) AS c4amount
WHERE ALL(x in gs4 WHERE x in similarGs)
WITH w1, w3, ws4, ws2Col, gsCol, similarGs, gCount, gs4, gCount4, c4amount
WHERE gCount4 = gCount AND c4amount = 100 AND NOT(w3 IS NULL OR w3 IN ws2Col)
WITH CASE WHEN NOT(w3 IS NULL) THEN COLLECT(w3) ELSE ['none'] END AS ws3Col, w1, w3, ws4, ws2Col, gsCol, gCount, similarGs
//Match ws where depending on number of gs in w 1 or 2+ gs match searched w
UNWIND ws4 AS w4
OPTIONAL MATCH (w4)-[c5b:CONTAINS]->(g5:G)
WITH w1, w4, ws2Col, ws3Col, gsCol, similarGs, gCount, sum(c5b.amount) AS c6amount, SIZE((w4)-[:CONTAINS]-()) as gCount5, collect(g5) AS gs5, max(c5b.amount) as c6max, ws2Col + ws3Col AS wsC
WHERE ALL(x IN gs5 WHERE x IN gsCol) AND (CASE WHEN gCount > 2 THEN c6amount > 25 ELSE c6amount > 65 END) AND NOT(w4 in ws2Col OR w4 in ws3Col)
WITH CASE WHEN w4 IS NULL THEN ['none'] ELSE COLLECT(w4) END AS ws4Col, w1, ws2Col, ws3Col, w4, gsCol, similarGs, gCount, c6amount, gCount5, gs5, c6max
// Return results
UNWIND (CASE ws2Col WHEN [] THEN [null] ELSE ws2Col END) AS ws2a
UNWIND (CASE ws3Col WHEN [] THEN [null] ELSE ws3Col END) AS ws3a
UNWIND (CASE ws4Col WHEN [] THEN [null] ELSE ws4Col END) AS ws4a
RETURN collect(distinct ws2a) AS match1, collect(distinct ws3a) AS match2, collect(distinct ws4a) AS match3

items.setName(obj.optString("FirstName"));
items.setDescription(obj.optString("LastName"));
相关问题