Reputation: 123
I need to send a text that contains text as well as emoticons to the server. I use jsonparser class to send this to the server .But when I send it the server side seems to contain question mark instaed of the unicode. how am I supposed to proceed in converting to unicode. Please help.
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");
My JSONParser class
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;
}
}
I get the response with the text and '?' for emoticons.Please Help.
Upvotes: 0
Views: 1681
Reputation: 123
I got my answer. To get the unicode of the emoticon:
StringEscapeUtils.escapeJava(<text>);
and decode it back to emoticon:
StringEscapeUtils.unescapeJava(<unicode_emoticon>);
Upvotes: 2