使用HttpURLConnection訪問網絡,java
一、首先須要得到一個HttpURLConnection實例,通常只須要new出一個URL對象,傳入目標的網絡地址,android
二、而後調用一下openConnection()的方法,服務器
三、以後,須要設置http請求的方法:網絡
3.一、POST或者GET(GET表示但願從服務器獲取數據,POST表示但願提交數據到服務器。),app
四、接下來,就自由定製了,好比設置訪問超時時間,讀取超時等,這些按照實際狀況來設置吧,ide
五、下面是實例代碼了:佈局
主佈局文件:學習
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" tools:context="com.example.yaowen.networktest.Main"> <Button android:id="@+id/sendRequest" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Send Request" /> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/response" android:layout_width="match_parent" android:layout_height="wrap_content" /> </ScrollView> </LinearLayout>
主活動代碼:ui
package com.example.yaowen.networktest; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class Main extends AppCompatActivity implements View.OnClickListener { public static final int SHOW_RESPONSE = 0; private Button sendRequest; private TextView responseText; private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) {//這裏更新UI組件 case SHOW_RESPONSE: String response = (String) msg.obj; responseText.setText(response); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sendRequest = (Button) findViewById(R.id.sendRequest); responseText = (TextView) findViewById(R.id.response); sendRequest.setOnClickListener(this);//設置監聽 } @Override public void onClick(View v) { if (v.getId() == R.id.sendRequest) { sendRequestWithHttpURLConnection();//開啓HttpURLConnection訪問的請求方法 } } private void sendRequestWithHttpURLConnection() { new Thread(new Runnable() { @Override public void run() {//新建線程 HttpURLConnection conn = null; try { URL url = new URL("https://www.baidu.com"); conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setReadTimeout(3000); conn.setConnectTimeout(3000); InputStream is = conn.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is)); StringBuilder response = new StringBuilder(); String line; while ((line = bufferedReader.readLine()) != null) { response.append(line); } Message msg = new Message(); msg.what = SHOW_RESPONSE; msg.obj = response.toString(); handler.sendMessage(msg); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (conn != null) { conn.disconnect(); } } } }) .start();//啓動該線程的方法。 } }
註冊文件:this
須要添加入訪問網絡權限:
<uses-permission android:name="android.permission.INTERNET" />
六、運行效果圖:
七、完結,有什麼問題在評論區評論吧,你們共同窗習,謝謝!