Android アプリケーションで .NET Web Api への HttpPost 要求があります。
オブジェクトをシリアル化するために、json データ型とgsonクラスを使用しています。
シリアル化に関する私のコードは次のとおりです。
HttpPost post = new HttpPost();
post.setURI(new URI("my url goes here"));
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("username", username);
hashMap.put("userCredentials", new Gson().toJson(credentials, UCredentials.class));
// username and credentials are parameters.
post.setHeader("Content-Type", "application/json; charset=utf-8");
post.setEntity(new StringEntity(new Gson().toJson(hashMap)));
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse httpResponse = httpClient.execute(post);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
// If successful.
}
hashMap.put("userCredentials", new Gson().toJson(credentials, UCredentials.class));
コード行を除いて、すべて正常に動作します。しかし、この行で...
Gsonクラスはjsonを次のように提供します:
{
"userCredentials":
"{
\"emailOrUsername\":\"asd@sadas.com\",
\"password\":\"113\"
}",
"username":"sdggfsdgf"
}
有効なjsonパターンではありません。次のようなjson出力が必要です。
{
"userCredentials":
{
"emailOrUsername":"asd@sadas.com",
"password":"113"
},
"username":"sdggfsdgf"
}
これを達成するにはどうすればよいですか?
前もって感謝します。