<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Android Resource &#124; 资源 资讯 教程 &#187; HttpURLConnection</title>
	<atom:link href="http://www.androidres.com/index.php/tag/httpurlconnection/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.androidres.com</link>
	<description>Discuss developing Android applications using the Android framework and Collect android resource</description>
	<lastBuildDate>Tue, 12 Jan 2010 10:03:28 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<!-- Easy AdSense V2.82 -->
<!-- Post[count: 1] -->
<div class="ezAdsense adsense adsense-leadin" style="text-align:center;margin:12px;"><script type="text/javascript"><!--
google_ad_client = "pub-4703322803494425";
/* 468x60, Top_SinglePost */
google_ad_slot = "7939142022";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>	<item>
		<title>应用HttpURLConnection读取InputStream信息</title>
		<link>http://www.androidres.com/index.php/2009/04/30/read-inputstream-via-httpurlconnection/</link>
		<comments>http://www.androidres.com/index.php/2009/04/30/read-inputstream-via-httpurlconnection/#comments</comments>
		<pubDate>Thu, 30 Apr 2009 06:49:49 +0000</pubDate>
		<dc:creator>EGGer</dc:creator>
				<category><![CDATA[Resource]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[BufferedReader]]></category>
		<category><![CDATA[HttpURLConnection]]></category>
		<category><![CDATA[InputStream]]></category>

		<guid isPermaLink="false">http://www.androidres.com/?p=403</guid>
		<description><![CDATA[基于Android平台的应用开发大部分都会涉及到应用网络资源，然而通过指定的URL来读取数据可以算得上是最基本的应用。下面通过一个小例子演示如何实现这个方法。
注意：在调试代码之前，请首先确认AndroidManifest中已经添加了Internet permission标签。
源代码：


    private void getConnStream(){

    	String result = null;
    	URL url = null;
		try {
			url = new URL("http://www.androidres.com");
		} catch (MalformedURLException e) {
			Log.e("TestConnStream_URL",e.getMessage());
		}

		if (url != null) {
			try {
				HttpURLConnection urlConn = (HttpURLConnection) url
						.openConnection();
				BufferedReader in = new BufferedReader(new InputStreamReader(
						urlConn.getInputStream()));
				String inputLine;

				int lineCount = 0; // limit the lines for the example

		 [...]]]></description>
			<content:encoded><![CDATA[<p>基于Android平台的应用开发大部分都会涉及到应用网络资源，然而通过指定的URL来读取数据可以算得上是最基本的应用。下面通过一个小例子演示如何实现这个方法。</p>
<blockquote><p>注意：在调试代码之前，请首先确认AndroidManifest中已经添加了Internet permission标签。</p></blockquote>
<p>源代码：</p>
<pre class="brush: java; smart-tabs: true;toolbar: false">

    private void getConnStream(){

    	String result = null;
    	URL url = null;
		try {
			url = new URL("http://www.androidres.com");
		} catch (MalformedURLException e) {
			Log.e("TestConnStream_URL",e.getMessage());
		}

		if (url != null) {
			try {
				HttpURLConnection urlConn = (HttpURLConnection) url
						.openConnection();
				BufferedReader in = new BufferedReader(new InputStreamReader(
						urlConn.getInputStream()));
				String inputLine;

				int lineCount = 0; // limit the lines for the example

		        while ((lineCount < 8 ) &#038;&#038; ((inputLine = in.readLine()) != null)) {
		             lineCount++;
		             result += "\n" + inputLine;
		        }

		        in.close();
		        urlConn.disconnect();

			} catch (IOException e) {
				Log.e("TestConnStream",e.getMessage());
			}
		}
    }
</pre>
<p>返回的result结果：</p>
<h5><span style="color: #888888;">&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;<br />
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;<br />
&lt;head profile="http://gmpg.org/xfn/11"&gt;<br />
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;<br />
&lt;title&gt;Android Resource | Android 资源 - 资讯 - 教程 |&lt;/title&gt;<br />
&lt;link rel="stylesheet" href="http://www.androidres.com/wp-content/themes/BobV3/style.css" type="text/css" media="screen" /&gt;<br />
&lt;link rel="alternate" type="application/rss+xml" title="Android Resource | 资源 资讯 教程 RSS Feed" href="http://www.androidres.com/?feed=rss2" /&gt;<br />
&lt;link rel="pingback" href="http://www.androidres.com/xmlrpc.php" /&gt;<br />
&lt;script type="text/javascript" language="javascript" src="http://www.androidres.com/wp-content/themes/BobV3/moo.js"&gt;&lt;/script&gt;<br />
&lt;script type="text/javascript" language="javascript" src="http://www.androidres.com/wp-content/themes/BobV3/moostick.js"&gt;&lt;/script&gt;</span></h5>
]]></content:encoded>
			<wfw:commentRss>http://www.androidres.com/index.php/2009/04/30/read-inputstream-via-httpurlconnection/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>如何应用ImageView读取网络图片资源</title>
		<link>http://www.androidres.com/index.php/2009/04/09/android-load-web-images-resource-via-imageview/</link>
		<comments>http://www.androidres.com/index.php/2009/04/09/android-load-web-images-resource-via-imageview/#comments</comments>
		<pubDate>Thu, 09 Apr 2009 10:03:09 +0000</pubDate>
		<dc:creator>Edgar Sun</dc:creator>
				<category><![CDATA[Resource]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Application Framework]]></category>
		<category><![CDATA[BitmapFactory]]></category>
		<category><![CDATA[HttpURLConnection]]></category>

		<guid isPermaLink="false">http://www.androidres.com/?p=344</guid>
		<description><![CDATA[这个教程将演示如何应用ImageView读取网络图片资源。有两个比较重要的知识点需要特别注意：

应用HttpURLConnection获得图片数据。
通过BitmapFactory将数据转换为可供ImageView识别的图像资源。


Layout 源代码 （XML）：




    
    


主程序源代码 （Java）：

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class HTTPTest extends Activity {

     ImageView imView;
     String imageUrl="http://11.0.6.23/";
     Random r;
    [...]]]></description>
			<content:encoded><![CDATA[<p>这个教程将演示如何应用ImageView读取网络图片资源。有两个比较重要的知识点需要特别注意：</p>
<ul>
<li>应用HttpURLConnection获得图片数据。</li>
<li>通过BitmapFactory将数据转换为可供ImageView识别的图像资源。</li>
</ul>
<p><span id="more-344"></span><br />
Layout 源代码 （XML）：</p>
<pre lang="xml" line="n">
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Hello World, HTTPImage load test"
    />
    <Button id="@+id/get_imagebt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Get an image"
    android:layout_gravity="center"
    />
    <ImageView id="@+id/imview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    />
</LinearLayout>
</pre>
<p>主程序源代码 （Java）：</p>
<pre class="brush: java; smart-tabs: true;toolbar: false">
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class HTTPTest extends Activity {

     ImageView imView;
     String imageUrl="http://11.0.6.23/";
     Random r;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        r= new Random();

        Button bt3= (Button)findViewById(R.id.get_imagebt);
        bt3.setOnClickListener(getImgListener);
        imView = (ImageView)findViewById(R.id.imview);
    }    

    View.OnClickListener getImgListener = new View.OnClickListener()
    {

          @Override
          public void onClick(View view) {
               // TODO Auto-generated method stub

               //i tried to randomize the file download, in my server i put 4 files with name like
                        //png0.png, png1.png, png2.png so different file is downloaded in button press
               int i =r.nextInt()%4;
               downloadFile(imageUrl+"png"+i+".png");
               Log.i("im url",imageUrl+"png"+i+".png");
          }

    };

    Bitmap bmImg;
    void downloadFile(String fileUrl){
          URL myFileUrl =null;
          try {
               myFileUrl= new URL(fileUrl);
          } catch (MalformedURLException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          }
          try {
               HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
               conn.setDoInput(true);
               conn.connect();
               int length = conn.getContentLength();

               InputStream is = conn.getInputStream();

               bmImg = BitmapFactory.decodeStream(is);
               imView.setImageBitmap(bmImg);
          } catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          }
     }
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.androidres.com/index.php/2009/04/09/android-load-web-images-resource-via-imageview/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
