Reputation: 1014
I'm parsing data from web in json in gujarati language but when i receive it at android application it looks like
{"message":"Authorized","u_status":0,"c_Alert":0,"get_data":[{"id":"29","End":"2015-02-19","EntrTime":"2015-02-26","Content":"ભરતીનું સ્થળ - સાબર સ્પોર્ટ્સ સ્ટેડિયમ , હિં&","Begin":"2015-03-10","Header":"લશ્કરી ભરતી મેળો - હિંમતનગર","link":"http:\/\/www.google.com"}],"c_Alert_Msg":"No Message","u_link":"http:\/\/www.google.com","c_Alert_Finish":0,"success":1}
when i set any filtered text from json string it looks like
Ē 4;Ĕ 5;Đ 5;đ 9;
(i putted space because it shows perfect unicode in html code) but actually it is
"સ્થળ"
I know thats encoding problem but how did i convert that strings to proper unicode characters
I'm using following code for http request to get json
try {
DefaultHttpClient httpclient = new DefaultHttpClient();
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url_for_is);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
String result = EntityUtils.toString(httpResponse.getEntity());
JSONObject obj = new JSONObject(result);
Log.d("JSON 123123",obj.toString());
} catch (JSONException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
I also tried getting string from json and converting perticular string to unicode but no effect
by this
JSONObject c = contenTs.getJSONObject(1);
String headN = c.getString("Header");
Charset chrutf = Charset.forName("UTF-8");
final String b = new String(headN.getBytes(),chrutf);
System.out.println(b);
or tell me the way that i can convert characters like 'Ē 4;Ĕ 5;Đ 5;đ 9;' to unicode string
Upvotes: 0
Views: 12556
Reputation: 23
// Java will convert it into a UTF-16 representation
String s = “This is my string” ;
// byte representation in UTF-8
ByteBuffer byteBuff = StandardCharset.UTF-8.encode(s);
// do what you want with this byte buffer
String v = new String( bytes, StandardCharset.UTF-8 );
Upvotes: 0
Reputation: 1224
EDIT: Maybe like this:
httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
Or, have you tried encoding your entity using library Gson ?
You can include it like this in your build.gradle (Module: app):
dependencies {
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.2'
}
and then use this part of code:
httpPost.setEntity(new StringEntity(new Gson().toJson(params), HTTP.UTF_8)));
Hope it helps.
Lionel
Upvotes: 1
Reputation: 1014
It's really bad that my post given negative reputation because they don't know the answer but i found the solution myself
I simply converted text to html content in code and displayed it using
String contentN = json.getJSONArray("get_data").getJSONObject(i).getString("c_Alert_Msg");
Html.fromHtml(contentN))
full code
contenTs = json.getJSONArray("get_data");
itemList = new ArrayList<HashMap<String, String>>();
for (int i = 0; i < contenTs.length(); i++) {
JSONObject c = contenTs.getJSONObject(i);
// Storing each json item in variable
String id = c.getString("id");
String headN = c.getString("Header");
String contentN = c.getString("Content");
String time_s = c.getString("Begin");
String time_e = c.getString("End");
String linkIn = c.getString("link");
HashMap map = new HashMap<String, Spannable>();
String txtHeadN = "<font color=#cc0029><strong>" + String.valueOf(i + 1) + " - " + headN + "</font>";
map.put("head", Html.fromHtml(txtHeadN));
map.put("content",Html.fromHtml(contentN));
map.put("Link", time_s + " to " + time_e);
map.put("links",linkIn);
// adding HashList to ArrayList
itemList.add(map);
}
And it worked perfectly fine
Upvotes: 1
Reputation: 385
Try this:
String jsonText = EntityUtils.toString(entity, HTTP.UTF_8);
Upvotes: 0