Strict Standards: Non-static method utf_normalizer::nfc() should not be called statically in /www/htdocs/w006661d/anddev/includes/utf/utf_tools.php on line 1781
[phpBB Debug] PHP Notice: in file /includes/session.php on line 1007: Cannot modify header information - headers already sent by (output started at /includes/utf/utf_tools.php:1781)
[phpBB Debug] PHP Notice: in file /includes/session.php on line 1007: Cannot modify header information - headers already sent by (output started at /includes/utf/utf_tools.php:1781)
[phpBB Debug] PHP Notice: in file /includes/session.php on line 1007: Cannot modify header information - headers already sent by (output started at /includes/utf/utf_tools.php:1781)

Strict Standards: Non-static method utf_normalizer::nfc() should not be called statically in /www/htdocs/w006661d/anddev/includes/utf/utf_tools.php on line 1781

Strict Standards: Non-static method utf_normalizer::nfc() should not be called statically in /www/htdocs/w006661d/anddev/includes/utf/utf_tools.php on line 1781
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 4362: Cannot modify header information - headers already sent by (output started at /includes/utf/utf_tools.php:1781)
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 4364: Cannot modify header information - headers already sent by (output started at /includes/utf/utf_tools.php:1781)
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 4365: Cannot modify header information - headers already sent by (output started at /includes/utf/utf_tools.php:1781)
[phpBB Debug] PHP Notice: in file /includes/functions.php on line 4366: Cannot modify header information - headers already sent by (output started at /includes/utf/utf_tools.php:1781)
anddev.org • View topic - Help with retrieving image or text from website

Help with retrieving image or text from website

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

Help with retrieving image or text from website

Postby Nuladion » Sun Jan 11, 2009 2:01 pm

Hello everyone,
I'd like to load some text and images from a website. I looked around and found the following code:

(code from http://www.devx.com/wireless/Article/39810/0/page/2)
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2.  
  3. public void onCreate(Bundle savedInstanceState) {
  4.  
  5.         super.onCreate(savedInstanceState);
  6.  
  7.         setContentView(R.layout.main);
  8.  
  9.        
  10.  
  11.         Bitmap bitmap = DownloadImage("http://www.streetcar.org/mim/cable/images/cable-01.jpg");
  12.  
  13.         img = (ImageView) findViewById(R.id.img);
  14.  
  15.         img.setImageBitmap(bitmap);
  16.  
  17. }
  18.  
  19.  
  20.  
  21.  
  22.  
  23. private Bitmap DownloadImage(String URL)
  24.  
  25. {        
  26.  
  27.         Bitmap bitmap = null;
  28.  
  29.         InputStream in = null;        
  30.  
  31.         try {
  32.  
  33.                 in = OpenHttpConnection(URL);
  34.  
  35.                 bitmap = BitmapFactory.decodeStream(in);
  36.  
  37.                 in.close();
  38.  
  39.         } catch (IOException e1) {
  40.  
  41.                 // TODO Auto-generated catch block
  42.  
  43.                 e1.printStackTrace();
  44.  
  45.         }
  46.  
  47.         return bitmap;                
  48.  
  49. }
  50.  
  51.  
Parsed in 0.010 seconds, using GeSHi 1.0.8.4


Android doesn't seem to recognize the OpenHttpConnection function. I guess this got replaced by an other function in an update. Could anyone tell me how I'd load an image/text from a webserver?

Thanks in advance.

Regards,
Guido
Nuladion
Developer
Developer
 
Posts: 26
Joined: Mon Jun 23, 2008 12:23 pm
Location: Maastricht, Netherlands

Top

Postby Emmanuel7 » Sun Jan 11, 2009 4:52 pm

I think you should use HttpUrlConnection :
Google Api Documentation link
Emmanuel7
Senior Developer
Senior Developer
 
Posts: 164
Joined: Fri Dec 12, 2008 2:17 am
Location: Paris

Postby Nuladion » Sun Jan 11, 2009 5:38 pm

Emmanuel7 wrote:I think you should use HttpUrlConnection :
Google Api Documentation link


Thanks for mentioning HttpUrlConnection. I googled around a bit and found this, which worked perfectly for images!
Now I'll have to try and get it to work for text, too.
Nuladion
Developer
Developer
 
Posts: 26
Joined: Mon Jun 23, 2008 12:23 pm
Location: Maastricht, Netherlands

Postby Nuladion » Thu Jan 15, 2009 12:20 pm

I searched around a bit, and I finally got both to work. For those who're trying to do the same, here's the code I ended up with:

Make sure to edit the package name and the urls to the .txt and .png files!
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.yourpackage.name;
  2.  
  3.  
  4.  
  5. import java.io.BufferedInputStream;
  6.  
  7. import java.io.IOException;
  8.  
  9. import java.io.InputStream;
  10.  
  11. import java.net.HttpURLConnection;
  12.  
  13. import java.net.MalformedURLException;
  14.  
  15. import java.net.URL;
  16.  
  17.  
  18.  
  19. import org.apache.http.util.ByteArrayBuffer;
  20.  
  21.  
  22.  
  23. import android.app.Activity;
  24.  
  25. import android.graphics.BitmapFactory;
  26.  
  27. import android.os.Bundle;
  28.  
  29. import android.view.Window;
  30.  
  31. import android.widget.ImageView;
  32.  
  33. import android.widget.TextView;
  34.  
  35.  
  36.  
  37. public class DynamicContent extends Activity {
  38.  
  39.         TextView txtView;
  40.  
  41.         String textUrl = "http://www.yourdomain.com/your_text_file.txt";
  42.  
  43.         ImageView imgView;
  44.  
  45.         String imageUrl = "http://www.yourdomain.com/your_image.png";
  46.  
  47.        
  48.  
  49.         /** Called when the activity is first created. */
  50.  
  51.         @Override
  52.  
  53.         public void onCreate(Bundle icicle) {
  54.  
  55.                 super.onCreate(icicle);
  56.  
  57.                
  58.  
  59.                 // Hide title bar.
  60.  
  61.                 getWindow().requestFeature(Window.FEATURE_NO_TITLE);
  62.  
  63.        
  64.  
  65.                  // Set content view
  66.  
  67.                 setContentView(R.layout.main);
  68.  
  69.                
  70.  
  71.                 // Get views
  72.  
  73.                 txtView = (TextView) findViewById(R.id.content_text);
  74.  
  75.                 imgView = (ImageView) findViewById(R.id.content_image);
  76.  
  77.                
  78.  
  79.                 // Download content
  80.  
  81.                 downloadText(textUrl, txtView);
  82.  
  83.                 downloadImage(imageUrl, imgView);              
  84.  
  85.         }
  86.  
  87.        
  88.  
  89.         // Function to download text from given URL, and parse it to desired TextView
  90.  
  91.         void downloadText(String fileUrl, TextView v){
  92.  
  93.                 URL myFileUrl = null;
  94.  
  95.                 try {
  96.  
  97.                         myFileUrl= new URL(fileUrl);
  98.  
  99.                 } catch (MalformedURLException e) {
  100.  
  101.                         // TODO Auto-generated catch block
  102.  
  103.                         e.printStackTrace();
  104.  
  105.                 }
  106.  
  107.                 try {
  108.  
  109.                         HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
  110.  
  111.                         conn.setDoInput(true);
  112.  
  113.                         conn.connect();
  114.  
  115.                         InputStream is = conn.getInputStream();
  116.  
  117.                         BufferedInputStream bis = new BufferedInputStream(is);
  118.  
  119.            
  120.  
  121.             /* Read bytes to the Buffer until
  122.  
  123.              * there is nothing more to read(-1). */
  124.  
  125.             ByteArrayBuffer baf = new ByteArrayBuffer(50);
  126.  
  127.             int current = 0;
  128.  
  129.             while((current = bis.read()) != -1){
  130.  
  131.                  baf.append((byte)current);
  132.  
  133.             }
  134.  
  135.  
  136.  
  137.             /* Convert the Bytes read to a String. */
  138.  
  139.             v.setText(new String(baf.toByteArray()));
  140.  
  141.                 } catch (IOException e) {
  142.  
  143.                         // TODO Auto-generated catch block
  144.  
  145.                         e.printStackTrace();
  146.  
  147.                 }
  148.  
  149.         }
  150.  
  151.        
  152.  
  153.         // Function to download image from given URL, and parse it to desired ImageView
  154.  
  155.         void downloadImage(String fileUrl, ImageView v) {
  156.  
  157.                 URL myFileUrl = null;
  158.  
  159.                 try {
  160.  
  161.                         myFileUrl= new URL(fileUrl);
  162.  
  163.                 } catch (MalformedURLException e) {
  164.  
  165.                         // TODO Auto-generated catch block
  166.  
  167.                         e.printStackTrace();
  168.  
  169.                 }
  170.  
  171.                 try {
  172.  
  173.                         HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
  174.  
  175.                         conn.setDoInput(true);
  176.  
  177.                         conn.connect();
  178.  
  179.                         InputStream is = conn.getInputStream();
  180.  
  181.  
  182.  
  183.                         v.setImageBitmap(BitmapFactory.decodeStream(is));
  184.  
  185.                 } catch (IOException e) {
  186.  
  187.                         // TODO Auto-generated catch block
  188.  
  189.                         e.printStackTrace();
  190.  
  191.                 }
  192.  
  193.         }
  194.  
  195. }
Parsed in 0.015 seconds, using GeSHi 1.0.8.4

The text will be put in the TextView with id content_text in main.xml, and the image will be put in the ImageView with id content_image, so make sure those two do exist :)

Make sure you add the INTERNET permission to your AndroidManifest!
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <uses-permission android:name="android.permission.INTERNET" />
Parsed in 0.000 seconds, using GeSHi 1.0.8.4
Nuladion
Developer
Developer
 
Posts: 26
Joined: Mon Jun 23, 2008 12:23 pm
Location: Maastricht, Netherlands

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 2 guests