Pergunta

I send a message with the mirror api from an android app, but when I receive the timeline on my glass I can't see the accented charters like "è"

This is the string:

String text_message = "try this accented char è";

This is the creation of the 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();

This is the AsyncTask that send the JSON to the mirror api :

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();
        }
    }

}

This is the response:

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"
}
}

Why i don't see the accented char? How can I encode it in the correct way?

Foi útil?

Solução

Problem solved !

escape the string to html :)

message = Html.escapeHtml(message);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top