使用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" />
六、运行效果图:
七、完结,有什么问题在评论区评论吧,你们共同窗习,谢谢!