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