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

将JSON作为HTTP POST参数发送(Android/Java),java,android,json,http-post,Java,Android,Json,Http Post,我试图通过HTTP POST参数发送一个JSON对象,其中包含一个JSON数组,其中包含JSON对象 参数的格式(服务器期望的格式)类似于: {""team"":[ {""teamid"":""179228"",""position"":1}, {""teamid"":""218036"",""position"":2}, {""teamid"":""88109"",""position"":3}, {""teamid"":""88111"",""position

我试图通过HTTP POST参数发送一个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}
]}
然而,发送的是:

{"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();
            }
为什么会这样?是Java吗? 我如何构建正确的JSON?或者有什么工作吗


提前多谢

我放弃了这一点,决定走肮脏的道路:用以下方法手动更换字符:

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

这很难看,可能很危险,但它很有效…

此任务的代码太多,请签出此库 它在内部使用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();
Http=HttpFactory.create(上下文); http.get(“http://example.com/users") .handler(新的ResponseHandler(){ @凌驾 public void成功(用户[]个用户,HttpResponse响应){ } }).execute();
只是一个提示。。。我不是真的喜欢安卓,只是喜欢java。。 但是您可以使用Base64对json进行反编码,并传递编码后的“字符串”

所以你不必担心任何“肮脏的方式”取代


希望这对你或其他人也有帮助。:)

我们能看到代码的结尾吗。另外,strings.xml文件中的数据是如何存储的?(不应添加任何\或“字符”)。此外,可能您在预期的JSON服务器端有一些输入错误:双引号不正确:)@mithrop问题是,在iOS中,这工作得很好,但在Android中,我遇到了很多问题,所以问题不应该是服务器端……顺便说一句,我刚刚添加了代码的最后一部分-只有几个
catch
statementsok太好了。你还能回答strings.xml文件中的数据吗?是的,对不起!在
strings.xml
中没有\你确定你的问题不在你的HTTP代码中吗?你也能告诉我们吗?也许你有
内容类型的问题?
Http http = HttpFactory.create(context);
http.get("http://example.com/users")
    .handler(new ResponseHandler<User[]>() {
        @Override
        public void success(User[] users, HttpResponse response) {
        }
    }).execute();