0

我正在使用应用程序引擎使用 Google Cloud Messaging 将希伯来语推送通知发送到智能手机,但是当我在电话上收到消息时,文本是 ??????? 而不是希伯来语。

这里是使用 JSON 对象准备将消息发送到电话的代码:

                JSONObject obj=new JSONObject();
                JSONArray registrationsIDList = new JSONArray();

                Query newsQuery = new Query("Devices");
                List<Entity> results = datastore.prepare(newsQuery).asList(
                        FetchOptions.Builder.withDefaults());
                if(results!=null)
                {
                    for(int j=0;j<results.size();j++)
                    {
                        Entity entity=results.get(j);
                        if(entity!=null)
                        {
                            String ID=(String) entity.getProperty("Registration ID");
                            if(ID!=null)
                            {
                                registrationsIDList.put(ID);
                            }
                        }
                    }
                }

                LinkedHashMap data = new LinkedHashMap();
                data.put("Subject",subjects.get(i).getSubject());
                data.put("Writer", subjects.get(i).getWriter());
                data.put("SubjectDate", subjects.get(i).getLastCommentDateTime());
                data.put("SubjectLink", subjects.get(i).getSubjectLink());

                try {
                    obj.put("registration_ids", registrationsIDList);
                    obj.put("data", data);
                } catch (JSONException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }



                //Sending message to GCM for devices


                try 
                {
                    URL url = new URL("https://android.googleapis.com/gcm/send");
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setDoOutput(true);
                    connection.setRequestMethod("POST");
                    connection.setRequestProperty("Content-Type", "application/json ; charset=utf-8");
                    connection.setRequestProperty("Authorization", "key=*************************************");

                    OutputStream writer = connection.getOutputStream();
                    Entity tmp=new Entity("JSON");
                    tmp.setProperty("Message", obj.toString());
                    datastore.put(tmp);
                    writer.write(obj.toString().getBytes());
                    writer.close();

                    Entity device=new Entity("Messages Results");
                    device.setProperty("Response Code", connection.getResponseCode());
                    device.setProperty("Response Message", connection.getResponseMessage());
                    datastore.put(device);
                    if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {

                    } else {
                        // Server returned HTTP error code.
                    }
                } catch (MalformedURLException ex) {
                    // ...
                } catch (IOException ex) {
                    // ...
                }

在发送消息之前,我检查了 JSON 对象是否包含正确的希伯来语文本。

这里是在手机上接收消息的代码:

@Override
protected void onMessage(Context arg0, Intent arg1) {
    Log.d("GCM", "RECIEVED A MESSAGE");
    String str = arg1.getExtras().getString("Subject");
    Log.d("GCM", str);
    String str1 =arg1.getExtras().getString("Writer");
    Log.d("GCM", str1);
    String str2 =arg1.getExtras().getString("SubjectDate");
    Log.d("GCM", str2);
    String str3 =arg1.getExtras().getString("SubjectLink");
    Log.d("GCM", str3);
}

现在的文字是??????????????????? 而不是希伯来语。任何建议将被认真考虑。

4

1 回答 1

3

您需要使用 Unicode:.toString.getBytes("UTF8")

于 2012-07-06T18:31:02.363 回答