Android中HttpURLConnection類使用介紹
Http協(xié)議的認(rèn)識:
Android中發(fā)送http網(wǎng)絡(luò)請求是很常見的,要有GET請求和POST請求。一個完整的http請求需要經(jīng)歷兩個過程:客戶端發(fā)送請求到服務(wù)器,然后服務(wù)器將結(jié)果返回給客戶端。
GET表示希望從服務(wù)器那里獲取數(shù)據(jù),而POST則表示希望提交數(shù)據(jù)給服務(wù)器。
通過Http訪問網(wǎng)絡(luò)的三個步驟:
1、發(fā)送http請求
2、接受服務(wù)響應(yīng)
3、解析返回數(shù)據(jù)
HttpURLConnection類位于java.net包中,它用于發(fā)送HTTP請求和獲取HTTP響應(yīng)。
話不多說,直接上代碼:
首先創(chuàng)建一個安卓項目。在xml中編寫如下代碼:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="獲取"
/>
<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>
ScrollView是可供用戶滾動的層次結(jié)構(gòu)布局容器,允許顯示比實(shí)際多的內(nèi)容,借助ScrollView控件,我們就可以以滾動的形式查看屏幕外的那部分內(nèi)容。
上面的代碼主要是實(shí)現(xiàn),當(dāng)點(diǎn)擊按鈕時,下面的滾動視圖將展示其內(nèi)容。
在java中編寫如下代碼:
public class MainActivity extends AppCompatActivity {
TextView response;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button)findViewById(R.id.button);
response=(TextView)findViewById(R.id.response);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url=new URL("http://www.baidu.com");
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
InputStream inputStream=connection.getInputStream();
BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder=new StringBuilder();
String Line;
while ((Line=reader.readLine())!=null){
stringBuilder.append(Line);
}
show(stringBuilder);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
});
}
private void show(final StringBuilder stringBuilder) {
runOnUiThread(new Runnable() {
@Override
public void run() {
response.setText(stringBuilder);
}
});
}
}首先需要獲取到HttpURLConnection的實(shí)例,一般只需new出一個URL對象,并傳入目標(biāo)的網(wǎng)絡(luò)地址,然后調(diào)用一下openConnection()方法即可,如下所示:
URL url=new URL("http://www.baidu.com");
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
在得到了HttpURLConnection的實(shí)例后,我們可以設(shè)置一下HTTP請求所使用的方法。常用的方法主要有兩個:GET和POST。GET表示希望從服務(wù)器那里獲取數(shù)據(jù),而POST則表示希望提交數(shù)據(jù)給服務(wù)器。寫法如下:
connection.setRequestMethod(“GET”);
之后在調(diào)用getInputStream()方法就可以獲取到服務(wù)器返回的輸入流了,剩下的任務(wù)就是對輸入流進(jìn)行讀取。
最后別忘了在AndroidManifest.xml中聲明一下網(wǎng)絡(luò)權(quán)限和添加如下代碼(不加就會有錯誤):
android:usesCleartextTraffic="true"
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.http4">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:usesCleartextTraffic="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
效果圖如下所示:

到此這篇關(guān)于Android中HttpURLConnection類使用介紹的文章就介紹到這了,更多相關(guān)Android HttpURLConnection內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android AsyncTask的缺陷和問題總結(jié)
這篇文章主要介紹了Android AsyncTask的缺陷和問題總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-03-03
Android?Framework原理Binder驅(qū)動源碼解析
這篇文章主要為大家介紹了Android?Framework原理Binder驅(qū)動源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
Android編程開發(fā)之ScrollView嵌套GridView的方法
這篇文章主要介紹了Android編程開發(fā)之ScrollView嵌套GridView的方法,結(jié)合實(shí)例分析了ScrollView嵌套GridView的相關(guān)注意事項與處理技巧,需要的朋友可以參考下2015-12-12
android自定義View實(shí)現(xiàn)手勢解鎖
這篇文章主要為大家詳細(xì)介紹了android自定義View實(shí)現(xiàn)手勢解鎖,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06
android全局監(jiān)控click事件的四種方式(小結(jié))
本篇文章主要介紹了android全局監(jiān)控click事件的四種方式(小結(jié)),詳細(xì)介紹如何在全局上去監(jiān)聽 click 點(diǎn)擊事件,并做些通用處理或是攔截,有興趣的可以了解一下2017-08-08
Android系列---JSON數(shù)據(jù)解析的實(shí)例
JSON(JavaScript Object Notation)和XML,并稱為客戶端和服務(wù)端交互解決方案的倚天劍和屠龍刀,這篇文章主要介紹了Android系列---JSON數(shù)據(jù)解析的實(shí)例,有興趣的可以了解一下。2016-11-11

