Getting Data From the Web (URLConnection via http)

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

Re: Getting Data From the Web (URLConnection via http)

Postby WynixToo » Wed Jul 07, 2010 2:48 am

Hi Plusminus,

i had test the code and it run success in Android 2.2. but if i run it on Android 1.1 it has error.

The Error :

"Host is unresolved: www.anddev.org:80"

i was add the user-permission. and the "Build Target = 8" , "minSDKversion = 2"

it can run in any version of Android except Android 1.1

is Android 1.1 no support internet? or it is any reason.

P/S: Sorry Im not very well in English grammer. Hope you understands what i'm talking about.

Thanks you.

Wynix
WynixToo
Junior Developer
Junior Developer
 
Posts: 16
Joined: Fri Jul 02, 2010 10:36 am
Location: Malaysia

Top

Re: Getting Data From the Web (URLConnection via http)

Postby anarche » Wed Jul 07, 2010 3:09 am

I wouldn't worry about testing on 1.1, there aren't any phones in the wild running it.

Test 1.5 and up.

As always correct me if I'm wrong.
anarche
Master Developer
Master Developer
 
Posts: 369
Joined: Sun Apr 11, 2010 5:21 am

Re: Getting Data From the Web (URLConnection via http)

Postby sanju7216 » Fri Jul 09, 2010 11:29 pm

Hi,

I have problem when i use my google app url which is deployed in the GAE.
the output is blank. I don't get any errors.
Code: Select all
[syntax="java"]
package cw.and.datafromweb;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;

import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpConnection;
import org.apache.commons.httpclient.HttpConnectionManager;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.HttpURL;
import org.apache.commons.httpclient.SimpleHttpConnectionManager;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.http.util.ByteArrayBuffer;
import org.apache.http.util.EncodingUtils;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class getdatafromweb extends Activity {
    /** Called when the activity is first created. */
   
   protected static HttpConnectionManager connectionManager = new  SimpleHttpConnectionManager();
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView tv = new TextView(this);
        HttpConnection connection = null;
        String myString = null;
        String url = "http://xxxxxxx.appspot.com/show?query=select%20*%20from%20Trial%20where%20name%20=%20'sanjay'%20limit%201";
       
        try{            
           HttpURL httpURL = new HttpURL(url);
           HostConfiguration host = new HostConfiguration();
           host.setHost(httpURL.getHost(), httpURL.getPort());
           connection = connectionManager.getConnection(host);
           connection.open();
           PostMethod postMethod = new PostMethod(url);
            postMethod.execute(new HttpState(), connection);
            InputStream is = postMethod.getResponseBodyAsStream();
                                  
           BufferedInputStream bis = new BufferedInputStream(is);
           ByteArrayBuffer baf = new ByteArrayBuffer(450);
           int current = 0;
           while((current = bis.read()) != -1){
              baf.append((byte)current);
           }
           myString = EncodingUtils.getString(baf.toByteArray(), "ISO-8859-1");
           bis.close();
        }catch(Exception e){
           myString = e.getMessage();
        }finally{
           connection.close();
        }
        tv.append(myString);
        this.setContentView(tv);
    }
}[/syntax]
Code: Select all


I don know what's wrong. Please help me with this.

Thanks
sanju7216
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Jul 09, 2010 11:19 pm

Re: Getting Data From the Web (URLConnection via http)

Postby Amires » Thu Jul 15, 2010 2:37 pm

hey +- ,
I was wondering if it is possible to run this code on an emulator
-Amine
plusminus wrote:
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.035 seconds, using GeSHi 1.0.8.4

Thats it :)

Regards,
plusminus
Amires
Freshman
Freshman
 
Posts: 5
Joined: Wed Jul 14, 2010 4:51 pm

Re: Getting Data From the Web (URLConnection via http)

Postby LouerAdun » Thu Jul 22, 2010 2:30 am

I have tried this code exactly as it was written other than changing the package name to match my code and I seem to get "Permission Denied" every time it is executed, even when changing the web address. I have been testing on the emulator using 2.2. Any help would be appreciated, as getting web requests working is integral to the application.

Aha, I figured it out, needed to add the INTERNET permission to the manifest, sorry I am a real newbie coding for the Android platform.
LouerAdun
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Jul 22, 2010 2:13 am

Re: Getting Data From the Web (URLConnection via http)

Postby Mikey30000 » Tue Aug 17, 2010 11:18 pm

If anyone is getting:
"Host Is Unresolved" errors;
"Permission Denied" errors;
Blank text instead of a web page;
No recognition when typing in URLs

Just make sure you add the following permissions to your AndroidManifest.xml under the <manifest...>:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>

Hope this helps any newcomers!
Mikey30000
Freshman
Freshman
 
Posts: 2
Joined: Tue Aug 10, 2010 6:46 pm

Top

Re: Getting Data From the Web (URLConnection via http)

Postby Leucistic » Fri Aug 20, 2010 12:28 pm

Hi plusminus,

I'm using your code, which works perfectly when I want to download text files.
But, I want to download a 9Mo size .mp4 video, and it doesn't work.


It results of a Exception :

08-20 12:57:35.381: ERROR/AndroidRuntime(2484): Uncaught handler: thread AsyncTask #1 exiting due to uncaught exception
08-20 12:57:35.381: ERROR/AndroidRuntime(2484): java.lang.RuntimeException: An error occured while executing doInBackground()
08-20 12:57:35.381: ERROR/AndroidRuntime(2484): at android.os.AsyncTask$3.done(AsyncTask.java:200)
08-20 12:57:35.381: ERROR/AndroidRuntime(2484): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:234)
08-20 12:57:35.381: ERROR/AndroidRuntime(2484): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:258)
08-20 12:57:35.381: ERROR/AndroidRuntime(2484): at java.util.concurrent.FutureTask.run(FutureTask.java:122)
08-20 12:57:35.381: ERROR/AndroidRuntime(2484): at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:648)
08-20 12:57:35.381: ERROR/AndroidRuntime(2484): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:673)
08-20 12:57:35.381: ERROR/AndroidRuntime(2484): at java.lang.Thread.run(Thread.java:1060)
08-20 12:57:35.381: ERROR/AndroidRuntime(2484): Caused by: java.lang.OutOfMemoryError
08-20 12:57:35.381: ERROR/AndroidRuntime(2484): at org.apache.http.util.ByteArrayBuffer.expand(ByteArrayBuffer.java:57)
08-20 12:57:35.381: ERROR/AndroidRuntime(2484): at org.apache.http.util.ByteArrayBuffer.append(ByteArrayBuffer.java:84)




Do you know how to download large files (~100Mo) ???


Thanks in advance.
Leucistic
Junior Developer
Junior Developer
 
Posts: 19
Joined: Mon Jul 26, 2010 2:13 pm

Re: Getting Data From the Web (URLConnection via http)

Postby g33k499 » Mon Sep 20, 2010 6:45 pm

How would you read each line of the returned data?
g33k499
Freshman
Freshman
 
Posts: 9
Joined: Fri Dec 18, 2009 10:23 pm

Re: Getting Data From the Web (URLConnection via http)

Postby zOro » Mon Dec 06, 2010 11:43 pm

Hi plusminus,
did you find solution with the charset issue,
I am trying to figure the same thing while loading Hebrew into webview but get no luck with it.
the funny thing is that when using 1.6 and upper version webview work OK with the my spesiffic charset....
zOro
Freshman
Freshman
 
Posts: 4
Joined: Thu Mar 11, 2010 12:36 pm

Re: Getting Data From the Web (URLConnection via http)

Postby Leucistic » Mon Dec 20, 2010 11:53 am

I've found the solution to my problem.
I give you the method :
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public boolean DownloadFromUrl(String fileUrl, String fileName, String pathToSave)
  2.         {
  3.                 try
  4.                 {
  5.                         URL url = new URL(fileUrl+fileName);
  6.                         File file = new File(pathToSave+fileName);
  7.                        
  8.                         long startTime = System.currentTimeMillis();
  9.                         Log.d("Debug", "download url:" + url);
  10.                         Log.d("Debug", "downloaded file name:" + pathToSave+fileName);
  11.                        
  12.                     HttpURLConnection c = (HttpURLConnection) url.openConnection();
  13.                     c.setRequestMethod("GET");
  14.                     c.setDoOutput(true);
  15.                     c.connect();
  16.                     FileOutputStream f = new FileOutputStream(file);
  17.  
  18.                     InputStream in = c.getInputStream();
  19.  
  20.                     byte[] buffer = new byte[1024];
  21.                     int len1 = 0;
  22.                     while ( (len1 = in.read(buffer)) > 0 ) {
  23.                          f.write(buffer, 0, len1);
  24.                     }
  25.                     f.close();
  26.                     in.close();
  27.                    
  28.                     Log.d("Debug", "download ready in"
  29.                                         + ((System.currentTimeMillis() - startTime) / 1000)
  30.                                         + " sec");
  31.                                         Log.d("Debug", " ");
  32.                 }
  33.                 catch (FileNotFoundException fnfe)
  34.                 {
  35.                         Log.e("Debug", "File Not Found: " + fnfe);
  36.                         return false;
  37.                 }
  38.                 catch (IOException e) {Log.e("Debug", "Error: " + e);}
  39.                
  40.                 return true;
  41.         }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


Here is the explanation found on stackOverflow
http://stackoverflow.com/questions/576513/android-download-binary-file-problems

I don't know if it's the only problem, but you've got a classic Java glitch in there: You're not counting on the fact that read() is always allowed to return fewer bytes than you ask for. Thus, your read could get less than 1024 bytes but your write always writes out exactly 1024 bytes possibly including bytes from the previous loop iteration.

Correct with:

Code: Select all
while ( (len1 = in.read(buffer)) > 0 ) {
         f.write(buffer,0, len1);

}



Hope this helps someone.
Leucistic
Junior Developer
Junior Developer
 
Posts: 19
Joined: Mon Jul 26, 2010 2:13 pm

Re: Getting Data From the Web (URLConnection via http)

Postby clege » Thu Dec 23, 2010 6:24 pm

Thanks a lot for this tutorial.
I have test it with a long txt file. I don't see all of the text and it is not possible to scroll.
How it is possible to scroll if the txt file is too long ?
clege
Freshman
Freshman
 
Posts: 2
Joined: Thu Dec 23, 2010 6:20 pm

Re: Getting Data From the Web (URLConnection via http)

Postby darth_sidious » Mon Dec 27, 2010 10:06 am

I simply get the text
"www.anddev.org"
I use at API Level 9. Has the file here changed or something?
darth_sidious
Freshman
Freshman
 
Posts: 4
Joined: Mon Dec 27, 2010 8:36 am

Re: Getting Data From the Web (URLConnection via http)

Postby pshirishreddy » Fri Feb 04, 2011 10:39 am

Just to add the problem I have faced, IF you get permission error for Internet
Edit your android manifest xml file to add the following line
<uses-permission android:name="android.permission.INTERNET" />
pshirishreddy
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Feb 04, 2011 8:31 am

Re: Getting Data From the Web (URLConnection via http)

Postby joshmetran » Wed Nov 02, 2011 6:18 am

Hi To All Professionals out there,

I'm a newbie android developer. Can i access the URL with basic authentication using plusminus codes posted on the first page? If yes.. How?

Thanks in advance...
joshmetran
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Nov 02, 2011 4:55 am

Re: Getting Data From the Web (URLConnection via http)

Postby Qubiz » Thu Nov 03, 2011 3:24 pm

I want to control a pan 'n tilt ip camera by using an app, the url command to turn the camera to the right is: http://fizix.dyndns.org/decoder_control.cgi?command=6.

So, in my app, when I click the button that says ' turn right', it should execute that url.

I have no problems making the buttons etc. I am just wondering how to execute the URL.

Can anyone help me out here?

~Qubiz
Qubiz
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Nov 03, 2011 2:52 pm

Top
PreviousNext

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 7 guests