getting data from the web

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

getting data from the web

Postby Lex » Thu Dec 20, 2007 2:28 pm

Hi, I'm trying to get data from a web page, I tried the following code but its not working (its compiling but I get an empty screen when I launch the activity );

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.google.android.Test;
  2.  
  3.  
  4.  
  5. import java.io.InputStream;
  6.  
  7. import java.net.HttpURLConnection;
  8.  
  9. import java.net.URL;
  10.  
  11.  
  12.  
  13. import android.app.Activity;
  14.  
  15. import android.os.Bundle;
  16.  
  17. import android.widget.TextView;
  18.  
  19.  
  20.  
  21.  
  22.  
  23. public class webCheck extends Activity
  24.  
  25. {
  26.  
  27.        
  28.  
  29.         private TextView vText;
  30.  
  31.         @Override
  32.  
  33.         public void onCreate(Bundle icicle)
  34.  
  35.         {
  36.  
  37.                 super.onCreate(icicle);
  38.  
  39.                 setContentView(R.layout.webCheck);
  40.  
  41.                 vText = (TextView) findViewById(R.id.text1);
  42.  
  43.                 try{
  44.  
  45.             URL url = new URL("http://localhost/lab/get.php");
  46.  
  47.             HttpURLConnection http =(HttpURLConnection)url.openConnection();
  48.  
  49.             http.setRequestMethod("GET");
  50.  
  51.             http.connect();
  52.  
  53.             InputStream in = http.getInputStream();
  54.  
  55.             byte by[] = new byte[1024];
  56.  
  57.             in.read(by);
  58.  
  59.             String myString = new String();
  60.  
  61.             myString = in.toString();
  62.  
  63.             in.close();
  64.  
  65.             http.disconnect();
  66.  
  67.             vText.setText(myString);
  68.  
  69.  
  70.  
  71.         }
  72.  
  73.                 catch(Exception e)
  74.  
  75.                 {
  76.  
  77.         }
  78.  
  79.                
  80.  
  81.                
  82.  
  83.         }
  84.  
  85. }
  86.  
  87.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


Whats wrong with my code ? How would u do that ?
Thanks
Lex
Developer
Developer
 
Posts: 30
Joined: Fri Nov 16, 2007 11:03 pm

Top

Postby plusminus » Thu Dec 20, 2007 3:49 pm

Hello Lex,

the toString()-method does not return the data it read :!:
Instead use the following code:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. // Replace:
  2. myString = in.toString();
  3. // With:
  4. myString = new String(by);
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Should be working now.

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 Lex » Fri Dec 21, 2007 11:31 am

Thanks but it still doesn't work : it compiles and runs, but nothing shows up.
Lex
Developer
Developer
 
Posts: 30
Joined: Fri Nov 16, 2007 11:03 pm

Postby Danuubz » Fri Dec 21, 2007 1:19 pm

I've just tried to implement a code example from O'Reillys 'Java Network Programming' book, but the URLConnection routine doesn't work. Simply the white screen as you already described and the 'not responding' message few seconds after....
User avatar
Danuubz
Experienced Developer
Experienced Developer
 
Posts: 78
Joined: Wed Dec 19, 2007 10:44 pm
Location: Germany

Postby plusminus » Fri Dec 21, 2007 5:05 pm

Hey guys,

I used the following Code to load an image from the web, for the :src: Google-Weather-API Tutorial i'll post later this day :)

And it works ^^:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. URL myURL= new URL("http://www.google.com/images/weather/sunny.gif");
  2. URLConnection conn = myURL.openConnection();
  3. conn.connect();
  4. InputStream is = conn.getInputStream();
Parsed in 0.033 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 Danuubz » Fri Dec 21, 2007 6:17 pm

Aah yes it works. You sometimes just have to wait about 15-20 seconds ^^

Here's my code which downloads the first 20 tokens of this forum thread :lol:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.google.android.webcheck;
  3.  
  4.  
  5.  
  6. import java.net.*;
  7.  
  8. import java.io.*;
  9.  
  10.  
  11.  
  12. import android.app.Activity;
  13.  
  14. import android.os.Bundle;
  15.  
  16. import android.widget.TextView;
  17.  
  18.  
  19.  
  20. public class WebCheck extends Activity
  21.  
  22. {
  23.  
  24.         String myString = "Test: ";
  25.  
  26.    
  27.  
  28.     @Override
  29.  
  30.     public void onCreate(Bundle icicle)
  31.  
  32.     {
  33.  
  34.         super.onCreate(icicle);
  35.  
  36.        
  37.  
  38.         TextView tv = new TextView(this);
  39.  
  40.        
  41.  
  42.         try
  43.  
  44.         {
  45.  
  46.                 URL myURL = new URL("http://www.anddev.org/getting_data_from_the_web-t330.html");
  47.  
  48.                 URLConnection ucon = myURL.openConnection();
  49.  
  50.                
  51.  
  52.                 InputStream is = ucon.getInputStream();
  53.  
  54.                 InputStream bis = new BufferedInputStream(is);
  55.  
  56.                 Reader reader = new InputStreamReader(bis);
  57.  
  58.                
  59.  
  60.                 int x;
  61.  
  62.                 int count = 0;  
  63.  
  64.                 byte by[] = new byte[20];
  65.  
  66.                 while((x = reader.read()) != -1)
  67.  
  68.                 {
  69.  
  70.                         if(count < 20)
  71.  
  72.                         {
  73.  
  74.                                 by[count] = (byte) x;
  75.  
  76.                         }
  77.  
  78.                         count++;
  79.  
  80.                 }
  81.  
  82.                 for(int i=0; i<20; i++)
  83.  
  84.                 {
  85.  
  86.                         myString = myString + (char)by[i];
  87.  
  88.                 }
  89.  
  90.         }
  91.  
  92.         catch(MalformedURLException ex)
  93.  
  94.         {
  95.  
  96.                 myString = "MalformedURLException occured.";
  97.  
  98.         }
  99.  
  100.         catch(IOException ex)
  101.  
  102.         {
  103.  
  104.                 myString = "IOException occured.";
  105.  
  106.         }
  107.  
  108.        
  109.  
  110.         tv.setText(myString);
  111.  
  112.         setContentView(tv);
  113.  
  114.     }
  115.  
  116. }
  117.  
  118.  
Parsed in 0.041 seconds, using GeSHi 1.0.8.4


Greets, Danuubz
User avatar
Danuubz
Experienced Developer
Experienced Developer
 
Posts: 78
Joined: Wed Dec 19, 2007 10:44 pm
Location: Germany

Top

Postby plusminus » Fri Dec 21, 2007 9:11 pm

=D fine

Note: there are some shorter functions to achieve the same as you are (probably fast-coded :) )

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. is.read(<byte[] buf>);
  2. String s = new String(<byte[] buf>);
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

Postby Lex » Fri Dec 21, 2007 10:42 pm

Allright thank you, now it works !
( Although the app doesn't work when the data is on http://localhost/ ... is there any way to make it work ? )
Lex
Developer
Developer
 
Posts: 30
Joined: Fri Nov 16, 2007 11:03 pm

Postby venkat » Sat Dec 22, 2007 8:26 am

Hi Danuubz,
This is what i am looking for, thanks for you tutorial :). when i ran ur code i got IOException.

Actual Exception is "java.net.unknownHostException: Host is unresolved:www.anddev.org:80", i have attached my Screen shot.
i am behind the proxy and my port number is 3128.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.    URL myURL = new URL("http://www.anddev.org/getting_data_from_the_web-t330.html");
  2.                            URLConnection ucon = myURL.openConnection();              
  3.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4

In above code i found that , using myURL.openConnection() we can set
proxy.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. myURL.openConnection(proxy)
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
. I tried to set proxy settings, but i can't. can u tell me how set proxy from coding?

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

Postby Danuubz » Sat Dec 22, 2007 12:49 pm

Hello venkat,

You can try to add
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. System.setProperty("http.proxyHost"," **IP_OF_YOUR_PROXY** ");
  3.  
  4. System.setProperty("http.proxyPort","3128");
  5.  
  6.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4

to the code (before the try-catch-block).
User avatar
Danuubz
Experienced Developer
Experienced Developer
 
Posts: 78
Joined: Wed Dec 19, 2007 10:44 pm
Location: Germany

Postby venkat » Sat Dec 22, 2007 3:35 pm

Dear Danuubz,:)
Thanks for ur reply, It Works but, now i got new Error, I think i need to set authentication. Can u tell me how to set authentication. i have attached Screen shot of authentication.


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

Postby Danuubz » Sat Dec 22, 2007 3:56 pm

Hmm... maybe it works properly if you choose another URL... Try with http://www.google.de/index.html :wink:
User avatar
Danuubz
Experienced Developer
Experienced Developer
 
Posts: 78
Joined: Wed Dec 19, 2007 10:44 pm
Location: Germany

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

Hey guys,

transformed this issue into its :src: own tutorial (click).

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 4:44 pm

Hi Danuubz, i have changed URL but same error is repeating.:(.
I have set user name and password also using following lines of code.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 System.setProperty("http.proxyUser", "venkat");
  2.  
  3.                 System.setProperty("http.proxyPassword", "password");
Parsed in 0.036 seconds, using GeSHi 1.0.8.4




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

Postby pramod_taragi » Mon Nov 17, 2008 6:36 am

Hi,
I am facing same problem of :Host is unresolved.
Please help me out.
pramod_taragi
Junior Developer
Junior Developer
 
Posts: 14
Joined: Fri Nov 14, 2008 1:03 pm

Top
Next

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 2 guests