Text encoding problem

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Text encoding problem

Postby E-Nature » Fri Jan 01, 2010 2:13 pm

Happy new year everyone :-)

I have a text encoding problem and could not find a solution for my problem here yet.

With my Android application I open this link:
http://www.cheat-database.com/..../.... ... eatId=3268

and want to display it in a TableLayout. Connection etc. works fine but I get an encoding problem. For a line break it displays that square symbol etc.
The data in the mysql database is stored in UTF-8. I also used utf8_encode() in the PHP file to display the string properly.

The code to fetch the data in my Android application looks like this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.         public static String getCheatById(int cheatId) {
  3.  
  4.  
  5.  
  6.                 String myString;
  7.  
  8.  
  9.  
  10.                 try {
  11.  
  12.                         /* Define the URL we want to load data from. */
  13.  
  14.                         URL myURL = new URL("http://www.cheat-database.com/..../.....php?cheatId=" + cheatId);
  15.  
  16.  
  17.  
  18.                         /* Open a connection to that URL. */
  19.  
  20.                         URLConnection ucon = myURL.openConnection();
  21.  
  22.  
  23.  
  24.                         /*
  25.  
  26.                          * Define InputStreams to read from the URLConnection.
  27.  
  28.                          */
  29.  
  30.                         InputStream is = ucon.getInputStream();
  31.  
  32.                         BufferedInputStream bis = new BufferedInputStream(is);
  33.  
  34.  
  35.  
  36.                         /*
  37.  
  38.                          * Read bytes to the Buffer until there is nothing more to read(-1).
  39.  
  40.                          */
  41.  
  42.                         ByteArrayBuffer baf = new ByteArrayBuffer(50);
  43.  
  44.                         int current = 0;
  45.  
  46.                         while ((current = bis.read()) != -1) {
  47.  
  48.                                 baf.append((byte) current);
  49.  
  50.                         }
  51.  
  52.  
  53.  
  54.                         /* Convert the Bytes read to a String. */
  55.  
  56.                         myString = new String(baf.toByteArray());
  57.  
  58.  
  59.  
  60.  
  61.  
  62.                 } catch (Exception e) {
  63.  
  64.                         // URL Incorrect
  65.  
  66.                         myString = e.getMessage();
  67.  
  68.                 }
  69.  
  70.  
  71.  
  72.                 return myString;
  73.  
  74.         }
  75.  
  76.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Does anyone know how I can change the encoding to get rid of the odd characters?

thanks for help in advance!
dominik
Last edited by E-Nature on Mon Jan 04, 2010 2:29 pm, edited 1 time in total.
User avatar
E-Nature
Experienced Developer
Experienced Developer
 
Posts: 93
Joined: Mon Nov 16, 2009 3:39 pm

Top

Postby jwei512 » Fri Jan 01, 2010 10:04 pm

I recently wrote three posts on how to work with web based Android coding and I'm pretty sure that within my three code snippets there's a line or two that talks about how to set the content to UTF-8...

Try checking this one out first I think it was in this post:

http://thinkandroid.wordpress.com/2009/ ... t-example/

If not then look at the other two "web" ones and maybe you'll find your answer
- jwei

http://thinkandroid.wordpress.com
Examples, Code, and Tutorials
jwei512
Senior Developer
Senior Developer
 
Posts: 135
Joined: Mon Jul 27, 2009 9:35 pm
Location: Palo Alto, CA

Postby E-Nature » Sat Jan 02, 2010 2:28 pm

thanks, jwei! I will take a look at your tutorial.

In the meantime I also found a solution which works for me. I just had to use the fromHtml() method to put the text I got from the PHP page into a CharSequence. This way it also worked with the encoding plus it also interprets the few HTML formatting tags I have in the text.

My solution:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. CharSequence styledText = Html.fromHtml(cheatText);
  3.  
  4. tvBody.setText(styledText);
  5.  
  6.  
Parsed in 0.030 seconds, using GeSHi 1.0.8.4
User avatar
E-Nature
Experienced Developer
Experienced Developer
 
Posts: 93
Joined: Mon Nov 16, 2009 3:39 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 2 guests