HttpURLConnection和okHttp兩種獲取網(wǎng)絡(luò)數(shù)據(jù)的實現(xiàn)方法
廢話少說,直接上代碼。簡單易懂。
xml如下:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_net" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.waterlamp.NetActivity"> <Button android:id="@+id/okhttp" android:text="okhttp請求" android:layout_width="150dp" android:layout_height="37dp" android:layout_alignBottom="@+id/http" android:layout_alignParentEnd="true" android:layout_alignParentTop="true" /> <Button android:id="@+id/http" android:text="HTTP請求" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentStart="true" /> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentStart="true" android:layout_below="@+id/okhttp" android:layout_alignParentBottom="true" android:layout_alignParentEnd="true" > <TextView android:id="@+id/stringData" android:layout_width="match_parent" android:layout_height="match_parent" /> </ScrollView> </RelativeLayout>
activity如下:
public class NetActivity extends AppCompatActivity implements View.OnClickListener{
private Button http,okhttp;
private TextView stringData;
private String web="https://www.baidu.com";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_net);
http= (Button) findViewById(R.id.http);
okhttp= (Button) findViewById(R.id.okhttp);
stringData= (TextView) findViewById(R.id.stringData);
http.setOnClickListener(this);
okhttp.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.http:
sendRequestWithHttpURLConnection();
break;
case R.id.okhttp:
sendRequestWithokHttp();
break;
}
}
private void sendRequestWithHttpURLConnection(){
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection connection=null;
BufferedReader reader=null;
try {
URL url=new URL(web);
connection= (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
InputStream in=connection.getInputStream();
reader=new BufferedReader(new InputStreamReader(in));
StringBuffer response=new StringBuffer();
String line;
while ((line=reader.readLine())!=null){
response.append(line);
}
showRespond(response.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (reader!=null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}).start();
}
private void showRespond(final String response) {
runOnUiThread(new Runnable() {
@Override
public void run() {
stringData.setText(response);
}
});
}
private void sendRequestWithokHttp(){
new Thread(new Runnable() {
@Override
public void run() {
try {
OkHttpClient client=new OkHttpClient();
Request request=new Request.Builder()
.url(web).build();
Response response=client.newCall(request).execute();
String str=response.body().string();
showRespond(str);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
}
注意:需要在加權(quán)限
1.<uses-permission android:name="android.permission.INTERNET"/>
2.okhttp3需要在gradle添加依賴
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:design:24.2.1'
compile 'com.squareup.okhttp3:okhttp:3.4.1'//依賴
testCompile 'junit:junit:4.12'
}
以上這篇HttpURLConnection和okHttp兩種獲取網(wǎng)絡(luò)數(shù)據(jù)的實現(xiàn)方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Android Retrofit2網(wǎng)路編程實現(xiàn)方法詳解
這篇文章主要介紹了Android Retrofit2網(wǎng)路編程實現(xiàn)方法詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-12-12
Android使用ViewFlipper實現(xiàn)圖片上下自動輪播的示例代碼
這篇文章主要介紹了Android使用ViewFlipper實現(xiàn)圖片上下自動輪播的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
Kotlin 封裝萬能SharedPreferences存取任何類型詳解
這篇文章主要介紹了Kotlin 封裝萬能SharedPreferences存取任何類型詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05
Android項目實戰(zhàn)之Glide 高斯模糊效果的實例代碼
這篇文章主要介紹了Android項目實戰(zhàn)之Glide 高斯模糊效果的實例代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-06-06
Android 使用Vitamio打造自己的萬能播放器(6)——在線播放(播放列表)
本文主要介紹Android Vitamino在線播放列表,這里給大家提供效果圖和實例代碼以便大家參考學(xué)習(xí),希望能幫助開發(fā)Android視頻播放的朋友2016-07-07
Android判斷后臺服務(wù)是否開啟的兩種方法實例詳解
這篇文章主要介紹了Android判斷后臺服務(wù)是否開啟的方法的相關(guān)資料,這里提供了兩種方法及實例,需要的朋友可以參考下2017-07-07
Android 進階實現(xiàn)性能優(yōu)化之OOM與Leakcanary詳解原理
LeakCanary 是大名鼎鼎的 square 公司開源的內(nèi)存泄漏檢測工具。目前上大部分App在開發(fā)測試階段都會接入此工具用于檢測潛在的內(nèi)存泄漏問題,做的好一點的可能會搭建一個服務(wù)器用于保存各個設(shè)備上的內(nèi)存泄漏問題再集中處理2021-11-11

