JSON编码mirrorapi的重音章程

时间:2013-12-22 18:52:02

标签: json encode google-glass google-mirror-api non-ascii-characters

我从Android应用程序发送带有镜像api的消息,但是当我在玻璃上收到时间线时,我看不到像“è”这样的重音章程

这是字符串:

String text_message = "try this accented char è";

这是JSON的创建:

                String html_message = String.format(
                        "<article>" +
                                "<section>" +
                                "<div class=text-x-small>" +
                                "<p class=blue>%s" +
                                "<p>%s</p>" +
                                "</p></div>" +
                                "</section>" +
                                "<footer>" +
                                "<div>%s</div>" +
                                "</footer>" +
                                "</article>"
                        ,name,text_message,app_name);

                JSONObject notification = new JSONObject();
                notification.put("level", "DEFAULT"); // Play a chime

                JSONArray menuitems = new JSONArray();

                JSONObject reqObj = new JSONObject();
                reqObj.put("action","TOGGLE_PINNED");
                menuitems.put(reqObj);

                reqObj = new JSONObject();
                reqObj.put( "action", "READ_ALOUD" );
                menuitems.put( reqObj );

                reqObj = new JSONObject();
                reqObj.put( "action", "VOICE_CALL" );
                menuitems.put( reqObj );
                reqObj = new JSONObject();
                reqObj.put( "action", "DELETE" );
                menuitems.put( reqObj );

                json = new JSONObject();
                json.put("html", html_message);
                json.put("speakableText",message);
                json.put("menuItems",menuitems);
                json.put("notification", notification);

                MyLog.log("JSON TO SEND: " + json.toString());

                new sendJSON().execute();

这是将JSON发送到镜像api的AsyncTask:

private class sendJSON extends AsyncTask<String, Integer, Double> {

    @Override
    protected Double doInBackground(String... params) {
        try {
            postData();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    protected void onPreExecute(){
    }

    protected void onPostExecute(Double result){
        //pb.setVisibility(View.GONE);
        //MyLog.log("Richiesta al server inviata GET CONTACTS");
    }
    protected void onProgressUpdate(Integer... progress){
        //pb.setProgress(progress[0]);
    }

    public void postData() throws URISyntaxException, UnsupportedEncodingException {

        HttpParams params_ = new BasicHttpParams();
        params_.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params_, "utf-8");

        String BASE_URL = "https://www.googleapis.com/mirror/v1/";
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient(params_);
        HttpPost httppost = new HttpPost();
        httppost.setURI(new URI(BASE_URL + "timeline"));
        httppost.addHeader("Authorization", String.format("Bearer %s", mAuthToken));
        httppost.addHeader("Content-Type", "application/json; charset=UTF-8");
        httppost.setEntity(new StringEntity(json.toString()));

        try {

            // Execute HTTP Post Request
            final HttpResponse response = httpclient.execute(httppost);

            MyLog.log("RESPONSE: " + EntityUtils.toString(response.getEntity()));

        } catch (ClientProtocolException e) {
        } catch (IOException e) {
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

这是回复:

RESPONSE: {
"kind": "mirror#timelineItem",
"id": "ea3f9046-c0a2-4ed5-8a02-f6432f1XXX43",
"selfLink": "https://www.googleapis.com/mirror/v1/timeline/ea3f9046-c0a2-4ed5-8a02-f6432f1XXX43",
"created": "2013-12-22T20:28:37.278Z",
"updated": "2013-12-22T20:28:37.278Z",
"etag": "1387744117278",
"html": "\u003carticle\u003e\u003csection\u003e\u003cdiv class=\"text-x-small\"\u003e\u003cp class=\"blue\"\u003e21:28\u003csub\u003e PM\u003c/sub\u003e\u003c/p\u003e\u003cp class=\"yellow\"\u003eMatteo Valenza\u003cp\u003eTry this accented char   \u003c/p\u003e\u003c/p\u003e\u003c/div\u003e\u003c/section\u003e\u003cfooter\u003e\u003cdiv\u003eWhatsGlass\u003c/div\u003e\u003c/footer\u003e\u003c/article\u003e",
"speakableText": "try this accented char  ",
"menuItems": [
{
"action": "READ_ALOUD"
},
{
"action": "DELETE"
}
],
"notification": {
"level": "DEFAULT"
}
}

为什么我看不到重音字符?如何以正确的方式对其进行编码?

1 个答案:

答案 0 :(得分:1)

问题解决了!

将字符串转义为html:)

message = Html.escapeHtml(message);