Mohammad Rajob
Mohammad Rajob

Reputation: 743

Android UTF-8 encoding for text

I have an android application which get jSON data through HttpGet. But I am facing some problem when I show the data in the text view. The second name in the JSON data shows like my attached photo below. But when I show the jSON data in browser it shows wrong like my photo. Sorry for my poor English.

Here is my activity:

HttpClient client = new DefaultHttpClient();
            String url = "http://54.228.199.162/api/campaigns";
            try {
                String res;
                HttpGet httpget = new HttpGet(url);
                ResponseHandler<String> reshan = new BasicResponseHandler();
                res = client.execute(httpget, reshan);
                Log.d("um1", res);
                JSONArray jsonArray = new JSONArray(res);
                //campaign.setText(res);
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    String id = jsonObject.getString(TAG_ID);
                    byte[] b = id.getBytes("utf-8");
                    String utfid = new String(b);
                    String name = jsonObject.getString(TAG_NAME);
                    byte[] s = name.getBytes("utf-8");
                    String utfname = new String(s);
                    Log.d("IDDDDDDD", id);
                    Log.d("Nameeeeeee", name);
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_ID, utfid);
                    map.put(TAG_NAME, utfname);
                    //Object contactList;
                    contactList.add(map);
                  }
                HttpResponse httpresponse = client.execute(httpget);
                int responsecode = httpresponse.getStatusLine().getStatusCode();
                Log.d("responsenummmm", "um11111"+responsecode);
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            };

Here is the jSON data file:

[{"_id":"520ba75acf17c8cc796b584b","name":"Oulu Liikkujan viikko 16-22.9.2013"},
{"_id":"52161d80ecaf181dc5982624","name":"Ylöjärvi Liikkujan viikko 16-22.9.2013"},
{"_id":"52262fa6d3ee051600d84c68","name":"Lapin yliopiston hyvinvointiviikko 16-22.9.2013"},
{"_id":"5293bbffbf2f15044800011d","name":"testi"},     {"_id":"52a318bbac059a0002000b4f","name":"Standing wave to Oulu"}]

Here is the problem in Text view:

In the second red line, the name

The name of the second one in the text view(photo) should be look like:Ylöjärvi Liikkujan viikko 16-22.9.2013 Please help me.

Upvotes: 2

Views: 2639

Answers (3)

Santhosh
Santhosh

Reputation: 1867

use this to display the name..

Html.fromHtml("<font color='#ffff0000'>enter...label</font>")

Upvotes: 0

GrIsHu
GrIsHu

Reputation: 23638

Try to decode your your value as below :

 String decodedString = URLDecoder.decode(jsonObject.getString(TAG_ID), "UTF-8");

Upvotes: 0

Nitin Misra
Nitin Misra

Reputation: 4522

write like this Html.fromHtml(YOUR STRING)

Upvotes: 1

Related Questions