android串連網路

來源:互聯網
上載者:User

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");
    }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.