[英]Sending JSON as HTTP POST parameter (Android/Java)
I'm trying to send a JSON object which contains a JSON array with JSON objects inside via HTTP POST parameters. 我正在尝试通过HTTP POST参数发送一个JSON对象,其中包含一个带有JSON对象的JSON数组。
The format of the parameter (what the server expects) is something like: 参数的格式(服务器期望的)是这样的:
{""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\\\"}\
"]"}
The way I build that JSON object is the following: 我构建该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();
}
In the last but one line ( jsonRoot.put("team", teamArray);
) it has the same format as what gets sent in the last line, but with one less \\
, so one less times "parsed" apparently. 在最后一行(
jsonRoot.put("team", teamArray);
)它具有与在最后一行中发送的格式相同的格式,但是少了一个\\
,因此显然少了一次“解析”。
Part of my HTTP code: 我的部分HTTP代码:
String postBody = json.toString();
Log.d("HTTPHelper", "posting JSON: " + postBody);
((HttpPost) httpRequest).setEntity(new StringEntity(postBody));
Why is this happening? 为什么会这样? Is it Java?
是Java吗? Any ideas how could I build the correct JSON?
任何想法我如何构建正确的JSON? or any work around?
或任何解决方法?
Thanks a lot in advance! 非常感谢提前!
I gave this up, and decided to go the dirty-way: replacing chars manually the following way: 我放弃了,并决定采用肮脏的方式:通过以下方式手动替换字符:
json = new JSONObject(nameValuePairs.toString().replace("\"", "'"));
json = new JSONObject(json.toString().replace("\"", ""));
It's ugly, probably dangerous or risky, but it works... 这很丑陋,可能很危险或有风险,但它有效......
Too much code for this task, checkout this library https://github.com/kodart/Httpzoid It uses GSON internally and provides API that works with objects. 此任务的代码太多,请检查此库https://github.com/kodart/Httpzoid它在内部使用GSON并提供可与对象一起使用的API。 All JSON details are hidden.
隐藏所有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();
just a tip... i'm not really into android, just java.. but you could de- and encode your json with Base64 on both sides, and pass the encoded "String". 只是一个提示...我不是真正进入android,只是java ..但你可以在两侧用Base64对你的json进行编码和编码,然后传递编码的“String”。
so you don't have to worry about any "dirty-way" replacing. 所以你不必担心任何“肮脏的方式”取代。
hope this helps u or others, too. 希望这也有助于你或其他人。 :)
:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.