Web Api上的Android REST请求,JSON响应被分块

时间:2013-03-07 13:33:53

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

我有一个简单的解析器来从Web Api服务获取JSON数据。

public JSONArray getJSONArrayFromUrl(String url) {
        InputStream is = null;
        JSONArray jObj = null;
        String json = "";
        // Making HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);

            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"));
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                //json += line;
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            jObj = new JSONArray(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }

它有效,但我的问题是,当数据大一点时,它们会被分块! 当内容较小时,一切工作正常,但在其他方面我得到例外,如“没有价值”。

我使用Restlet查询REST Web Api服务,异常是

Unterminated array at character 13 of [application/json,UTF-8]

我在IIS中发现了一些关于内容长度的问题,但无法找到或理解任何解决方案,有人可以提供帮助,我绝对认为这不是客户端的问题,但我认为我应该专注于服务器端!

[编辑]

我应该得到的JSON:

[
{
$id: "1",
ExamId: 1,
Title: "Agile Part Two",
ClassId: 1,
Description: "Agile Development 202",
Details: "What is Agile software Development? I explain agile with one dozen words: Iterative planning, honest plans, project heartbeat, working software, team ...",
TotalTime: "03:00:00",
RowVersion: "AAAAAAAAD6M=",
Class: null,
Questions: [
{
$id: "2",
QuestionId: 1,
ExamId: 1,
Question1: "You are a coach facilitating retrospective meeting. What is the main purpose of this meeting that facilitator need to ensure during the meeting?",
TimeAverage: "00:10:00",
RowVersion: "AAAAAAAAD6Q=",
Choices: [
{
$id: "3",
ChoiceId: 1,
QuestionId: 1,
Choice1: "Peer pressure, Fine-grain coordination, Focusing on the few, Daily commitment and Raising impediments.",
IsCorrect: true,
Points: 12,
RowVersion: "AAAAAAAAD64=",
Question: {
$ref: "2"
}
},
{
$id: "4",
ChoiceId: 2,
QuestionId: 1,
Choice1: "Know the work, Get a fresh start, Commit to shared goals and Create focus and abundance.",
IsCorrect: false,
Points: 0,
RowVersion: "AAAAAAAAD7g=",
Question: {
$ref: "2"
}
},
{
$id: "5",
ChoiceId: 3,
QuestionId: 1,
Choice1: "True-up, Show and tell, Get direct feedback, Offer insights and Ask for help.",
IsCorrect: false,
Points: 0,
RowVersion: "AAAAAAAAD7c=",
Question: {
$ref: "2"
}
},
{
$id: "6",
ChoiceId: 5,
QuestionId: 1,
Choice1: "Inspect and adapt. Look back at how, not what Do (even) better next time.",
IsCorrect: false,
Points: 0,
RowVersion: "AAAAAAAAD7Y=",
Question: {
$ref: "2"
}
}
],
Exam: {
$ref: "1"
}
},
{
$id: "7",
QuestionId: 2,
ExamId: 1,
Question1: "How does tools like FIT and Fitness compliment Agile?",
TimeAverage: "00:10:00",
RowVersion: "AAAAAAAAD6Y=",
Choices: [
{
$id: "8",
ChoiceId: 6,
QuestionId: 2,
Choice1: "Automated Acceptance Tests",
IsCorrect: false,
Points: 0,
RowVersion: "AAAAAAAAD7U=",
Question: {
$ref: "7"
}
},
{
$id: "9",
ChoiceId: 7,
QuestionId: 2,
Choice1: "Continuous Integration",
IsCorrect: true,
Points: 15,
RowVersion: "AAAAAAAAD7Q=",
Question: {
$ref: "7"
}
},
{
$id: "10",
ChoiceId: 8,
QuestionId: 2,
Choice1: "Unit Test",
IsCorrect: false,
Points: 0,
RowVersion: "AAAAAAAAD7k=",
Question: {
$ref: "7"
}
},
{
$id: "11",
ChoiceId: 9,
QuestionId: 2,
Choice1: "Code coverage metrics",
IsCorrect: false,
Points: 0,
RowVersion: "AAAAAAAAD7o=",
Question: {
$ref: "7"
}
}
],
Exam: {
$ref: "1"
}
},
{
$id: "12",
QuestionId: 3,
ExamId: 1,
Question1: "What does "Timeboxed" means in Agile terms?",
TimeAverage: "00:05:00",
RowVersion: "AAAAAAAAD6g=",
Choices: [
{
$id: "13",
ChoiceId: 10,
QuestionId: 3,
Choice1: "Fast",
IsCorrect: true,
Points: 13,
RowVersion: "AAAAAAAAD7s=",
Question: {
$ref: "12"
}
},
{
$id: "14",
ChoiceId: 11,
QuestionId: 3,
Choice1: "Flexible",
IsCorrect: false,
Points: 0,
RowVersion: "AAAAAAAAD7w=",
Question: {
$ref: "12"
}
},
{
$id: "15",
ChoiceId: 12,
QuestionId: 3,
Choice1: "Frequent",
IsCorrect: false,
Points: 0,
RowVersion: "AAAAAAAAD70=",
Question: {
$ref: "12"
}
},
{
$id: "16",
ChoiceId: 13,
QuestionId: 3,
Choice1: "Fixed",
IsCorrect: false,
Points: 0,
RowVersion: "AAAAAAAAD74=",
Question: {
$ref: "12"
}
}
],
Exam: {
$ref: "1"
}
},
{
$id: "17",
QuestionId: 4,
ExamId: 1,
Question1: "How are EVM metrics used in Agile?",
TimeAverage: "00:15:00",
RowVersion: "AAAAAAAAD6o=",
Choices: [
{
$id: "18",
ChoiceId: 14,
QuestionId: 4,
Choice1: "They are used to prioritize projects.",
IsCorrect: true,
Points: 22,
RowVersion: "AAAAAAAAD78=",
Question: {
$ref: "17"
}
},
{
$id: "19",
ChoiceId: 15,
QuestionId: 4,
Choice1: "They are used to monitor project performance",
IsCorrect: false,
Points: 0,
RowVersion: "AAAAAAAAD8A=",
Question: {
$ref: "17"
}
},
{
$id: "20",
ChoiceId: 16,
QuestionId: 4,
Choice1: "They are used to analyze risk.",
IsCorrect: false,
Points: 0,
RowVersion: "AAAAAAAAD8E=",
Question: {
$ref: "17"
}
},
{
$id: "21",
ChoiceId: 17,
QuestionId: 4,
Choice1: "They are used to measure coach's performance.",
IsCorrect: false,
Points: 0,
RowVersion: "AAAAAAAAD8I=",
Question: {
$ref: "17"
}
}
],
Exam: {
$ref: "1"
}
},
{
$id: "22",
QuestionId: 5,
ExamId: 1,
Question1: "If a team can complete 10 story points in an iteration then how long will it take for the team to complete 100 story points?",
TimeAverage: "00:12:00",
RowVersion: "AAAAAAAAD6w=",
Choices: [
{
$id: "23",
ChoiceId: 19,
QuestionId: 5,
Choice1: "10 Iterations",
IsCorrect: true,
Points: 11,
RowVersion: "AAAAAAAAD8Q=",
Question: {
$ref: "22"
}
},
{
$id: "24",
ChoiceId: 20,
QuestionId: 5,
Choice1: "10 waves",
IsCorrect: false,
Points: 0,
RowVersion: "AAAAAAAAD8U=",
Question: {
$ref: "22"
}
},
{
$id: "25",
ChoiceId: 21,
QuestionId: 5,
Choice1: "20 Iterations",
IsCorrect: false,
Points: 0,
RowVersion: "AAAAAAAAD8Y=",
Question: {
$ref: "22"
}
},
{
$id: "26",
ChoiceId: 23,
QuestionId: 5,
Choice1: "20 waves",
IsCorrect: false,
Points: 0,
RowVersion: "AAAAAAAAD8g=",
Question: {
$ref: "22"
}
}
],
Exam: {
$ref: "1"
}
}
],
Schedule: {
$id: "27",
ScheduleId: 1,
DateScheduled: "2013-03-06T00:00:00",
RowVersion: "AAAAAAAAD8k=",
Exam: {
$ref: "1"
}
}
}
]

我得到的JSON:

[{"ClassId":1,"Description":"Agile Development 202","Schedule":{"RowVersion":"AAAAAAAAD8k=","Exam":{"$ref":"1"},"DateScheduled":"2013-03-06T00:00:00","ScheduleId":1,"$id":"27"},"Questions":[{"Exam":{"$ref":"1"},"RowVersion":"AAAAAAAAD6Q=","Choices":[{"Question":{"$ref":"2"},"RowVersion":"AAAAAAAAD64=","QuestionId":1,"IsCorrect":true,"ChoiceId":1,"Points":12,"$id":"3","Choice1":"Peer pressure, Fine-grain coordination, Focusing on the few, Daily commitment and Raising impediments."},{"Question":{"$ref":"2"},"RowVersion":"AAAAAAAAD7g=","QuestionId":1,"IsCorrect":false,"ChoiceId":2,"Points":0,"$id":"4","Choice1":"Know the work, Get a fresh start, Commit to shared goals and Create focus and abundance."},{"Question":{"$ref":"2"},"RowVersion":"AAAAAAAAD7c=","QuestionId":1,"IsCorrect":false,"ChoiceId":3,"Points":0,"$id":"5","Choice1":"True-up, Show and tell, Get direct feedback, Offer insights and Ask for help."},{"Question":{"$ref":"2"},"RowVersion":"AAAAAAAAD7Y=","QuestionId":1,"IsCorrect":false,"ChoiceId":5,"Points":0,"$id":"6","Choice1":"Inspect and adapt. Look back at how, not what\r\nDo (even) better next time."}],"QuestionId":1,"TimeAverage":"00:10:00","ExamId":1,"Question1":"You are a coach facilitating retrospective meeting. What is the main purpose of this meeting that facilitator need to ensure during the meeting?","$id":"2"},{"Exam":{"$ref":"1"},"RowVersion":"AAAAAAAAD6Y=","Choices":[{"Question":{"$ref":"7"},"RowVersion":"AAAAAAAAD7U=","QuestionId":2,"IsCorrect":false,"ChoiceId":6,"Points":0,"$id":"8","Choice1":"Automated Acceptance Tests"},{"Question":{"$ref":"7"},"RowVersion":"AAAAAAAAD7Q=","QuestionId":2,"IsCorrect":true,"ChoiceId":7,"Points":15,"$id":"9","Choice1":"Continuous Integration"},{"Question":{"$ref":"7"},"RowVersion":"AAAAAAAAD7k=","QuestionId":2,"IsCorrect":false,"ChoiceId":8,"Points":0,"$id":"10","Choice1":"Unit Test"},{"Question":{"$ref":"7"},"RowVersion":"AAAAAAAAD7o=","QuestionId":2,"IsCorrect":false,"ChoiceId":9,"Points":0,"$id":"11","Choice1":"Code coverage metrics"}],"QuestionId":2,"TimeAverage":"00:10:00","ExamId":1,"Question1":"How does tools like FIT and Fitness compliment Agile?","$id":"7"},{"Exam":{"$ref":"1"},"RowVersion":"AAAAAAAAD6g=","Choices":[{"Question":{"$ref":"12"},"RowVersion":"AAAAAAAAD7s=","QuestionId":3,"IsCorrect":true,"ChoiceId":10,"Points":13,"$id":"13","Choice1":"Fast"},{"Question":{"$ref":"12"},"RowVersion":"AAAAAAAAD7w=","QuestionId":3,"IsCorrect":false,"ChoiceId":11,"Points":0,"$id":"14","Choice1":"Flexible"},{"Question":{"$ref":"12"},"RowVersion":"AAAAAAAAD70=","QuestionId":3,"IsCorrect":false,"ChoiceId":12,"Points":0,"$id":"15","Choice1":"Frequent"},{"Question":{"$ref":"12"},"RowVersion":"AAAAAAAAD74=","QuestionId":3,"IsCorrect":false,"ChoiceId":13,"Points":0,"$id":"16","Choice1":"Fixed"}],"QuestionId":3,"TimeAverage":"00:05:00","ExamId":1,"Question1":"What does \"Timeboxed\" means in Agile terms?","$id":"12"},{"Exam":{"$ref":"1"},"RowVersion":"AAAAAAAAD6o=","Choices":[{"Question":{"$ref":"17"},"RowVersion":"AAAAAAAAD78=","QuestionId":4,"IsCorrect":true,"ChoiceId":14,"Points":22,"$id":"18","Choice1":"They are used to prioritize projects."},{"Question":{"$ref":"17"},"RowVersion":"AAAAAAAAD8A=","QuestionId":4,"IsCorrect":false,"ChoiceId":15,"Points":0,"$id":"19","Choice1":"They are used to monitor project performance"},{"Question":{"$ref":"17"},"RowVersion":"AAAAAAAAD8E=","QuestionId":4,"IsCorrect":false,"ChoiceId":16,"Points":0,"$id":"20","Choice1":"They are used to analyze risk."},{"Question":{"$ref":"17"},"RowVersion":"AAAAAAAAD8I=","QuestionId":4,"IsCorrect":false,"ChoiceId":17,"Points":0,"$id":"21","Choice1":"They are used to measure coach's performance."}],"QuestionId":4,"TimeAverage":"00:15:00","ExamId":1,"Question1":"How are EVM metrics used in Agile?","$id":"17"},{"Exam":{"$ref":"1"},"RowVersion":"AAAAAAAAD6w=","Choices":[{"Question":{"$ref":"22"},"RowVersion":"AAAAAAAAD8Q=","QuestionId":5,"IsCorrect":true,"ChoiceId":19,"Points":11,"$id":"23","Choice1":"10 Iterations"},{"Question":{"$ref":"22"},"RowVersion":"AAA

提前谢谢。

2 个答案:

答案 0 :(得分:0)

[
    {
        "ClassId": 1,
        "Description": "Agile Development 202",
        "Schedule": {
            "RowVersion": "AAAAAAAAD8k=",
            "Exam": {
                "$ref": "1"
            },
            "DateScheduled": "2013-03-06T00:00:00",
            "ScheduleId": 1,
            "$id": "27"
        },
        "Questions": [
            {
                "Exam": {
                    "$ref": "1"
                },
                "RowVersion": "AAAAAAAAD6Q=",
                "Choices": [
                    {
                        "Question": {
                            "$ref": "2"
                        },
                        "RowVersion": "AAAAAAAAD64=",
                        "QuestionId": 1,
                        "IsCorrect": true,
                        "ChoiceId": 1,
                        "Points": 12,
                        "$id": "3",
                        "Choice1": "Peer pressure, Fine-grain coordination, Focusing on the few, Daily commitment and Raising impediments."
                    },
                    {
                        "Question": {
                            "$ref": "2"
                        },
                        "RowVersion": "AAAAAAAAD7g=",
                        "QuestionId": 1,
                        "IsCorrect": false,
                        "ChoiceId": 2,
                        "Points": 0,
                        "$id": "4",
                        "Choice1": "Know the work, Get a fresh start, Commit to shared goals and Create focus and abundance."
                    },
                    {
                        "Question": {
                            "$ref": "2"
                        },
                        "RowVersion": "AAAAAAAAD7c=",
                        "QuestionId": 1,
                        "IsCorrect": false,
                        "ChoiceId": 3,
                        "Points": 0,
                        "$id": "5",
                        "Choice1": "True-up, Show and tell, Get direct feedback, Offer insights and Ask for help."
                    },
                    {
                        "Question": {
                            "$ref": "2"
                        },
                        "RowVersion": "AAAAAAAAD7Y=",
                        "QuestionId": 1,
                        "IsCorrect": false,
                        "ChoiceId": 5,
                        "Points": 0,
                        "$id": "6",
                        "Choice1": "Inspect and adapt. Look back at how, not what\r\nDo (even) better next time."
                    }
                ],
                "QuestionId": 1,
                "TimeAverage": "00:10:00",
                "ExamId": 1,
                "Question1": "You are a coach facilitating retrospective meeting. What is the main purpose of this meeting that facilitator need to ensure during the meeting?",
                "$id": "2"
            },
            {
                "Exam": {
                    "$ref": "1"
                },
                "RowVersion": "AAAAAAAAD6Y=",
                "Choices": [
                    {
                        "Question": {
                            "$ref": "7"
                        },
                        "RowVersion": "AAAAAAAAD7U=",
                        "QuestionId": 2,
                        "IsCorrect": false,
                        "ChoiceId": 6,
                        "Points": 0,
                        "$id": "8",
                        "Choice1": "Automated Acceptance Tests"
                    },
                    {
                        "Question": {
                            "$ref": "7"
                        },
                        "RowVersion": "AAAAAAAAD7Q=",
                        "QuestionId": 2,
                        "IsCorrect": true,
                        "ChoiceId": 7,
                        "Points": 15,
                        "$id": "9",
                        "Choice1": "Continuous Integration"
                    },
                    {
                        "Question": {
                            "$ref": "7"
                        },
                        "RowVersion": "AAAAAAAAD7k=",
                        "QuestionId": 2,
                        "IsCorrect": false,
                        "ChoiceId": 8,
                        "Points": 0,
                        "$id": "10",
                        "Choice1": "Unit Test"
                    },
                    {
                        "Question": {
                            "$ref": "7"
                        },
                        "RowVersion": "AAAAAAAAD7o=",
                        "QuestionId": 2,
                        "IsCorrect": false,
                        "ChoiceId": 9,
                        "Points": 0,
                        "$id": "11",
                        "Choice1": "Code coverage metrics"
                    }
                ],
                "QuestionId": 2,
                "TimeAverage": "00:10:00",
                "ExamId": 1,
                "Question1": "How does tools like FIT and Fitness compliment Agile?",
                "$id": "7"
            },
            {
                "Exam": {
                    "$ref": "1"
                },
                "RowVersion": "AAAAAAAAD6g=",
                "Choices": [
                    {
                        "Question": {
                            "$ref": "12"
                        },
                        "RowVersion": "AAAAAAAAD7s=",
                        "QuestionId": 3,
                        "IsCorrect": true,
                        "ChoiceId": 10,
                        "Points": 13,
                        "$id": "13",
                        "Choice1": "Fast"
                    },
                    {
                        "Question": {
                            "$ref": "12"
                        },
                        "RowVersion": "AAAAAAAAD7w=",
                        "QuestionId": 3,
                        "IsCorrect": false,
                        "ChoiceId": 11,
                        "Points": 0,
                        "$id": "14",
                        "Choice1": "Flexible"
                    },
                    {
                        "Question": {
                            "$ref": "12"
                        },
                        "RowVersion": "AAAAAAAAD70=",
                        "QuestionId": 3,
                        "IsCorrect": false,
                        "ChoiceId": 12,
                        "Points": 0,
                        "$id": "15",
                        "Choice1": "Frequent"
                    },
                    {
                        "Question": {
                            "$ref": "12"
                        },
                        "RowVersion": "AAAAAAAAD74=",
                        "QuestionId": 3,
                        "IsCorrect": false,
                        "ChoiceId": 13,
                        "Points": 0,
                        "$id": "16",
                        "Choice1": "Fixed"
                    }
                ],
                "QuestionId": 3,
                "TimeAverage": "00:05:00",
                "ExamId": 1,
                "Question1": "What does \"Timeboxed\" means in Agile terms?",
                "$id": "12"
            },
            {
                "Exam": {
                    "$ref": "1"
                },
                "RowVersion": "AAAAAAAAD6o=",
                "Choices": [
                    {
                        "Question": {
                            "$ref": "17"
                        },
                        "RowVersion": "AAAAAAAAD78=",
                        "QuestionId": 4,
                        "IsCorrect": true,
                        "ChoiceId": 14,
                        "Points": 22,
                        "$id": "18",
                        "Choice1": "They are used to prioritize projects."
                    },
                    {
                        "Question": {
                            "$ref": "17"
                        },
                        "RowVersion": "AAAAAAAAD8A=",
                        "QuestionId": 4,
                        "IsCorrect": false,
                        "ChoiceId": 15,
                        "Points": 0,
                        "$id": "19",
                        "Choice1": "They are used to monitor project performance"
                    },
                    {
                        "Question": {
                            "$ref": "17"
                        },
                        "RowVersion": "AAAAAAAAD8E=",
                        "QuestionId": 4,
                        "IsCorrect": false,
                        "ChoiceId": 16,
                        "Points": 0,
                        "$id": "20",
                        "Choice1": "They are used to analyze risk."
                    },
                    {
                        "Question": {
                            "$ref": "17"
                        },
                        "RowVersion": "AAAAAAAAD8I=",
                        "QuestionId": 4,
                        "IsCorrect": false,
                        "ChoiceId": 17,
                        "Points": 0,
                        "$id": "21",
                        "Choice1": "They are used to measure coach's performance."
                    }
                ],
                "QuestionId": 4,
                "TimeAverage": "00:15:00",
                "ExamId": 1,
                "Question1": "How are EVM metrics used in Agile?",
                "$id": "17"
            },
            {
                "Exam": {
                    "$ref": "1"
                },
                "RowVersion": "AAAAAAAAD6w=",
                "Choices": [
                    {
                        "Question": {
                            "$ref": "22"
                        },
                        "RowVersion": "AAAAAAAAD8Q=",
                        "QuestionId": 5,
                        "IsCorrect": true,
                        "ChoiceId": 19,
                        "Points": 11,
                        "$id": "23",
                        "Choice1": "10 Iterations"
                    },
                    {
                        "Question": {
                            "$ref": "22"
                        },
                        "RowVersion": "AAA

因为我可以看到你的json格式不正确而你的数组没有关闭所以你收到错误Unterminated array

答案 1 :(得分:0)

解决了禁用分块编码的问题。

请参阅此answer

中的详细信息