无法通过android volley从url获取JSONP

时间:2015-09-26 11:31:16

标签: android json

我用于在android中通过volley获取json数据但是今天我遇到了我无法解决的问题。有一个我想使用的api返回一个JSONP而不是一个简单的json。是否有可能在Android中以某种方式检索JSONP数据?

 String url =
                "https://getbible.net/json?passage=1Jn3:16";

        mQueue = Volley.newRequestQueue(this);
        mQueue.add(new JsonObjectRequest(Request.Method.POST, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Log.d("TAGGED",response.toString());
                    }
                },

                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                    }
                }));

2 个答案:

答案 0 :(得分:0)

因为你的API返回了无效的JSON格式

({
    "book": [
        {
            "book_ref": "1 Jhn",
            "book_name": "1 John",
            "book_nr": "62",
            "chapter_nr": "3",
            "chapter": {
                "16": {
                    "verse_nr": "16",
                    "verse": "Hereby perceive we the love of God, because he laid down his life for us: and we ought to lay down our lives for the brethren."
                }
            }
        }
    ],
    "direction": "LTR",
    "type": "verse",
    "version": "kjv"
});

它在开始时和结束时(添加了这个括号);,所以通过纠正它应该可行!

所以您的更正后的回复应该像JSON格式一样

{
    "book": [
        {
            "book_ref": "1 Jhn",
            "book_name": "1 John",
            "book_nr": "62",
            "chapter_nr": "3",
            "chapter": {
                "16": {
                    "verse_nr": "16",
                    "verse": "Hereby perceive we the love of God, because he laid down his life for us: and we ought to lay down our lives for the brethren."
                }
            }
        }
    ],
    "direction": "LTR",
    "type": "verse",
    "version": "kjv"
}

答案 1 :(得分:0)

尝试更改方法Request.Method.GET而不是Request.Method.POST