Getting Data From the Web (URLConnection via http)

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

Postby Romeo Ordos » Wed Aug 12, 2009 5:47 pm

And how can I retrieve file size on the server before downloading it?
Romeo Ordos
Junior Developer
Junior Developer
 
Posts: 11
Joined: Thu Aug 06, 2009 6:30 pm
Location: Donetsk, Ukraine

Top

Postby nachiket » Wed Sep 09, 2009 7:07 am

Thank You Plusminus.... really helpful tutorial.
nachiket
Freshman
Freshman
 
Posts: 2
Joined: Wed Sep 09, 2009 4:37 am

Postby porzino » Thu Oct 08, 2009 3:24 pm

Hi,

I have try this solution and I have a problem: I only get the first 8K of the web (The size is 300K)

Any ideas?
porzino
Freshman
Freshman
 
Posts: 2
Joined: Thu Oct 08, 2009 3:17 pm

works with my REST

Postby stevebull » Wed Jan 06, 2010 5:15 am

Happy New Year 2010.

Getting Data From the Web (URLConnection via http) was a great tutorial...I got this working quickly and even produces interesting results with my REST script---check it out for yourself. REST is fast for me when I roll my own.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. /* Define the URL we want to load data from. */
  3.  
  4.                URL myURL = new URL(
  5.  
  6.                          // "http://www.anddev.org/images/tut/basic/getdatafromtheweb/loadme.txt"
  7.  
  8.                            // "http://iconic.4feets.com/update/"
  9.  
  10.                            "http://hollywood-usa.mobi/rest/site/index.php?siteID=15"
  11.  
  12.                            );
  13.  
  14.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


I look forward to reading more from plusminus. Thank you.

Cheers, Steve
stevebull
Freshman
Freshman
 
Posts: 3
Joined: Wed Jan 06, 2010 5:03 am
Location: New York City

Postby sbarnesi » Fri Jan 29, 2010 10:48 pm

blackpearl wrote:
cs_cckai wrote:Hi,

Actually, I have try your code, but it throw to exception with unknow error, I found that when it run
InputStream is = ucon.getInputStream();
then throw exception.

Can anyone tell me why?


Did you Specify Permission in AndroidManifest file? In the new version, permission needs to be granted.
Please add the following line in AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />

http://code.google.com/android/devel/security.html


You solved my problem! Thanks! :D
sbarnesi
Freshman
Freshman
 
Posts: 6
Joined: Wed Jan 27, 2010 11:46 pm

ProgressBar not working during file getting

Postby ianwong » Tue Feb 23, 2010 11:03 am

Hi, I have the below code using this tutorial code which works fine
in file getting. However, the progress bar does not update (actually
even Toast seems not working) during the file getting period.

As seen, I have log code and log does show the whole progress,
only the progress bar doesn't. The progress bar works fine in general
web surfing.

Can anyone point me which part I'd done wrongly? Thank You!

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import java.io.FileOutputStream;
  2.  
  3. import java.io.InputStream;
  4.  
  5. import java.net.HttpURLConnection;
  6.  
  7. import java.net.URL;
  8.  
  9. import android.app.Activity;
  10.  
  11. import android.os.Bundle;
  12.  
  13. import android.util.Log;
  14.  
  15. import android.webkit.WebChromeClient;
  16.  
  17. import android.webkit.WebView;
  18.  
  19. import android.webkit.WebViewClient;
  20.  
  21. import android.widget.ProgressBar;
  22.  
  23. import android.widget.Toast;
  24.  
  25.  
  26.  
  27. public class Downfile extends Activity {
  28.  
  29.     private static final String fext = ".bin";
  30.  
  31.  
  32.  
  33.     private WebView mWebView;
  34.  
  35.     private ProgressBar mProgress;
  36.  
  37.  
  38.  
  39.     //private Handler mHandler = new Handler();
  40.  
  41.  
  42.  
  43.     @Override
  44.  
  45.     public void onCreate(Bundle icicle){
  46.  
  47.         super.onCreate(icicle);
  48.  
  49.         setContentView(R.layout.downfile);
  50.  
  51.         mWebView = (WebView) findViewById(R.id.webview);
  52.  
  53.         mWebView.getSettings().setJavaScriptEnabled(true);
  54.  
  55.         mWebView.getSettings().setAllowFileAccess(true);
  56.  
  57.         mProgress = (ProgressBar) findViewById(R.id.pb);
  58.  
  59.        
  60.  
  61.         mWebView.setWebChromeClient(new WebChromeClient(){
  62.  
  63.                 @Override
  64.  
  65.                 public void onProgressChanged(WebView v, int progress){
  66.  
  67.                         mProgress.setProgress(progress);
  68.  
  69.                 }
  70.  
  71.         });
  72.  
  73.        
  74.  
  75. //        mWebView.loadUrl("http://www.google.com");
  76.  
  77.         mWebView.loadUrl(getString(R.string.dl_page));
  78.  
  79.        
  80.  
  81.         mWebView.setWebViewClient(new MyWebViewClient());
  82.  
  83.     }
  84.  
  85.    
  86.  
  87.     private class MyWebViewClient extends WebViewClient{
  88.  
  89.         @Override
  90.  
  91.         public boolean shouldOverrideUrlLoading(WebView view, String url){
  92.  
  93.                 mProgress.setProgress(0);
  94.  
  95.                 if (TestNeedFile(url)) testGet(url);
  96.  
  97.                 else view.loadUrl(url);
  98.  
  99.                
  100.  
  101.                 return true;
  102.  
  103.         }
  104.  
  105.        
  106.  
  107.         @Override
  108.  
  109.         public void onLoadResource(WebView view, String url){
  110.  
  111.                 view.setSaveEnabled(true);
  112.  
  113.                 //Toast.makeText(Downfile.this, url, Toast.LENGTH_SHORT).show();
  114.  
  115.         }
  116.  
  117.     }
  118.  
  119.    
  120.  
  121.     private boolean TestNeedFile (String link){
  122.  
  123.         String s = link.substring(link.length()-4, link.length());
  124.  
  125.         //Toast.makeText(Downfile.this, s, Toast.LENGTH_SHORT).show();         
  126.  
  127.                 if (s.equalsIgnoreCase(fext)) return true;
  128.  
  129.                 else return false;
  130.  
  131.     }
  132.  
  133.    
  134.  
  135.     private String Getfname (String link){
  136.  
  137.         return link.substring(link.lastIndexOf("/")+1);
  138.  
  139.     }
  140.  
  141.    
  142.  
  143.     private void testGet (String link){
  144.  
  145.         try{
  146.  
  147.                 String outfile = "/sdcard/mydir/" + Getfname(link);
  148.  
  149.                 Toast.makeText(Downfile.this, link, Toast.LENGTH_SHORT).show();        
  150.  
  151.                 URL url = new URL(link);
  152.  
  153.                 HttpURLConnection uCon = (HttpURLConnection) url.openConnection();
  154.  
  155.                 uCon.setRequestMethod("GET");
  156.  
  157.                 uCon.setDoOutput(true);
  158.  
  159.                 uCon.connect();
  160.  
  161.                 FileOutputStream fout = new FileOutputStream(outfile);
  162.  
  163.                 InputStream in = uCon.getInputStream();
  164.  
  165.                 int lengtotal = uCon.getContentLength();
  166.  
  167.                
  168.  
  169.                 byte[] Buffer = new byte[1024];
  170.  
  171.                 int leng = 0, lengnow=0;
  172.  
  173.                 double prog =0;
  174.  
  175.                 while ( (leng = in.read(Buffer)) != -1 ){
  176.  
  177.                         fout.write(Buffer,0,leng);
  178.  
  179.                         lengnow+=leng;
  180.  
  181.                         prog = 100 * ((double) lengnow / (double) lengtotal );
  182.  
  183.                         Log.d("File", "Progress = " + Integer.toString((int)prog));
  184.  
  185.                         mProgress.setProgress((int)prog);
  186.  
  187.                 }
  188.  
  189.                 fout.close();
  190.  
  191.                 in.close();
  192.  
  193.                
  194.  
  195.         } catch (Exception e){
  196.  
  197.                 Log.e("test", "This read is wrong");
  198.  
  199.         }
  200.  
  201.        
  202.  
  203.     }
  204.  
  205.    
  206.  
  207. }
  208.  
  209.  
Parsed in 0.045 seconds, using GeSHi 1.0.8.4
ianwong
Freshman
Freshman
 
Posts: 6
Joined: Fri Nov 20, 2009 7:46 am

Top

Postby yuriman » Wed Mar 17, 2010 3:48 pm

-snip-
forgot to add internet access in manifest
yuriman
Junior Developer
Junior Developer
 
Posts: 15
Joined: Mon Mar 01, 2010 2:09 pm

Postby ianwong » Thu Mar 18, 2010 3:53 pm

yuriman wrote:-snip-
forgot to add internet access in manifest


nope and I believe if internet access permission does not enabled in manifest, it should result error loading page instead of holding everything during download.

anyway, problem solved by creating new thread for each file it downloads.
ianwong
Freshman
Freshman
 
Posts: 6
Joined: Fri Nov 20, 2009 7:46 am

Postby yuriman » Thu Mar 18, 2010 4:14 pm

ianwong wrote:
yuriman wrote:-snip-
forgot to add internet access in manifest


nope and I believe if internet access permission does not enabled in manifest, it should result error loading page instead of holding everything during download.

anyway, problem solved by creating new thread for each file it downloads.

what?
I solved the problem!
I created a thread couse i want to get mysql stuff to the phone in a table...
And it was the manifest, couse it worked after i added internet access!
yuriman
Junior Developer
Junior Developer
 
Posts: 15
Joined: Mon Mar 01, 2010 2:09 pm

Postby DRJTower » Thu Mar 18, 2010 7:06 pm

Is there a tutorial anywhere for doing this in a background service?

I want my activities to be able to communicate with a background service that handle the network (I am using sockets not URLs).

I have already tried putting all my network code in a thread created by the activity (the way I always coded on PC)
but then found out that when an activity is stopped so is any created threads, so I need to do this in a service.
DRJTower
Freshman
Freshman
 
Posts: 4
Joined: Thu Mar 18, 2010 6:35 am

sending data to the server

Postby strog » Sat Mar 27, 2010 8:50 am

Hi peeps,
The code is really good.
But what if i would like to write stuff to my server?
I added the following line:
OnputStream os = urlConn.getOutputStream();
and when I have a string (which is in the json format) to be written, nothing actually happens on the other side...
I have the port on 810 set for technical reasons of my project that i want to implement.
In the attachment you will find a .Net Programm that is the server side where I have json objects stored.
Any ideas about why nothing gets to the server?
strog
Freshman
Freshman
 
Posts: 2
Joined: Sat Nov 21, 2009 11:58 am

Cookie Problems

Postby los93sol » Sat Mar 27, 2010 7:50 pm

Hello, firstly, this is a great tutorial, helped me out a lot with the first site I tried to run it against.

I'm having a problem now because the site I'm trying to get the source from requires cookies, and I've been all over trying to get my head around what method I should be using to retrieve the html source from a site that requires cookies be enabled.

Thanks in advance.
los93sol
Freshman
Freshman
 
Posts: 3
Joined: Sat Mar 27, 2010 7:47 pm

Postby imac » Mon Mar 29, 2010 4:09 pm

Thank you for your tutorial. Is helping me a lot.
Tel me please, what if I need to provide an email address and password to get data ?
I am trying to login to Google Adsense and retrieve last data.
Thank you again for your help.
imac
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Mar 29, 2010 4:05 pm
Location: Romania

Re:

Postby nioupy » Sun May 23, 2010 4:05 pm

Berzerk wrote: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.032 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


Thanks Berzek, you helped me a lot with this line of code !!!
It took me a few tries though to understand that my source was encoded weirdly (windows-1251??) but this plus plusminus code works like a charm !

Thanks both of you :)
nioupy
Developer
Developer
 
Posts: 36
Joined: Sat Apr 03, 2010 1:34 pm

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

Postby aashishkotagiri » Sat Jul 03, 2010 11:17 am

im getting this "permission denied" error every time i try to acess any url

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3. public class screen2 extends Activity implements OnClickListener {
  4.        
  5.         @Override
  6.         public void onCreate(Bundle instance)
  7.         {
  8.                 super.onCreate(instance);
  9.                 setContentView(R.layout.view2);
  10.                
  11.                 Button bu = (Button)findViewById(R.id.exit);
  12.                 bu.setOnClickListener(this);
  13.                
  14.                 Button bu1 = (Button)findViewById(R.id.but);
  15.                
  16.                 bu1.setOnClickListener(this);
  17.         }
  18.        
  19.         public void onClick(View v)
  20.         {
  21.                 TextView tv = (TextView) findViewById(R.id.txt);
  22.                
  23.          if(v.getId()==R.id.exit)
  24.          {
  25.                 tv.setText("you clicked exit");
  26.                
  27.                 Intent i = new Intent(this,tablelayout.class);
  28.                 startActivity(i);
  29.                
  30.          }
  31.          
  32.          if(v.getId()==R.id.but)
  33.          {
  34.                  String myString = null;
  35.          try {
  36.                
  37.                  URL myURL = new URL("http://www.ssis.co.in/ongc");
  38.                
  39.                  URLConnection ucon = myURL.openConnection();
  40.  
  41.                
  42.                  InputStream is = ucon.getInputStream();
  43.                  BufferedInputStream bis = new BufferedInputStream(is);
  44.                  
  45.                  
  46.                  ByteArrayBuffer baf = new ByteArrayBuffer(50);
  47.                  int current = 0;
  48.                  while((current = bis.read()) != -1){
  49.                          baf.append((byte)current);
  50.                  }
  51.  
  52.                
  53.                  myString = new String(baf.toByteArray());
  54.          } catch (Exception e) {
  55.                
  56.                  myString = e.getMessage();
  57.          }
  58.        
  59.          tv.setText(myString);    
  60.                    
  61.                  //tv.setText("you clicked but");
  62.          }
  63.          
  64.          
  65.         }
  66.  
  67. }
  68.  
  69.  
Parsed in 0.041 seconds, using GeSHi 1.0.8.4



PlusMinus Please help me
aashishkotagiri
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Jan 19, 2010 7:59 am

Top
PreviousNext

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 6 guests