将JSON作为HTTP POST参数发送(Android / Java)

3
我将尝试通过HTTP POST参数发送包含JSON对象和JSON对象内部的JSON数组的JSON对象。
参数的格式(服务器期望的内容)大致如下:
{""team"":[
    {""teamid"":""179228"",""position"":1},
    {""teamid"":""218036"",""position"":2},
    {""teamid"":""88109"",""position"":3},
    {""teamid"":""88111"",""position"":4},
    {""teamid"":""165536"",""position"":5},
    {""teamid"":""224645"",""position"":6}
]}

然而,发送的内容是:

nevertheless, what gets sent is:

{"team":"[
\"{\\\"position\\\":0,\\\"teamid\\\":\\\"88107\\\"}\",\"{\\\"position\\\":1,\\\"teamid\\\":\\\"88109\\\"}\",\"{\\\"position\\\":2,\\\"teamid\\\":\\\"156714\\\"}\",\"{\\\"position\\\":3,\\\"teamid\\\":\\\"138877\\\"}\",\"{\\\"position\\\":4,\\\"teamid\\\":\\\"168730\\\"}\",\"{\\\"position\\\":5,\\\"teamid\\\":\\\"88110\\\"}\",\"{\\\"position\\\":6,\\\"teamid\\\":\\\"88111\\\"}\",\"{\\\"position\\\":7,\\\"teamid\\\":\\\"134431\\\"}\",\"{\\\"position\\\":8,\\\"teamid\\\":\\\"88112\\\"}\",\"{\\\"position\\\":9,\\\"teamid\\\":\\\"138507\\\"}\",\"{\\\"position\\\":10,\\\"teamid\\\":\\\"138880\\\"}\",\"{\\\"position\\\":11,\\\"teamid\\\":\\\"138881\\\"}\",\"{\\\"position\\\":12,\\\"teamid\\\":\\\"151465\\\"}\",\"{\\\"position\\\":13,\\\"teamid\\\":\\\"151464\\\"}\
"]"}

我构建JSON对象的方式如下:
            JSONArray teamArray = new JSONArray();
            JSONObject jsonRoot = new JSONObject();
            for (int i = 0; i < mTeams.size(); i++) {
                String teamId = null;
                BaseModel data = mTeams.get(i);
                if (data != null && data instanceof TeamModel) {
                    teamId = ((TeamModel) data).getId();
                }
                JSONObject teamObject = new JSONObject();
                try {
                    teamObject.put(
                            getResources().getString(
                                    R.string.sendResortedTeamsPosition), i);
                    teamObject.put(
                            getResources().getString(
                                    R.string.sendResortedTeamsTeamId), teamId);
                    teamArray.put(teamObject);
                } catch (NotFoundException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            try {
                jsonRoot.put("team", teamArray);
                mNameValuePairs.put("teams", jsonRoot);
                    } catch (JSONException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }

在倒数第二行(jsonRoot.put("team", teamArray);)的格式与最后一行发送的格式相同,但是少了一个\,因此明显少解析了一次。
我的HTTP代码的一部分:
String postBody = json.toString();
Log.d("HTTPHelper", "posting JSON: " + postBody);
((HttpPost) httpRequest).setEntity(new StringEntity(postBody));

为什么会出现这种情况?是Java的原因吗?有什么方法可以正确构建JSON吗?或者有其他解决办法吗?
非常感谢您提前的帮助!

1
我们能看到代码的结尾吗?另外,你的strings.xml文件中的数据怎么样了?(不应添加任何\或"字符)。此外,也许您在期望的JSON服务器端中有一些拼写错误:双引号不正确 :) - mithrop
好的,你能否还回答一下关于strings.xml文件中数据的问题? - mithrop
抱歉!在 strings.xml 中没有反斜杠。 - noloman
2
你确定问题不在你的HTTP代码中吗?你能把它也展示给我们看吗?也许你有一个“内容类型”问题? - Ken Y-N
你是如何传递URL和其他参数的? - Harish
显示剩余3条评论
3个回答

1

我放弃了这个方法,决定采用“肮脏”的方式:手动替换字符,具体方法如下:

json = new JSONObject(nameValuePairs.toString().replace("\"", "'"));
json = new JSONObject(json.toString().replace("\"", ""));

虽然不美观,可能存在危险或风险,但它能够正常工作...


1

这个任务需要的代码太多了,看看这个库https://github.com/kodart/Httpzoid吧。它在内部使用GSON并提供与对象一起使用的API。所有JSON细节都被隐藏了。

Http http = HttpFactory.create(context);
http.get("http://example.com/users")
    .handler(new ResponseHandler<User[]>() {
        @Override
        public void success(User[] users, HttpResponse response) {
        }
    }).execute();

0

小建议...我并不是很懂安卓,只了解Java... 但你可以在双方使用Base64进行JSON的编码和解码,并传递已编码的"字符串"。

这样你就不用担心任何"肮脏的办法"替换了。

希望这可以帮到你或其他人。:)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接