String uriAPI = "http://www.dubblogs.cc:8751/Android/Test/API/YamWeather/";
URL objURL = new URL(uriAPI);
/* 取得串連 */
URLConnection conn = objURL.openConnection();
conn.connect();
/* 將InputStream轉成Reader */
BufferedReader in = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String inputLine;
/* 圖檔案路徑 */
String uriPic = "";
/* 一行一行讀取 */
while ((inputLine = in.readLine()) != null)
{
uriPic += inputLine;
}
objURL = new URL(uriPic);
/* 取得串連 */
HttpURLConnection conn2 = (HttpURLConnection) objURL
.openConnection();
conn2.connect();
/* 取得返回的InputStream */
InputStream is = conn2.getInputStream();
/* 將InputStream變成Bitmap */
Bitmap bm = BitmapFactory.decodeStream(is);
/* 關閉InputStream */
is.close();
mImageView1.setImageBitmap(bm);
/* 會將上面的網狀圖片顯示在ImageView裡面*/
使用WebView
Android手機中內建了一款高效能webkit核心瀏覽器,在SDK中封裝成了WebView組件。
http://developer.android.com/guide/tutorials/views/hello-webview.html提供了一個簡單的例子:
1. webview的XML定義:
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
2.Manifest檔案中許可權的設定:
<uses-permission android:name="android.permission.INTERNET" />
3.如果想要支援JavaScript:
webview.getSettings().setJavaScriptEnabled(true);
4.
如果不做任何處理,在顯示你的Brower UI時,點擊系統“Back”鍵,整個Browser會作為一個整體“Back"到其他Activity中,
而不是希望的在Browser的曆史頁面中Back。如果希望實現在曆史頁面中Back,需要在當前Activity中處理Back事
件:mWebView.goBack();
WebView webview;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 擷取WebView對象
webview = (WebView) findViewById(R.id.webview);
// 使能JavaScript
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://www.google.com");
}
以上是採用loadUrl方法實現網頁的載入,也可以採用loadData方法實現網頁的載入:
mWebView1 = (WebView) findViewById(R.id.myWebView1);
/*自行設定WebView要顯示的網頁內容*/
mWebView1.
loadData(
"<html><body><p>Subscribe to my Blog</p>" +
"<div class='widget-content'> "+
"<a href=http://www.wretch.cc/blog/blackoa&rss20=1>" +
"<img src=http://angelosu.googlepages.com/feeds128.png />" +
"<a href=http://www.cnblogs.com/tt_mc>Link Blog</a>" +
"</body></html>", "text/html", "utf-8");
}