【发布时间】:2015-06-13 09:36:45
【问题描述】:
我能够发送 Emoji 的 unicode,但一直坚持从 PHP API 获取 unicode。
这里我得到了 JsonArray 对象的响应,因为现在只是测试目的,我只是 JsonArray,因为我将来也需要。
["\ud83dde00"]
现在在 Android 中如何获取这个不知道并显示到 EmojiTextView。我用这个例子在TextView和EditText中使用Emoji https://github.com/pepibumur/emojize
这是我的 Java 代码,我创建了一个 API 来发送 unicode 并得到相同的响应,保存在数据库中,工作完美,只是在这里卡住了一点。
new AsyncTask<Void, Void, Object>() {
ProgressDialog pDialog;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
protected void onPreExecute() {
super.onPreExecute();
pDialog = ProgressDialog.show(MainActivity.this, "", "please wait");
}
@Override
protected Object doInBackground(Void... params) {
try {
String contents;
HttpPost request = new HttpPost("http://172.16.16.44/test/save_emoji.php");
List<NameValuePair> param = new ArrayList<NameValuePair>();
param.add(new BasicNameValuePair("addEmoji", s+""));
request.setEntity(new UrlEncodedFormEntity(param,"UTF-8"));
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(request);
response.setHeader("Content-Type", "application/json; charset=utf-8");
InputStream is = response.getEntity().getContent();
InputStreamReader(is));
byte[] buffer = new byte[1024];
int nread;
while ((nread = is.read(buffer)) > 0) {
baos.write(buffer, 0, nread);
}
is.close();
response = null;
is = null;
client = null;
return parseJson();
}catch (JSONException e) {
e.printStackTrace();
try {
return new String(baos.toByteArray(), "UTF-16");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
return e1.getMessage();
}
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
}
}
@SuppressLint("NewApi")
private String parseJson() throws JSONException, UnsupportedEncodingException {
String jsonText = new String(baos.toByteArray());
Log.e("emoji", "resp "+jsonText);
JSONArray jarr = new JSONArray(jsonText);
for(int i=0;i<jarr.length();i++){
Log.e("emoji", new String(jarr.get(i).toString().getBytes(),"UTF-8"));
return jarr.getString(i);
}
return "";
}
protected void onPostExecute(final Object result) {
super.onPostExecute(result);
if(pDialog!=null)
pDialog.dismiss();
MainActivity.this.runOnUiThread(new Runnable(){
@Override
public void run() {
try {
Log.e("emoji", "ss "+result);
mTxtEmojicon.setText(new String(result.toString().getBytes()));
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}.execute();
谢谢
【问题讨论】:
标签: php android mysql unicode emoji