用HTTP协议连接网络(HttpURLConnection)
生活随笔
收集整理的這篇文章主要介紹了
用HTTP协议连接网络(HttpURLConnection)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
2019獨角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
使用HttpURLConnection訪問網(wǎng)絡(luò),
1、首先需要獲得一個HttpURLConnection實例,一般只需要new出一個URL對象,傳入目標(biāo)的網(wǎng)絡(luò)地址,
2、然后調(diào)用一下openConnection()的方法,
3、之后,需要設(shè)置http請求的方法:
3.1、POST或者GET(GET表示希望從服務(wù)器獲取數(shù)據(jù),POST表示希望提交數(shù)據(jù)到服務(wù)器。),
4、接下來,就自由定制了,比如設(shè)置訪問超時時間,讀取超時等,這些按照實際情況來設(shè)置吧,
5、下面是實例代碼了:
主布局文件:
<?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"><Buttonandroid:id="@+id/sendRequest"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Send?Request"?/><ScrollViewandroid:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:id="@+id/response"android:layout_width="match_parent"android:layout_height="wrap_content"?/></ScrollView> </LinearLayout>主活動代碼:
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()?{@Overridepublic?void?handleMessage(Message?msg)?{super.handleMessage(msg);switch?(msg.what)?{//這里更新UI組件case?SHOW_RESPONSE:String?response?=?(String)?msg.obj;responseText.setText(response);}}};@Overrideprotected?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);//設(shè)置監(jiān)聽}@Overridepublic?void?onClick(View?v)?{if?(v.getId()?==?R.id.sendRequest)?{sendRequestWithHttpURLConnection();//開啟HttpURLConnection訪問的請求方法}}private?void?sendRequestWithHttpURLConnection()?{new?Thread(new?Runnable()?{@Overridepublic?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();//啟動該線程的方法。} }注冊文件:
需要添加入訪問網(wǎng)絡(luò)權(quán)限:
<uses-permission?android:name="android.permission.INTERNET"?/>6、運行效果圖:
7、完結(jié),有什么問題在評論區(qū)評論吧,大家共同學(xué)習(xí),謝謝!
?
轉(zhuǎn)載于:https://my.oschina.net/yaowen424/blog/550952
總結(jié)
以上是生活随笔為你收集整理的用HTTP协议连接网络(HttpURLConnection)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 编程之美——买书问题:贪心算法
- 下一篇: 老男孩学习之亲身经历心得