Getting Data From the Web (URLConnection via http)

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

Postby Klian » Wed Feb 25, 2009 12:21 pm

Ive tried the example code and all I get is (see attached image)


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package org.klian.android.GetDataFromTheWeb;
  3.  
  4.  
  5.  
  6. import android.app.Activity;
  7.  
  8. import android.os.Bundle;
  9.  
  10.  
  11.  
  12. import java.io.BufferedInputStream;
  13.  
  14. import java.io.InputStream;
  15.  
  16. import java.net.URL;
  17.  
  18. import java.net.URLConnection;
  19.  
  20.  
  21.  
  22. import org.apache.http.util.ByteArrayBuffer;
  23.  
  24. import android.widget.TextView;
  25.  
  26.  
  27.  
  28. public class GetDataFromTheWeb extends Activity {
  29.  
  30.     /** Called when the activity is first created. */
  31.  
  32.     @Override
  33.  
  34.     public void onCreate(Bundle savedInstanceState) {
  35.  
  36.  
  37.  
  38.         /* We will show the data we read in a TextView. */
  39.  
  40.         TextView tv = new TextView(this);
  41.  
  42.          
  43.  
  44.         /* Will be filled and displayed later. */
  45.  
  46.         String myString = null;
  47.  
  48.         try {
  49.  
  50.              /* Define the URL we want to load data from. */
  51.  
  52.              URL myURL = new URL(
  53.  
  54.                        "http://www.tortuganinja.com");
  55.  
  56.              /* Open a connection to that URL. */
  57.  
  58.              URLConnection ucon = myURL.openConnection();
  59.  
  60.  
  61.  
  62.              /* Define InputStreams to read
  63.  
  64.               * from the URLConnection. */
  65.  
  66.              InputStream is = ucon.getInputStream();
  67.  
  68.              BufferedInputStream bis = new BufferedInputStream(is);
  69.  
  70.              
  71.  
  72.              /* Read bytes to the Buffer until
  73.  
  74.               * there is nothing more to read(-1). */
  75.  
  76.              ByteArrayBuffer baf = new ByteArrayBuffer(50);
  77.  
  78.              int current = 0;
  79.  
  80.              while((current = bis.read()) != -1){
  81.  
  82.                   baf.append((byte)current);
  83.  
  84.              }
  85.  
  86.  
  87.  
  88.              /* Convert the Bytes read to a String. */
  89.  
  90.              myString = new String(baf.toByteArray());
  91.  
  92.         } catch (Exception e) {
  93.  
  94.              /* On any Error we want to display it. */
  95.  
  96.              myString = e.getMessage();
  97.  
  98.         }
  99.  
  100.         /* Show the String on the GUI. */
  101.  
  102.         tv.setText(myString);
  103.  
  104.         this.setContentView(tv);
  105.  
  106.     }
  107.  
  108. }
  109.  
  110.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4
Attachments
android-error.jpg
android-error.jpg (29.6 KiB) Viewed 20732 times
Klian
Freshman
Freshman
 
Posts: 4
Joined: Wed Feb 18, 2009 8:30 pm

Top

Postby kali » Thu Feb 26, 2009 7:31 am

hai friend is it possible to brows gallary from code
kali
Experienced Developer
Experienced Developer
 
Posts: 62
Joined: Tue Jan 27, 2009 1:31 pm

Postby alienmanfc6 » Fri Feb 27, 2009 7:31 pm

Hello there,
I am using this code as a start to my app that will get info from a html file. What i need to know is, is there a way to adjust the time out on the web page request?
Thanks
alienmanfc6
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Feb 26, 2009 11:24 pm
Location: Dekalb, IL

Postby metachris » Tue Mar 03, 2009 9:45 am

Hey!

Some of you might have noticed that fetching data from the web blocks the GUI, even if you do it in a handler.

Here is how to do the same thing non-blocking, using a Thread. Threads on the other side cannot interact with the GUI (not even Toast), so we'll be posting a Handler that can do that:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class Iconic extends Activity {
  2.     private String html = "";
  3.     private Handler mHandler;
  4.  
  5.     public void onCreate(Bundle savedInstanceState) {
  6.         super.onCreate(savedInstanceState);
  7.         setContentView(R.layout.main);
  8.         mHandler = new Handler();
  9.         checkUpdate.start();
  10.     }
  11.  
  12.     private Thread checkUpdate = new Thread() {
  13.         public void run() {
  14.             try {
  15.                 URL updateURL = new URL("http://iconic.4feets.com/update");
  16.                 URLConnection conn = updateURL.openConnection();
  17.                 InputStream is = conn.getInputStream();
  18.                 BufferedInputStream bis = new BufferedInputStream(is);
  19.                 ByteArrayBuffer baf = new ByteArrayBuffer(50);
  20.  
  21.                 int current = 0;
  22.                 while((current = bis.read()) != -1){
  23.                     baf.append((byte)current);
  24.                 }
  25.  
  26.                 /* Convert the Bytes read to a String. */
  27.                 html = new String(baf.toByteArray());
  28.                 mHandler.post(showUpdate);
  29.             } catch (Exception e) {
  30.             }
  31.         }
  32.     };
  33.  
  34.     private Runnable showUpdate = new Runnable(){
  35.         public void run(){
  36.             Toast.makeText(Iconic.this, "HTML Code: " + html, Toast.LENGTH_SHORT).show();
  37.         }
  38.     };
  39. }
  40.  
Parsed in 0.043 seconds, using GeSHi 1.0.8.4


Hope some of you can use this code! This was originally posted on 4feets.com

- Chris
metachris
Junior Developer
Junior Developer
 
Posts: 24
Joined: Sat Feb 07, 2009 11:40 am

Postby Berzerk » Tue Mar 10, 2009 5:56 am

If anyone is still having problems with character encoding, character set, unicode, etc...

Here is what worked for me:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. myString = EncodingUtils.getString(baf.toByteArray(), "ISO-8859-1");
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


That line replaced the original line in the GetDataFromWeb tutorial.

The characterset "ISO-8859-1" works perfect for Portuguese and similar languages. For other languages, look up the appropriate characterset and replace it.

Don't forget to include:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import org.apache.http.util.EncodingUtils;
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


-Berzerk
Berzerk
Freshman
Freshman
 
Posts: 3
Joined: Tue Mar 10, 2009 5:50 am

error

Postby rahulp » Tue Mar 17, 2009 8:03 am

hi.. plusminus
when i tried running ur code..i got an "unknown error
rahul phadnis
rahulp
Developer
Developer
 
Posts: 30
Joined: Thu Feb 19, 2009 12:52 pm

Top

Postby cymon » Fri Mar 27, 2009 1:21 am

@rahulp

Add this to your manifest file: <uses-permission android:name="android.permission.INTERNET" />
cymon
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Mar 27, 2009 1:16 am

How about mp3/pdf/jpegs ??

Postby sujay0000 » Thu Apr 16, 2009 5:40 pm

Hello;

Can someone tell me how do we get the same thing working with other file formats and save them in the default location??

like if I wanna download something from


mydomain.com/music/abcd.mp3

so what modification in this code could help us save this abcd.mp3 on the default laction on our phone?
sujay0000
Freshman
Freshman
 
Posts: 3
Joined: Sun Mar 29, 2009 9:06 am

servlet

Postby rahulp » Fri Apr 24, 2009 10:12 am

hi.. i want to download a file from a server. where i can send the name of the file to download.. how can i send this to my server,, also how to retrieve this filename at the server
rahul phadnis
rahulp
Developer
Developer
 
Posts: 30
Joined: Thu Feb 19, 2009 12:52 pm

Postby glory » Mon Apr 27, 2009 12:02 pm

Hi Plusminus,

When I tried to run your code then I got unknown Error as output

I have also given permission in the Android manifest file

And when I tried to do HTTP Connection then i Got the follwing error

04-27 16:38:59.296: ERROR/OSNetworkSystem(326): unknown socket error -1

Please help me out , I have given the appropiate Permission in the Manifest in the file
Deepak Kumar Singh
glory
Freshman
Freshman
 
Posts: 5
Joined: Fri Feb 08, 2008 1:49 pm
Location: India

Postby myshoes » Mon May 04, 2009 2:11 am

Can someone verify whether webview is working with the 1.5 sdk? Not even the sample provided on the google code page works as of today (5/3).
myshoes
Junior Developer
Junior Developer
 
Posts: 12
Joined: Mon May 04, 2009 2:07 am

Postby desuvinodkumar » Wed Jun 17, 2009 7:04 am

plusminus wrote:Hello venkat,

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


Regards,
plusminus




HI PLUS MINUS

i need to query the webservices
how can i do it

i need to downnload images for the PHp (nusoap) server how can i do it
Vinod...
User avatar
desuvinodkumar
Senior Developer
Senior Developer
 
Posts: 102
Joined: Mon Mar 23, 2009 6:51 am
Location: India

How to get data from PHP server

Postby desuvinodkumar » Tue Jun 23, 2009 7:51 am

Hi

I need to get data from PHP server and should return that data in Xml format How can i do that

thanks in advance
Vinod...
User avatar
desuvinodkumar
Senior Developer
Senior Developer
 
Posts: 102
Joined: Mon Mar 23, 2009 6:51 am
Location: India

Postby sedimental » Tue Jul 21, 2009 7:50 am

Hi guys, can anyone help me with this error? I have no idea how to solve it!
Attachments
Error.jpg
Error.jpg (21.36 KiB) Viewed 18608 times
sedimental
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Jul 21, 2009 7:33 am

Postby dapaintballer331 » Sat Aug 08, 2009 10:26 pm

read what the screen says.... add that permission to android manifest... wow.

Eclipse can do this, or read the 4-20 posts in this thread, specifying where, what, and how to add the permission.
dapaintballer331
Junior Developer
Junior Developer
 
Posts: 11
Joined: Wed Jul 01, 2009 2:03 am

Top
PreviousNext

Return to Novice Tutorials

Who is online

Users browsing this forum: Majestic-12 [Bot] and 4 guests