JSON GCMメッセージを送受信しようとしていますが、クライアントでは常にUTF8として送信されているようです。
protected HttpURLConnection post(String url, String contentType, String body)
throws IOException {
Print.logInfo("In HttpURLConnection: " + contentType + body);
byte[] bytes = body.getBytes();
HttpURLConnection conn = getConnection(url);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setFixedLengthStreamingMode(bytes.length);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", contentType);
conn.setRequestProperty("Authorization", "key=" + key);
OutputStream out = conn.getOutputStream();
out.write(bytes);
out.close();
return conn;
}
サーバーでは、UTF8メッセージは次のとおりです。HttpURLConnection:application / x-www-form-urlencoded; charset = UTF-8registration_id = APA91 ...&delay_while_idle = 0&collapse_key = Test&time_to_live = 2419200&data.Data1 = Value + 1&data.Data2 = Value + 2&data .Data3 = Value + 3
JSONメッセージは次のとおりです。HttpURLConnection:application / json {"delay_while_idle":false、 "collapse_key": "Test"、 "data":{"Data1": "Value 1"、 "Data2": "Value 2"、 "Data3": "Value 3"}、 "time_to_live":2419200、 "registration_ids":["APA91 ..."]}
マルチキャスト結果付きMulticastResult(multicast_id = 5036 ...、total = 1、success = 1、failure = 0、canonical_ids = 0、results:[[messageId = 0:1362 ..]]:
両方のメッセージがクライアントで正常に受信されましたが、json形式を認識できません。
@Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Received message");
String inAction = intent.getAction();
// check to see if it is a message
if (inAction.equals("com.google.android.c2dm.intent.RECEIVE")) {
// if your key/value is a JSON string, extract and parse it using JSONObject
String json_info = intent.getExtras().getString("data");
if (json_info != null) {
try {
JSONObject jsonObj = new JSONObject(json_info);
payload = jsonObj.get("Data1") + "\n"
+ jsonObj.get("Data2")+ "\n"
+ jsonObj.get("Data3");
}
catch (JSONException e) {
// do nothing
return;
}
}
else {
payload = intent.getStringExtra("Data1") + "\n"
+ intent.getStringExtra("Data2")+ "\n"
+ intent.getStringExtra("Data3");
}
}
}
どちらの場合も、json_infoはnullです。apache/tomcatを実行しているサーバーでJavaサーブレットを使用しています。
助けてください。どうもありがとう