日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > Android >内容正文

Android

Android简易实战教程--第四十七话《使用OKhttp回调方式获取网络信息》

發(fā)布時(shí)間:2025/3/14 Android 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android简易实战教程--第四十七话《使用OKhttp回调方式获取网络信息》 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

在之前的小案例中寫(xiě)過(guò)一篇使用HttpUrlConnection獲取網(wǎng)絡(luò)數(shù)據(jù)的例子。在OKhttp盛行的時(shí)代,當(dāng)然要學(xué)會(huì)怎么使用它,本篇就對(duì)其基本使用做一個(gè)介紹,然后再使用它的接口回調(diào)的方式獲取相同的數(shù)據(jù)。因?yàn)樗庋b的很好了,并不需要我們?nèi)プ龇庋b,只需要寫(xiě)少量的代碼就可以獲取到復(fù)雜的網(wǎng)絡(luò)數(shù)據(jù)了。

一、OKhttp的最基本使用。

還是直接使用代碼來(lái)說(shuō)話:

1、添加依賴:

Github網(wǎng)址:https://github.com/square/okhttp

compile 'com.squareup.okhttp3:okhttp:3.5.0'

2、等待構(gòu)建成功后:在主活動(dòng)中直接使用它的API

1、創(chuàng)建布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/send_request" 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>

2、主活動(dòng)中的代碼:

public class MainActivity extends Activity implements View.OnClickListener {private Button sendRequest; private TextView responseText; @Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sendRequest = (Button) findViewById(R.id.send_request); responseText = (TextView) findViewById(R.id.response); sendRequest.setOnClickListener(this); }@Override public void onClick(View v) {if (v.getId() == R.id.send_request) {sendRequestWithOKHttp(); }}private void sendRequestWithOKHttp() {// 開(kāi)啟線程來(lái)發(fā)起網(wǎng)絡(luò)請(qǐng)求 new Thread(new Runnable() {@Override public void run() {try {OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder().url("http://www.baidu.com").build(); Response response = client.newCall(request).execute(); String responseData = response.body().string(); show(responseData); } catch (IOException e) {e.printStackTrace(); }}}).start(); }public void show(final String finalDatas){runOnUiThread(new Runnable() {@Override public void run() {responseText.setText(finalDatas); }}); }}

這樣我們就成功獲取到了網(wǎng)絡(luò)的數(shù)據(jù),運(yùn)行程序我們可以看到:


二、使用OKhttp提供的接口回調(diào)

1、定義一個(gè)HttpUtils類。加入如下靜態(tài)方法:

//使用OKhttp,OKhttp給封裝好了回調(diào),我們直接使用即可 public static void sendOKHttpRequst(final String address, final okhttp3.Callback callback){new Thread(new Runnable() {@Override public void run() {OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder().url(address).build(); client.newCall(request).enqueue(callback); }}).start(); }

2、使用工具類,使用回調(diào)方法

//使用OKhttp工具類 findViewById(R.id.btn_okhttp).setOnClickListener(new View.OnClickListener() {@Override public void onClick(View v) {HttpUtils.sendOKHttpRequst(address, new Callback() {@Override public void onFailure(Call call, IOException e) {//對(duì)異常情況處理 Log.e("MainActivity","錯(cuò)誤信息是" + e.toString()); }@Override public void onResponse(Call call, final Response response) throws IOException {//得到服務(wù)器返回的數(shù)據(jù)response runOnUiThread(new Runnable() {@Override public void run() {try {mTextView.setText(response.body().string()); } catch (IOException e) {e.printStackTrace(); }}}); }}); }}); }

這里只需要給出主活動(dòng)的代碼即可~,因?yàn)榉庋b的太好,我們只需要幾行代碼就可以完成功能。還需要注意的是,谷歌工程師給封裝業(yè)務(wù)邏輯是在子線程執(zhí)行的,因此我們要更新數(shù)據(jù)要在主線程執(zhí)行。運(yùn)行程序接口是一樣的~


喜歡我的朋友可以關(guān)注我,本專欄不定期更新簡(jiǎn)單有趣的安卓小文~

對(duì)于OKHttp更高級(jí)的用法,以后會(huì)在Android Studio精彩案例專欄里面進(jìn)行細(xì)致詳細(xì)的分析



轉(zhuǎn)載于:https://www.cnblogs.com/wanghang/p/6299475.html

總結(jié)

以上是生活随笔為你收集整理的Android简易实战教程--第四十七话《使用OKhttp回调方式获取网络信息》的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。