0

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

&#274 4;&#276 5;&#272 5;&#273 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 '&#274 4;&#276 5;&#272 5;&#273 9;' to unicode string

4
  • Did you tried to convert the result before inserting it into the JSONObject? Commented Feb 26, 2015 at 19:12
  • mean what to convert ? Commented Feb 26, 2015 at 19:30
  • 1
    String result = EntityUtils.toString(httpResponse.getEntity(), HTTP.UTF_8); JSONObject obj = new JSONObject(result); Commented Feb 26, 2015 at 19:31
  • @3d0 No effect string is stil as it was Commented Feb 27, 2015 at 3:46

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

4 Comments

no I've seen that gson is deprecated and I have not much idea about it so I use just personalized code for json response
I edited my post: using new StringEntity(new UrlEncodedFormEntity(params), HTTP.UTF_8) maybe?
new StringEntity(new UrlEncodedFormEntity(params) not works because 'StringEntity(java.lang.String, java.lang.String)' in 'org.apache.http.entity.StringEntity' cannot be applied to '(org.apache.http.client.entity.UrlEncodedFormEntity, java.lang.String)'
I think i have found: httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
0

Try this: String jsonText = EntityUtils.toString(entity, HTTP.UTF_8);

1 Comment

Nopp i don't know what problem is i used your code too but still no effect
0
// 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 );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.