Getting Data From the Web (URLConnection via http)

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Getting Data From the Web (URLConnection via http)

Postby plusminus » Sat Dec 22, 2007 4:37 pm

Getting Data From the Web (URLConnection via http)


What you will learn: You will learn how to download files/data from any URL (useful for reading the returned xml-data from Web2.0-APIs)

:?: Problems/Questions: Write it right below...

Difficulty: 1.5 of 5 :)

What it will look like:
Image


Description:
0.) We will use just URL, URLConnection and InputStreams to receive data from a txt-file via http from the web. (works with any other dynamic url, too :!: like the Google Weather API http://www.google.com/ig/api?weather=Sc ... im,Germany )

This is the full Code:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package org.anddev.android.getdatafromtheweb;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.InputStream;
  5. import java.net.URL;
  6. import java.net.URLConnection;
  7.  
  8. import org.apache.http.util.ByteArrayBuffer;
  9.  
  10. import android.app.Activity;
  11. import android.os.Bundle;
  12. import android.widget.TextView;
  13.  
  14. public class GetDataFromTheWeb extends Activity {
  15.         @Override
  16.         public void onCreate(Bundle icicle) {
  17.                 super.onCreate(icicle);
  18.  
  19.                 /* We will show the data we read in a TextView. */
  20.                 TextView tv = new TextView(this);
  21.                
  22.                 /* Will be filled and displayed later. */
  23.                 String myString = null;
  24.                 try {
  25.                         /* Define the URL we want to load data from. */
  26.                         URL myURL = new URL(
  27.                                         "http://www.anddev.org/images/tut/basic/getdatafromtheweb/loadme.txt");
  28.                         /* Open a connection to that URL. */
  29.                         URLConnection ucon = myURL.openConnection();
  30.  
  31.                         /* Define InputStreams to read
  32.                          * from the URLConnection. */
  33.                         InputStream is = ucon.getInputStream();
  34.                         BufferedInputStream bis = new BufferedInputStream(is);
  35.                        
  36.                         /* Read bytes to the Buffer until
  37.                          * there is nothing more to read(-1). */
  38.                         ByteArrayBuffer baf = new ByteArrayBuffer(50);
  39.                         int current = 0;
  40.                         while((current = bis.read()) != -1){
  41.                                 baf.append((byte)current);
  42.                         }
  43.  
  44.                         /* Convert the Bytes read to a String. */
  45.                         myString = new String(baf.toByteArray());
  46.                 } catch (Exception e) {
  47.                         /* On any Error we want to display it. */
  48.                         myString = e.getMessage();
  49.                 }
  50.                 /* Show the String on the GUI. */
  51.                 tv.setText(myString);
  52.                 this.setContentView(tv);
  53.         }
  54. }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4

Thats it :)

Regards,
plusminus
Last edited by plusminus on Sun Feb 03, 2008 7:03 pm, edited 2 times in total.
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby venkat » Sat Dec 22, 2007 4:58 pm

Dear PlusMinus, Thank u for Great Tutorial. :)

Can you tell me how to set proxy and authentication using code. because i am behind the proxy and it will ask authentication. i have attached screen shot of my error.

Thanks and regards,
venkat.
Attachments
device.png
device.png (5.92 KiB) Viewed 88983 times
venkat
Senior Developer
Senior Developer
 
Posts: 152
Joined: Tue Nov 27, 2007 5:42 am
Location: India

Postby plusminus » Sat Dec 22, 2007 5:01 pm

Hello venkat,

perhaps sth like this works:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                         // ...
  2.  
  3.                         /* Open a connection to that URL. */
  4.  
  5.                         URLConnection ucon = myURL.openConnection(new Proxy(...));
  6.  
  7.                         // ...
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby venkat » Sat Dec 22, 2007 5:20 pm

yes plusminus, i tried this. but still it not working. i have attached my full source code here..


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class GetDataFromTheWeb extends Activity
  2.  
  3. {
  4.  
  5.      String myString = "Test: ";
  6.  
  7.    Proxy p;
  8.  
  9.     @Override
  10.  
  11.     public void onCreate(Bundle icicle)
  12.  
  13.     {
  14.  
  15.         try{   
  16.  
  17.                
  18.  
  19.                 Socket s=new Socket("vsnlproxy.in",3128);
  20.  
  21.         SocketAddress sa=s.getLocalSocketAddress();
  22.  
  23.                 p=new Proxy(Proxy.Type.HTTP,sa);
  24.  
  25.         }catch(Exception e)
  26.  
  27.         {
  28.  
  29.                 myString = "Socket Error"+e;
  30.  
  31.         }
  32.  
  33.         super.onCreate(icicle);
  34.  
  35.         TextView tv = new TextView(this);
  36.  
  37.         try
  38.  
  39.         {
  40.  
  41.           URL myURL = new URL("http://www.anddev.org/images/tut/basic/getdatafromtheweb/loadme.txt");
  42.  
  43.  
  44.  
  45.           URLConnection ucon = myURL.openConnection();
  46.  
  47.          
  48.  
  49.           InputStream is = ucon.getInputStream();
  50.  
  51.           InputStream bis = new BufferedInputStream(is);
  52.  
  53.           Reader reader = new InputStreamReader(bis);
  54.  
  55.          
  56.  
  57.           int x;
  58.  
  59.           int count = 0;  
  60.  
  61.           byte by[] = new byte[20];
  62.  
  63.           while((x = reader.read()) != -1)
  64.  
  65.           {
  66.  
  67.                if(count < 20)
  68.  
  69.                {
  70.  
  71.                     by[count] = (byte) x;
  72.  
  73.                }
  74.  
  75.                count++;
  76.  
  77.           }
  78.  
  79.           for(int i=0; i<20; i++)
  80.  
  81.           {
  82.  
  83.                myString = myString + (char)by[i];
  84.  
  85.           }
  86.  
  87.         }
  88.  
  89.         catch(MalformedURLException ex)
  90.  
  91.         {
  92.  
  93.           myString = "MalformedURLException occured.";
  94.  
  95.         }
  96.  
  97.         catch(IOException ex)
  98.  
  99.         {
  100.  
  101.           myString = "IOException occured." + ex;
  102.  
  103.         }
  104.  
  105.        
  106.  
  107.         tv.setText(myString);
  108.  
  109.         setContentView(tv);
  110.  
  111.     }
  112.  
  113. }
Parsed in 0.040 seconds, using GeSHi 1.0.8.4
venkat
Senior Developer
Senior Developer
 
Posts: 152
Joined: Tue Nov 27, 2007 5:42 am
Location: India

Postby plusminus » Sat Dec 22, 2007 5:27 pm

Hello venkat,

I'm out of idas as I never was behind a proxy... :(
Did you put p in to the openConnection :?:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. URLConnection ucon = myURL.openConnection(p);
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
:?:

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

How to get the full HTML source of the page??

Postby Nitinkcv » Sat Dec 29, 2007 7:46 pm

Hi,

I tried to get the full HTML source of a page but i was getting an IllegalArgumentException being thrown at the line
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. InputStream is = ucon.getInputStream();
  3.  
  4.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4

Not sure what the prob is. It would be great if it could be solved out

Thanx,
Nitin
Nitinkcv
Developer
Developer
 
Posts: 29
Joined: Thu Nov 29, 2007 1:02 pm

Top

Postby plusminus » Thu Jan 03, 2008 12:11 pm

Hello Nitinkcv,

thats pretty strange, as getInputStream() takes to Parameters and should not throw an IllegalArgumentException :(

Code: Select all
getInputStream

public InputStream getInputStream()
                           throws IOException

    Returns an input stream that reads from this open connection.

    Returns:
        an input stream that reads from this open connection.
    Throws:
        IOException - if an I/O error occurs while creating the input stream.
        UnknownServiceException - if the protocol does not support input.


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Correct not getting parsed..

Postby Nitinkcv » Sat Jan 05, 2008 7:22 am

Hi plusminus,

i was able to parse the html source, but the problem is that the correct source is not getting parsed.

My java file is:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. /**
  2.  *
  3.  */
  4. package com.cognizant.Transliterate;
  5.  
  6. import java.io.BufferedInputStream;
  7. import java.io.BufferedReader;
  8. import java.io.DataInputStream;
  9. import java.io.InputStream;
  10. import java.io.Reader;
  11.  
  12. import org.apache.commons.httpclient.HostConfiguration;
  13. import org.apache.commons.httpclient.HttpConnection;
  14. import org.apache.commons.httpclient.HttpConnectionManager;
  15. import org.apache.commons.httpclient.HttpState;
  16. import org.apache.commons.httpclient.HttpURL;
  17. import org.apache.commons.httpclient.SimpleHttpConnectionManager;
  18. import org.apache.commons.httpclient.methods.PostMethod;
  19. import org.apache.http.util.ByteArrayBuffer;
  20.  
  21. import android.app.Activity;
  22. import android.os.Bundle;
  23. import android.util.Log;
  24. import android.widget.TextView;
  25.  
  26.  
  27. public class Literate1 extends Activity {
  28.  
  29.         protected static HttpConnectionManager connectionManager = new SimpleHttpConnectionManager();
  30.  
  31.         @Override
  32.         public void onCreate(Bundle icicle) {
  33.                 super.onCreate(icicle);
  34.  
  35.                 /* We will show the data we read in a TextView. */
  36.                 TextView tv = new TextView(this);
  37.                 HttpConnection connection = null;
  38.                 /* Will be filled and displayed later. */
  39.                 String myString = null;
  40.  
  41.                 /* Define the URL we want to load data from. */
  42.                 String baseURL = "http://www.google.com/transliterate/indic?tlqt=1&langpair=en|ml&text=sukham%2C&tl_app=3";
  43.  
  44.                 // Setup the connection
  45.  
  46.                 try {
  47.                         HttpURL httpURL = new HttpURL(baseURL);
  48.                         HostConfiguration host = new HostConfiguration();
  49.                         host.setHost(httpURL.getHost(), httpURL.getPort());
  50.                         connection = connectionManager.getConnection(host);
  51.  
  52.                         Log.i("MyLogger", "Before Opening Conn");
  53.  
  54.                         // Open it
  55.                         connection.open();
  56.  
  57.                         // Post (send) data to the connection
  58.                         PostMethod postMethod = new PostMethod(baseURL);
  59.                        
  60.                         postMethod.execute(new HttpState(), connection);
  61.  
  62.                         InputStream response = postMethod.getResponseBodyAsStream();
  63.  
  64.                         Log.i("MyLogger", response.toString());
  65.                         BufferedInputStream bis = new BufferedInputStream(response);
  66.                        
  67.                         ByteArrayBuffer baf = new ByteArrayBuffer(450);
  68.                         int current = 0;
  69.                         while ((current = bis.read()) != -1) {
  70.                                 baf.append((byte) current);
  71.                         }
  72.                        
  73.                         /* Convert the Bytes read to a String. */
  74.                         myString = new String(baf.toByteArray());
  75.                         bis.close();
  76.                         Log.i("MyLogger", "myString is :" + myString);
  77.                        
  78.                 } catch (Exception e) {
  79.                         Log.i("MyLogger", "myString is :" + myString);
  80.                         Log.i("MyLogger", e.toString());
  81.                         myString = e.getMessage();
  82.                 } finally {
  83.                         connection.close();
  84.                 }
  85.  
  86.                
  87.                 /* Show the String on the GUI. */
  88.                 tv.setText(myString);
  89.                 this.setContentView(tv);
  90.  
  91.         }
  92. }
  93.  
Parsed in 0.046 seconds, using GeSHi 1.0.8.4


The url which i pass is http://www.google.com/transliterate/ind ... C&tl_app=3

:?: Hmmm not able to put the above url correctly.. the full url is up till the tl_app=3 :?:

But the html source that i get is not the above one's html source. i believe that the html source i get is that of this

Any Workarounds..

Thanx,
Nitin
Nitinkcv
Developer
Developer
 
Posts: 29
Joined: Thu Nov 29, 2007 1:02 pm

Postby plusminus » Sat Jan 05, 2008 1:27 pm

Hello Nitinkcv,

with me it returns: "Argument must not be null". Very probably a problem with the URL-Encoding.

Working...

Yes it is, the "|" needs to be replaced by its urlencoded-expression "%7C":
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. String baseURL = "http://www.google.com/transliterate/indic?tlqt=1&langpair=en|ml&text=sukham&tl_app=3";
  2.                                         baseURL = baseURL.replace("|", "%7C");
  3.                                         URL myURL = new URL(baseURL);
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Returns:
while(1);
[
{
"ew" : "sukham",
"hws" : [
"സുഖം","സുക്തം","സുക്ഹം","സുഖമ","സ്ഖം",
]
},
]

Without back-conversion of the (indian ?) html-specialchars:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. while(1);
  2. [
  3. {
  4. "ew" : "sukham",
  5. "hws" : [
  6. "സുഖം", "സുക്തം", "സുക്ഹം", "സുഖമ", "സ്ഖം",
  7. ]
  8. },
  9. ]]
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Let us know if it helped :)

Regards,
plusminus
Last edited by plusminus on Sun Jan 06, 2008 7:45 pm, edited 2 times in total.
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby venkat » Sat Jan 05, 2008 3:10 pm

Hi Plusminus, :)
Can you tell me , how to upload files or data from android to web server.

Thanks and Regards,
Venkat.
venkat
Senior Developer
Senior Developer
 
Posts: 152
Joined: Tue Nov 27, 2007 5:42 am
Location: India

Postby Nitinkcv » Sat Jan 05, 2008 3:19 pm

Hi PlusMinus,

Thanx for the reply :D . While the app i'm getting the output without back-conversion of the html-specialchars. i.e. with the symbols സ etc..

Was wondering how to get them in the respective language.. i.e. സുഖം

I believe they are unicode format. But was not able to get it still :?: .

Thanx,
Nitin
Nitinkcv
Developer
Developer
 
Posts: 29
Joined: Thu Nov 29, 2007 1:02 pm

x-ISCII91 is not registered.

Postby Nitinkcv » Sat Jan 05, 2008 4:51 pm

Hmmm... Seems like x-ISCII91 is the required charset. Got this from here.

but still im getting the exception UnsupportedEncodingException.

Found this code to get all the installed charsets in the JVM.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import java.nio.charset.Charset;
  2. import java.util.Iterator;
  3. import java.util.Map;
  4.  
  5. public class GetAllCharSets {
  6.  
  7.   public static void main(String[] args) {
  8.     Map charsets = Charset.availableCharsets();
  9.     Iterator iterator = charsets.values().iterator();
  10.     while (iterator.hasNext()) {
  11.       Charset cs = (Charset) iterator.next();
  12.       System.out.print(cs.displayName());
  13.       if (cs.isRegistered()) {
  14.         System.out.print(" (registered): ");
  15.       } else {
  16.         System.out.print(" (unregistered): ");
  17.       }
  18.       System.out.print(cs.name());
  19.       Iterator names = cs.aliases().iterator();
  20.       while (names.hasNext()) {
  21.         System.out.print(", ");
  22.         System.out.print(names.next());
  23.       }
  24.       System.out.println();
  25.     }
  26.   }
  27. }
  28.  
Parsed in 0.040 seconds, using GeSHi 1.0.8.4


This is the result i got:


Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. Big5 (registered): Big5, csBig5
  2. Big5-HKSCS (registered): Big5-HKSCS, big5-hkscs, big5hk, big5-hkscs:unicode3.0, big5hkscs, Big5_HKSCS
  3. EUC-JP (registered): EUC-JP, eucjis, x-eucjp, csEUCPkdFmtjapanese, eucjp, Extended_UNIX_Code_Packed_Format_for_Japanese, x-euc-jp, euc_jp
  4. EUC-KR (registered): EUC-KR, ksc5601, 5601, ksc5601_1987, ksc_5601, ksc5601-1987, euc_kr, ks_c_5601-1987, euckr, csEUCKR
  5. GB18030 (registered): GB18030, gb18030-2000
  6. GB2312 (registered): GB2312, gb2312-1980, gb2312, EUC_CN, gb2312-80, euc-cn, euccn, x-EUC-CN
  7. GBK (registered): GBK, windows-936, CP936
  8. IBM-Thai (registered): IBM-Thai, ibm-838, ibm838, 838, cp838
  9. IBM00858 (registered): IBM00858, cp858, ccsid00858, cp00858, 858
  10. IBM01140 (registered): IBM01140, 1140, ccsid01140, cp01140, cp1140
  11. IBM01141 (registered): IBM01141, cp01141, cp1141, ccsid01141, 1141
  12. IBM01142 (registered): IBM01142, cp01142, cp1142, ccsid01142, 1142
  13. IBM01143 (registered): IBM01143, 1143, cp01143, cp1143, ccsid01143
  14. IBM01144 (registered): IBM01144, cp01144, cp1144, 1144, ccsid01144
  15. IBM01145 (registered): IBM01145, ccsid01145, cp01145, 1145, cp1145
  16. IBM01146 (registered): IBM01146, ccsid01146, cp1146, 1146, cp01146
  17. IBM01147 (registered): IBM01147, cp1147, 1147, ccsid01147, cp01147
  18. IBM01148 (registered): IBM01148, cp01148, cp1148, ccsid01148, 1148
  19. IBM01149 (registered): IBM01149, cp1149, ccsid01149, 1149, cp01149
  20. IBM037 (registered): IBM037, csIBM037, cpibm37, cp037, cs-ebcdic-cp-us, ibm-037, ibm-37, cs-ebcdic-cp-ca, cs-ebcdic-cp-wt, cs-ebcdic-cp-nl, ibm037, 037
  21. IBM1026 (registered): IBM1026, 1026, ibm1026, cp1026, ibm-1026
  22. IBM1047 (registered): IBM1047, 1047, ibm-1047, cp1047
  23. IBM273 (registered): IBM273, ibm273, 273, cp273, ibm-273
  24. IBM277 (registered): IBM277, ibm277, cp277, ibm-277, 277
  25. IBM278 (registered): IBM278, csIBM278, ibm278, cp278, ebcdic-cp-se, 278, ibm-278, ebcdic-sv
  26. IBM280 (registered): IBM280, ibm280, cp280, 280, ibm-280
  27. IBM284 (registered): IBM284, cpibm284, csIBM284, ibm-284, ibm284, 284, cp284
  28. IBM285 (registered): IBM285, 285, ebcdic-cp-gb, ibm-285, csIBM285, cp285, ibm285, cpibm285, ebcdic-gb
  29. IBM297 (registered): IBM297, csIBM297, ebcdic-cp-fr, cp297, ibm297, ibm-297, 297, cpibm297
  30. IBM420 (registered): IBM420, ibm420, 420, ebcdic-cp-ar1, csIBM420, ibm-420, cp420
  31. IBM424 (registered): IBM424, cp424, 424, ebcdic-cp-he, ibm424, csIBM424, ibm-424
  32. IBM437 (registered): IBM437, windows-437, cspc8codepage437, ibm437, cp437, 437, ibm-437
  33. IBM500 (registered): IBM500, 500, ebcdic-cp-ch, ebcdic-cp-bh, ibm-500, csIBM500, cp500, ibm500
  34. IBM775 (registered): IBM775, ibm775, cp775, ibm-775, 775
  35. IBM850 (registered): IBM850, ibm-850, 850, ibm850, cspc850multilingual, cp850
  36. IBM852 (registered): IBM852, 852, ibm-852, csPCp852, cp852, ibm852
  37. IBM855 (registered): IBM855, 855, ibm855, cp855, cspcp855, ibm-855
  38. IBM857 (registered): IBM857, cp857, ibm857, csIBM857, 857, ibm-857
  39. IBM860 (registered): IBM860, ibm860, ibm-860, csIBM860, cp860, 860
  40. IBM861 (registered): IBM861, csIBM861, ibm861, 861, cp861, ibm-861
  41. IBM862 (registered): IBM862, cp862, ibm862, 862, ibm-862, csIBM862
  42. IBM863 (registered): IBM863, cp863, csIBM863, ibm863, 863, ibm-863
  43. IBM864 (registered): IBM864, csIBM864, ibm-864, 864, ibm864, cp864
  44. IBM865 (registered): IBM865, ibm-865, csIBM865, 865, ibm865, cp865
  45. IBM866 (registered): IBM866, 866, ibm-866, csIBM866, ibm866, cp866
  46. IBM868 (registered): IBM868, cp-ar, 868, ibm868, csIBM868, ibm-868, cp868
  47. IBM869 (registered): IBM869, ibm869, ibm-869, 869, cp869, csIBM869, cp-gr
  48. IBM870 (registered): IBM870, ebcdic-cp-yu, ibm870, ibm-870, 870, csIBM870, cp870, ebcdic-cp-roece
  49. IBM871 (registered): IBM871, csIBM871, ibm-871, cp871, ebcdic-cp-is, 871, ibm871
  50. IBM918 (registered): IBM918, ibm-918, 918, cp918, ebcdic-cp-ar2
  51. ISO-2022-CN (registered): ISO-2022-CN, csISO2022CN, ISO2022CN
  52. ISO-2022-JP (registered): ISO-2022-JP, jis, jis_encoding, csjisencoding, csISO2022JP, iso2022jp
  53. ISO-2022-KR (registered): ISO-2022-KR, ISO2022KR, csISO2022KR
  54. ISO-8859-1 (registered): ISO-8859-1, iso-ir-100, 8859_1, ISO_8859-1, ISO8859_1, 819, csISOLatin1, IBM-819, ISO_8859-1:1987, latin1, cp819, ISO8859-1, IBM819, ISO_8859_1, l1
  55. ISO-8859-13 (registered): ISO-8859-13, ISO8859-13, 8859_13, iso8859_13, iso_8859-13
  56. ISO-8859-15 (registered): ISO-8859-15, 8859_15, csISOlatin9, IBM923, cp923, 923, L9, IBM-923, ISO8859-15, LATIN9, ISO_8859-15, LATIN0, csISOlatin0, ISO8859_15_FDIS, ISO-8859-15, ISO8859_15
  57. ISO-8859-2 (registered): ISO-8859-2, ibm912, l2, ibm-912, cp912, ISO_8859-2:1987, ISO_8859-2, latin2, csISOLatin2, iso8859_2, 912, 8859_2, ISO8859-2, iso-ir-101
  58. ISO-8859-3 (registered): ISO-8859-3, iso8859_3, cp913, csISOLatin3, ibm-913, ISO_8859-3, 913, ISO8859-3, 8859_3, ibm913, iso-ir-109, ISO_8859-3:1988, latin3, l3
  59. ISO-8859-4 (registered): ISO-8859-4, iso-ir-110, l4, 8859_4, ibm914, latin4, ibm-914, csISOLatin4, iso8859_4, iso8859-4, cp914, 914, ISO_8859-4:1988, ISO_8859-4
  60. ISO-8859-5 (registered): ISO-8859-5, 915, ISO_8859-5:1988, iso8859_5, cp915, ibm915, ISO_8859-5, ISO8859-5, csISOLatinCyrillic, cyrillic, 8859_5, iso-ir-144, ibm-915
  61. ISO-8859-6 (registered): ISO-8859-6, 8859_6, arabic, ibm-1089, iso8859_6, ISO_8859-6, iso-ir-127, ibm1089, ISO_8859-6:1987, ECMA-114, 1089, csISOLatinArabic, ISO8859-6, ASMO-708, cp1089
  62. ISO-8859-7 (registered): ISO-8859-7, sun_eu_greek, 8859_7, iso-ir-126, ISO_8859-7:1987, ibm-813, iso8859_7, ISO_8859-7, csISOLatinGreek, greek8, ECMA-118, ibm813, ELOT_928, iso8859-7, cp813, greek, 813
  63. ISO-8859-8 (registered): ISO-8859-8, iso-ir-138, ibm-916, iso8859_8, cp916, ISO8859-8, ISO_8859-8:1988, hebrew, 8859_8, csISOLatinHebrew, ibm916, 916, ISO_8859-8
  64. ISO-8859-9 (registered): ISO-8859-9, cp920, l5, ISO_8859-9, ibm-920, csISOLatin5, 8859_9, iso-ir-148, latin5, 920, ISO8859-9, ibm920, ISO_8859-9:1989, iso8859_9
  65. JIS_X0201 (registered): JIS_X0201, JIS_X0201, X0201, JIS0201, csHalfWidthKatakana
  66. JIS_X0212-1990 (registered): JIS_X0212-1990, jis_x0212-1990, iso-ir-159, x0212, JIS0212, csISO159JISX02121990
  67. KOI8-R (registered): KOI8-R, koi8, koi8_r, cskoi8r
  68. Shift_JIS (registered): Shift_JIS, shift-jis, shift_jis, x-sjis, ms_kanji, csShiftJIS, sjis
  69. TIS-620 (registered): TIS-620, tis620.2533, tis620
  70. US-ASCII (registered): US-ASCII, ISO646-US, IBM367, ASCII, cp367, default, ascii7, ANSI_X3.4-1986, iso-ir-6, us, 646, iso_646.irv:1983, csASCII, ANSI_X3.4-1968, ISO_646.irv:1991
  71. UTF-16 (registered): UTF-16, utf16, UTF_16
  72. UTF-16BE (registered): UTF-16BE, X-UTF-16BE, UnicodeBigUnmarked, UTF_16BE, ISO-10646-UCS-2
  73. UTF-16LE (registered): UTF-16LE, UnicodeLittleUnmarked, X-UTF-16LE, UTF_16LE
  74. UTF-8 (registered): UTF-8, UTF8, unicode-1-1-utf-8
  75. windows-1250 (registered): windows-1250, cp1250, cp5346
  76. windows-1251 (registered): windows-1251, ansi-1251, cp1251, cp5347
  77. windows-1252 (registered): windows-1252, cp1252, cp5348
  78. windows-1253 (registered): windows-1253, cp1253, cp5349
  79. windows-1254 (registered): windows-1254, cp5350, cp1254
  80. windows-1255 (registered): windows-1255, cp1255
  81. windows-1256 (registered): windows-1256, cp1256
  82. windows-1257 (registered): windows-1257, cp1257, cp5353
  83. windows-1258 (registered): windows-1258, cp1258
  84. windows-31j (registered): windows-31j, csWindows31J, windows-932, MS932
  85. x-Big5-Solaris (unregistered): x-Big5-Solaris, Big5_Solaris
  86. x-euc-jp-linux (unregistered): x-euc-jp-linux, euc_jp_linux, euc-jp-linux
  87. x-EUC-TW (unregistered): x-EUC-TW, cns11643, euc_tw, EUC-TW, euctw
  88. x-eucJP-Open (unregistered): x-eucJP-Open, EUC_JP_Solaris, eucJP-open
  89. x-IBM1006 (unregistered): x-IBM1006, cp1006, ibm1006, 1006, ibm-1006
  90. x-IBM1025 (unregistered): x-IBM1025, ibm1025, 1025, cp1025, ibm-1025
  91. x-IBM1046 (unregistered): x-IBM1046, ibm1046, 1046, cp1046, ibm-1046
  92. x-IBM1097 (unregistered): x-IBM1097, ibm1097, 1097, cp1097, ibm-1097
  93. x-IBM1098 (unregistered): x-IBM1098, cp1098, ibm-1098, ibm1098, 1098
  94. x-IBM1112 (unregistered): x-IBM1112, cp1112, 1112, ibm1112, ibm-1112
  95. x-IBM1122 (unregistered): x-IBM1122, ibm-1122, 1122, cp1122, ibm1122
  96. x-IBM1123 (unregistered): x-IBM1123, cp1123, ibm1123, ibm-1123, 1123
  97. x-IBM1124 (unregistered): x-IBM1124, cp1124, ibm1124, ibm-1124, 1124
  98. x-IBM1381 (unregistered): x-IBM1381, 1381, cp1381, ibm1381, ibm-1381
  99. x-IBM1383 (unregistered): x-IBM1383, ibm1383, ibm-1383, cp1383, 1383
  100. x-IBM33722 (unregistered): x-IBM33722, ibm-33722, cp33722, ibm-33722_vascii_vpua, ibm-5050, ibm33722, 33722
  101. x-IBM737 (unregistered): x-IBM737, ibm-737, ibm737, cp737, 737
  102. x-IBM834 (unregistered): x-IBM834, cp834, ibm-834, ibm834
  103. x-IBM856 (unregistered): x-IBM856, ibm-856, 856, ibm856, cp856
  104. x-IBM874 (unregistered): x-IBM874, cp874, ibm874, ibm-874, 874
  105. x-IBM875 (unregistered): x-IBM875, ibm875, ibm-875, 875, cp875
  106. x-IBM921 (unregistered): x-IBM921, 921, cp921, ibm921, ibm-921
  107. x-IBM922 (unregistered): x-IBM922, cp922, ibm922, ibm-922, 922
  108. x-IBM930 (unregistered): x-IBM930, cp930, 930, ibm930, ibm-930
  109. x-IBM933 (unregistered): x-IBM933, ibm933, cp933, 933, ibm-933
  110. x-IBM935 (unregistered): x-IBM935, 935, cp935, ibm935, ibm-935
  111. x-IBM937 (unregistered): x-IBM937, cp937, ibm-937, ibm937, 937
  112. x-IBM939 (unregistered): x-IBM939, ibm-939, ibm939, cp939, 939
  113. x-IBM942 (unregistered): x-IBM942, cp942, ibm942, ibm-942, 942
  114. x-IBM942C (unregistered): x-IBM942C, ibm942C, cp942C, ibm-942C, 942C
  115. x-IBM943 (unregistered): x-IBM943, ibm943, ibm-943, cp943, 943
  116. x-IBM943C (unregistered): x-IBM943C, ibm-943C, ibm943C, 943C, cp943C
  117. x-IBM948 (unregistered): x-IBM948, 948, ibm-948, cp948, ibm948
  118. x-IBM949 (unregistered): x-IBM949, ibm-949, cp949, 949, ibm949
  119. x-IBM949C (unregistered): x-IBM949C, cp949C, 949C, ibm949C, ibm-949C
  120. x-IBM950 (unregistered): x-IBM950, 950, cp950, ibm-950, ibm950
  121. x-IBM964 (unregistered): x-IBM964, 964, cp964, ibm-964, ibm964
  122. x-IBM970 (unregistered): x-IBM970, ibm970, 970, cp970, ibm-eucKR, ibm-970
  123. x-ISCII91 (unregistered): x-ISCII91, iscii, ST_SEV_358-88, iso-ir-153, csISO153GOST1976874, ISCII91
  124. x-ISO-2022-CN-CNS (unregistered): x-ISO-2022-CN-CNS, ISO2022CN_CNS, ISO-2022-CN-CNS
  125. x-ISO-2022-CN-GB (unregistered): x-ISO-2022-CN-GB, ISO-2022-CN-GB, ISO2022CN_GB
  126. x-iso-8859-11 (unregistered): x-iso-8859-11, iso-8859-11, iso8859_11
  127. x-JIS0208 (unregistered): x-JIS0208, JIS0208, csISO87JISX0208, x0208, JIS_C6226-1983, JIS_X0208-1983, iso-ir-87
  128. x-JISAutoDetect (unregistered): x-JISAutoDetect, JISAutoDetect
  129. x-Johab (unregistered): x-Johab, johab, ms1361, ksc5601-1992, ksc5601_1992
  130. x-MacArabic (unregistered): x-MacArabic, MacArabic
  131. x-MacCentralEurope (unregistered): x-MacCentralEurope, MacCentralEurope
  132. x-MacCroatian (unregistered): x-MacCroatian, MacCroatian
  133. x-MacCyrillic (unregistered): x-MacCyrillic, MacCyrillic
  134. x-MacDingbat (unregistered): x-MacDingbat, MacDingbat
  135. x-MacGreek (unregistered): x-MacGreek, MacGreek
  136. x-MacHebrew (unregistered): x-MacHebrew, MacHebrew
  137. x-MacIceland (unregistered): x-MacIceland, MacIceland
  138. x-MacRoman (unregistered): x-MacRoman, MacRoman
  139. x-MacRomania (unregistered): x-MacRomania, MacRomania
  140. x-MacSymbol (unregistered): x-MacSymbol, MacSymbol
  141. x-MacThai (unregistered): x-MacThai, MacThai
  142. x-MacTurkish (unregistered): x-MacTurkish, MacTurkish
  143. x-MacUkraine (unregistered): x-MacUkraine, MacUkraine
  144. x-MS950-HKSCS (unregistered): x-MS950-HKSCS, MS950_HKSCS
  145. x-mswin-936 (unregistered): x-mswin-936, ms936, ms_936
  146. x-PCK (unregistered): x-PCK, pck
  147. x-windows-50220 (unregistered): x-windows-50220, cp50220, ms50220
  148. x-windows-50221 (unregistered): x-windows-50221, ms50221, cp50221
  149. x-windows-874 (unregistered): x-windows-874, windows-874, ms874, ms-874
  150. x-windows-949 (unregistered): x-windows-949, windows949, ms_949, ms949
  151. x-windows-950 (unregistered): x-windows-950, windows-950, ms950
  152. x-windows-iso2022jp (unregistered): x-windows-iso2022jp, windows-iso2022jp
  153.  
Parsed in 0.000 seconds, using GeSHi 1.0.8.4


But the charset x-ISCII91 is unregistered. So could that be the reason that im not gettin it right. Also i put an entry in the strings.xml..


<string name="default_output">സുഖം</string>


and tried to display that in the emulator but it displayed the notorious SQUARE BOXES :(

So does that mean android doesnt support indic charset or do i have to some make that x-ISCII91 'REGISTERED' instead of 'UNREGISTERED'...

Will investigate more and come back.

Thnax,
Nitin
Nitinkcv
Developer
Developer
 
Posts: 29
Joined: Thu Nov 29, 2007 1:02 pm

Postby plusminus » Sun Jan 06, 2008 1:03 pm

Hello Nitin,

as this page uses
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

and the chars from my post above get displayed correctly and "iso-8859-1" it is listed in your output as "registered" it should work.

Especially when you display it within a WebView, you can set the charset yourself.
Does UTF-8 not work :?:

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby Nitinkcv » Sun Jan 06, 2008 4:10 pm

Hi plusminus,

I'm afraid utf-8 does not work.. also by default the InputStreamReader takes the iso-8859-1 format which also didnt work :( . Still getting as സ etc..

Were you able to get the output in the emulator as :

while(1);
[
{
"ew" : "sukham",
"hws" : [
"സുഖം","സുക്തം","സുക്ഹം","സുഖമ","സ്ഖം",
]
},
]


Coz i find that you had initially posted as returns :?:

If thats the case could you please post your code which you had got the output correctly.

Also i could use a webview (not sure how to use though :roll: ) but what i wanted was to get the source and put it in a String for further use.

Im posting my code here( pretty much the same as the one in the post), just to be sure that we are on the same track..

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import java.io.IOException;
  2. import java.io.InputStream;
  3. import java.io.InputStreamReader;
  4. import java.io.Reader;
  5. import java.net.HttpURLConnection;
  6. import java.net.URL;
  7.  
  8. import org.apache.http.util.ByteArrayBuffer;
  9.  
  10. import android.app.Activity;
  11. import android.os.Bundle;
  12. import android.util.Log;
  13. import android.webkit.WebView;
  14. import android.widget.TextView;
  15.  
  16. public class Transliterate extends Activity {
  17.         @Override
  18.         public void onCreate(Bundle icicle) {
  19.                 super.onCreate(icicle);
  20.  
  21.                 /* We will show the data we read in a TextView. */
  22.                 TextView tv = new TextView(this);
  23.                
  24.                 /* Will be filled and displayed later. */
  25.                 String myString = null;
  26.                 HttpURLConnection ucon = null;
  27.                 try {
  28.                         /* Define the URL we want to load data from. */
  29.                         String indicURL = "http://www.google.com/transliterate/indic?tlqt=1&langpair=en|hi&text=yaar%2C&tl_app=3";
  30.                        
  31.                         String newIndicURL = indicURL.replace("|", "%7C");
  32.                        
  33.                         URL myURL = new URL(newIndicURL);
  34.  
  35.                         Log.i("MyLogger", "Before Opening Conn");
  36.  
  37.                         /* Open a connection to that URL. */
  38.  
  39.                         ucon = (HttpURLConnection)myURL.openConnection();
  40.                         ucon.setRequestMethod("GET");
  41.                         ucon.connect();
  42.  
  43.                         Log.i("MyLogger", "After Opening Conn");
  44.  
  45.                         /*
  46.                          * Define InputStreams to read from the URLConnection.
  47.                          */
  48.                         InputStream is = null;
  49.                         try {
  50.                                 is = ucon.getInputStream();
  51.                         } catch (IllegalArgumentException i) {
  52.  
  53.                                 Log.i("MyLogger", "inside IEA" + i.toString());
  54.                         } catch (IOException i) {
  55.                                 Log.i("MyLogger", "inside IOE" + i.toString());
  56.                         }
  57.  
  58.                         Log.i("MyLogger", "After getInputStream");
  59.                         //BufferedInputStream bis = new BufferedInputStream(is);
  60.                         Log.i("MyLogger", is.toString());
  61.                        
  62.                         Reader reader = new InputStreamReader(is, "iso-8859-1");
  63.                         ByteArrayBuffer baf = new ByteArrayBuffer(250);
  64.                         int current = 0;
  65.                         while ((current = reader.read()) != -1) {
  66.                                 baf.append((byte) current);
  67.                         }
  68.  
  69.                         // Convert the Bytes read to a String.
  70.                         myString = new String(baf.toByteArray());
  71.  
  72.                         Log.i("MyLogger", myString);
  73.  
  74.                 } catch (Exception e) {
  75.                         /* On any Error we want to display it. */
  76.                         e.printStackTrace();
  77.                         Log.i("MyLogger", e.toString());
  78.                         myString = e.getMessage();
  79.                 }
  80.                
  81.                 /* Show the String on the GUI. */
  82.                 tv.setText(myString);
  83.                 this.setContentView(tv);
  84.         }
  85. }
  86.  
Parsed in 0.047 seconds, using GeSHi 1.0.8.4

Thanx for ur help.

Thanx,
Nitin
Nitinkcv
Developer
Developer
 
Posts: 29
Joined: Thu Nov 29, 2007 1:02 pm

Postby plusminus » Sun Jan 06, 2008 7:42 pm

Hello Nitin,

the first thing gets auto-converted by our browsers, I never got it displayed that way :(
Tried around some time now and it doesn't want to get displayed properly as we want it...

Seems like the encoding doesn't want to get applied (also tried with "iso-8859-1"),
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                                 /* We will show the data we read in a TextView. */
  2.                                 WebView wv = new WebView(this);
  3.                                 wv.getSettings().setDefaultTextEncodingName("utf-8");
  4.                                
  5.                                 /* Show the String on the GUI. */
  6.                                 wv.loadData(myString, "text/html", "utf-8");
  7.                                
  8.                                 this.setContentView(wv);
Parsed in 0.036 seconds, using GeSHi 1.0.8.4

as I can even type "hahahaha" as encoding and still get the same output (without exception)...

:( Is this a bug perhaps ???

I've put a file :src: here on this server, where you can browse the page we are talking about from your local browser and from your Android Browser.
Once Again, even the original browser is not capable of doing this :(

And another one ( :src: here) with explicitly set charset ( "charset=iso-8859-1" ) still no luck :evil:

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top
Next

Return to Novice Tutorials

Who is online

Users browsing this forum: Google Feedfetcher and 4 guests