【问题标题】:convert emoticons to unicode将表情符号转换为 unicode
【发布时间】:2015-01-15 10:51:19
【问题描述】:

我需要向服务器发送包含文本和表情符号的文本。我使用 jsonparser 类将其发送到服务器。但是当我发送它时,服务器端似乎包含 unicode 的问号。我应该如何继续转换为 unicode。请帮忙。

    List<NameValuePair> params = new ArrayList<NameValuePair>();

         Log.d("userstatus",userstatus);

        params.add(new BasicNameValuePair("my_status",userstatus));
         params.add(new BasicNameValuePair("user_id",userid));



          try{
              // getting JSON string from URL
               JSONObject json3 = jParser.makeHttpRequest(url_updateprofilestatus, "POST", params);

                 Log.d("json response: ", json3.toString());
                 JSONObject response=json3.getJSONObject("response"); 

我的 JSONParser 类

public class JSONParser {

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

// constructor
public JSONParser() {

}

// function get json from url
// by making HTTP POST or GET mehtod
public JSONObject makeHttpRequest(String url, String method,
        List<NameValuePair> params) {

    // Making HTTP request
    try {

        // check for request method
        if(method == "POST"){
            // request method is POST
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        }else if(method == "GET"){
            // request method is GET
            DefaultHttpClient httpClient = new DefaultHttpClient();
            String paramString = URLEncodedUtils.format(params, "utf-8");
            url += "?" + paramString;
            HttpGet httpGet = new HttpGet(url);

            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
        }          

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

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();

    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;

}

}

我收到带有文本和“?”的回复用于表情符号。请帮助。

【问题讨论】:

    标签: android json unicode emoji


    【解决方案1】:

    我得到了答案。获取表情符号的unicode:

    StringEscapeUtils.escapeJava(<text>); 
    

    并将其解码回表情:

    StringEscapeUtils.unescapeJava(<unicode_emoticon>);
    

    【讨论】:

    • 是这个 API StringEscapeUtils
    猜你喜欢
    • 2015-10-18
    • 1970-01-01
    • 2018-11-16
    • 1970-01-01
    • 1970-01-01
    • 2019-07-11
    • 2012-01-27
    • 2018-07-19
    • 2019-05-20
    相关资源
    最近更新 更多