安卓上面敲127.0.0.1或者是localhost是不行的,安卓上面的localhost/127.0.0.1默認是10.0.2.2.有些第三方的模擬器可能不是這個值。安卓自帶的模擬器確定是沒問題的。若是是第三方的模擬器本身練習一下能夠搞一個路由器鏈接一下,讓你當前的機器有一個地址。路由仍是比較好搞的,把你的手機共享一下wifi。把全部的設備都鏈接到你的手機上,組成一個局域網,你把流量關了就能夠了。html
聯網的API叫作HttpURLConnection.java
Firefox脫機工做以後文檔的查詢會快不少.android
HttpURLConnection表明我當前用的協議是Http協議。web
An URLConnection for HTTP (RFC 2616) used to send and receive data over the web. Data may be of any type and length. This class may be used to send and receive streaming data whose length is not known in advance.
這個就是HttpURLConnection.cookie
按照以下的方式使用HttpURLConnection這個類:網絡
Uses of this class follow a pattern:
Obtain a new HttpURLConnection by calling URL.openConnection() and casting the result to HttpURLConnection.
Prepare the request. The primary property of a request is its URI. Request headers may also include metadata such as credentials, preferred content types, and session cookies.
Optionally upload a request body. Instances must be configured with setDoOutput(true) if they include a request body. Transmit data by writing to the stream returned by getOutputStream().
Read the response. Response headers typically include metadata such as the response body's content type and length, modified dates and session cookies. The response body may be read from the stream returned by getInputStream(). If the response has no body, that method returns an empty stream.
Disconnect. Once the response body has been read, the HttpURLConnection should be closed by calling disconnect(). Disconnecting releases the resources held by a connection so they may be closed or reused.
小例子session
For example, to retrieve the webpage at http://www.android.com/: URL url = new URL("http://www.android.com/"); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); try { InputStream in = new BufferedInputStream(urlConnection.getInputStream()); readStream(in); finally { urlConnection.disconnect(); } }
[2017-06-20 05:20:54 - Day08_01_網頁源碼查看器] Android Launch!
[2017-06-20 05:20:54 - Day08_01_網頁源碼查看器] adb is running normally.
[2017-06-20 05:20:54 - Day08_01_網頁源碼查看器] Performing com.itheima.htmlcodeviewer.MainActivity activity launch
[2017-06-20 05:20:55 - Day08_01_網頁源碼查看器] Automatic Target Mode: launching new emulator with compatible AVD '2.3'
[2017-06-20 05:20:55 - Day08_01_網頁源碼查看器] Launching a new emulator with Virtual Device '2.3'
[2017-06-20 05:20:55 - Emulator] emulator: Failed to open the HAX device!
[2017-06-20 05:20:55 - Emulator] HAX is not working and emulator runs in emulation mode
[2017-06-20 05:20:55 - Emulator]
[2017-06-20 05:20:55 - Emulator] emulator: Open HAX device failed
[2017-06-20 05:20:55 - Emulator]
[2017-06-20 05:20:56 - Emulator] emulator: emulator window was out of view and was recentered
[2017-06-20 05:20:56 - Emulator]
[2017-06-20 05:20:56 - Emulator] bind: Permission denied
[2017-06-20 05:20:56 - Day08_01_網頁源碼查看器] New emulator found: emulator-5556
[2017-06-20 05:20:56 - Day08_01_網頁源碼查看器] Waiting for HOME ('android.process.acore') to be launched...
[2017-06-20 05:21:27 - Day08_01_網頁源碼查看器] HOME is up on device 'emulator-5556'
[2017-06-20 05:21:27 - Day08_01_網頁源碼查看器] Uploading Day08_01_網頁源碼查看器.apk onto device 'emulator-5556'
[2017-06-20 05:21:27 - Day08_01_網頁源碼查看器] Installing Day08_01_網頁源碼查看器.apk...
[2017-06-20 05:21:38 - Day08_01_網頁源碼查看器] Success!
[2017-06-20 05:21:38 - Day08_01_網頁源碼查看器] Starting activity com.itheima.htmlcodeviewer.MainActivity on device emulator-5556
[2017-06-20 05:21:39 - Day08_01_網頁源碼查看器] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.itheima.htmlcodeviewer/.MainActivity }
聯網就涉及到要花錢,花錢的時候就須要權限。app
package com.itheima.htmlcodeviewer; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import android.os.Bundle; import android.app.Activity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { private EditText et_url; private Button btn_show; private TextView tv_code; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_url = (EditText) findViewById(R.id.et_url); btn_show = (Button) findViewById(R.id.btn_show); tv_code = (TextView) findViewById(R.id.tv_code); btn_show.setOnClickListener(new MyOnClickListenr()); } private class MyOnClickListenr implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub //獲取url String path = et_url.getText().toString().trim(); try { URL url = new URL(path); //URLConnection openConnection = url.openConnection(); HttpURLConnection openConnection = (HttpURLConnection) url.openConnection(); //} catch (MalformedURLException e) { //設置請求的方法 方法要大寫 默認採用的是GET方式請求 openConnection.setRequestMethod("GET"); //若是聯網以後網絡信號不是太好,那就涉及到一直在等,一直在等 //有的時候可能會等的時間很長,等的時間很長的話那就有問題了,我究竟等到何時算是個結束 //設置超時的時間,一旦超過這個時間默認就是鏈接失敗,讓它中止下來 openConnection.setConnectTimeout(10000);//設置一個鏈接超時的時間 //獲取響應碼 int responseCode = openConnection.getResponseCode(); if(responseCode==200){ InputStream inputStream = openConnection.getInputStream();//InputStream其實放的就是跟我們HTML代碼相關的內容 String stringFromStream = Utils.getStringFromStream(inputStream); tv_code.setText(stringFromStream); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } //拿着url聯網 //判斷響應碼 若是是200 //聯網後得到響應內容 響應是經過流的方式去返回回來的 //經過textview 展現對應的內容 } } }
package com.itheima.htmlcodeviewer; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; public class Utils { //從這個流裏面把它內容讀出來,讀出來以後把它轉化成一個String類型的數據 public static String getStringFromStream(InputStream inputStream) { // TODO Auto-generated method stub //把流轉化爲字符串 ByteArrayOutputStream baso = new ByteArrayOutputStream(); int len = -1; byte[] buffer = new byte[1024]; try { while((len=inputStream.read(buffer))!=-1){ baso.write(buffer, 0, len); } inputStream.close(); byte[] byteArray = baso.toByteArray(); return new String(byteArray); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <EditText android:id="@+id/et_url" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textUri" android:hint="請輸入網址" /> <Button android:id="@+id/btn_show" android:layout_below="@id/et_url" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="查看"/> <TextView android:id="@+id/tv_code" android:layout_below="@id/btn_show" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.itheima.htmlcodeviewer" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.itheima.htmlcodeviewer.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>