[安卓] Android OKHttp发送get网络请求实例
作者:CC下载站 日期:2020-09-23 00:00:00 浏览:56 分类:编程开发
今天总算把安卓的网络请求弄了一下了。
获取的是我自己做的接口:https://api.565.ink/one/
随机一句英语,不得不说换一门语言,写法上真的有点不适应。
MainActivify.java
packageink.cik.firsthttpapp; importandroid.os.Bundle; importandroid.util.Log; importandroid.view.View; importandroid.widget.Button; importandroid.widget.TextView; importandroidx.appcompat.app.AppCompatActivity; importjava.io.IOException; importokhttp3.OkHttpClient; importokhttp3.Request; importokhttp3.Response; publicclassMainActivityextendsAppCompatActivity{ TextViewresponseText; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Buttonbutton=(Button)findViewById(R.id.btnSyncTest); responseText=findViewById(R.id.response_text); button.setOnClickListener(newView.OnClickListener(){ @Override publicvoidonClick(Viewv){ Log.d("点击事件","点击发送请求按钮"); sendRequest(); } }); } privatevoidsendRequest(){ //开启线程发送请求 newThread(newRunnable(){ @Override publicvoidrun(){ try{ OkHttpClientclient=newOkHttpClient(); Requestrequest=newRequest.Builder().url("https://api.565.ink/one/").build(); Responseresponse=client.newCall(request).execute(); StringresponseData=response.body().string(); showResponse(responseData); }catch(IOExceptione){ e.printStackTrace(); } } }).start(); } privatevoidshowResponse(finalStringresponse){ runOnUiThread(newRunnable(){ @Override publicvoidrun(){ responseText.setText(response); } }); } }
AndroidMainifest.xml
<?xmlversion="1.0"encoding="utf-8"?> <manifestxmlns:android="http://schemas.android.com/apk/res/android" package="ink.cik.firsthttpapp"> <uses-permissionandroid: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:theme="@style/AppTheme"> <activityandroid:name=".MainActivity"> <intent-filter> <actionandroid:name="android.intent.action.MAIN"/> <categoryandroid:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest>
activity_main.xml
<?xmlversion="1.0"encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="10dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/btnSyncTest" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="同步测试"/> <Button android:id="@+id/btnAsyncTest" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="异步测试"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:id="@+id/etId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="6" android:hint="输入用户编号" android:inputType="number"/> <Button android:id="@+id/btnParamTest" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="doParamTest" android:text="传参测试"/> </LinearLayout> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/response_text" android:layout_width="match_parent" android:layout_height="match_parent"/> </ScrollView> </LinearLayout>
build.gradle
applyplugin:'com.android.application' android{ compileSdkVersion30 buildToolsVersion"30.0.2" defaultConfig{ applicationId"ink.cik.firsthttpapp" minSdkVersion30 targetSdkVersion30 versionCode1 versionName"1.0" testInstrumentationRunner"androidx.test.runner.AndroidJUnitRunner" } buildTypes{ release{ minifyEnabledfalse proguardFilesgetDefaultProguardFile('proguard-android-optimize.txt'),'proguard-rules.pro' } } } dependencies{ implementationfileTree(dir:'libs',include:['*.jar']) implementation'androidx.appcompat:appcompat:1.2.0' implementation'androidx.constraintlayout:constraintlayout:2.0.1' testImplementation'junit:junit:4.12' androidTestImplementation'androidx.test.ext:junit:1.1.2' androidTestImplementation'androidx.test.espresso:espresso-core:3.3.0' implementation'com.squareup.okhttp3:okhttp:4.9.0' implementation'com.squareup.okio:okio:2.8.0' }
总结一下踩得一些坑。
坑一:
导入okhttp包导错了地方。
书上说,要在build.gradle中导入okhttp和okio
于是乎。。这两个里面果断的选择了第一个,因为名字是自己取的,按照python来说,默认的都是系统的。
结果出错了,其实是Module:app这个
坑二:本来我是本地搭建的环境,然后电脑端自己修改的hosts,将lan指向了127.0.0.1于是乎。
坑三:原本用的http,结果不行,百度发现得https
猜你还喜欢
- 03-29 [编程相关] Winform窗体圆角以及描边完美解决方案
- 03-29 [前端问题] has been blocked by CORS policy跨域问题解决
- 03-29 [编程相关] GitHub Actions 入门教程
- 03-29 [编程探讨] CSS Grid 网格布局教程
- 10-12 [编程相关] python实现文件夹所有文件编码从GBK转为UTF8
- 10-11 [编程算法] opencv之霍夫变换:圆
- 10-11 [编程算法] OpenCV Camshift算法+目标跟踪源码
- 10-11 [Python] python 创建 Telnet 客户端
- 10-11 [编程相关] Python 基于 Yolov8 + CPU 实现物体检测
- 03-15 [脚本工具] 使用go语言开发自动化脚本 - 一键定场、抢购、预约、捡漏
- 01-08 [编程技术] 秒杀面试官系列 - Redis zset底层是怎么实现的
- 01-05 [编程技术] 《Redis设计与实现》pdf
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[壁纸工具] 每日必应壁纸爬取
[文件转换] Unlock Music 音乐解锁---搬运工---使用过最好的音乐格式转换全能工具
[图床工具] 百家号变免费图床
[行业软件] 超市播音系统V9.9(思维构造)——定时播放功能免费
[数码资讯] 松下Lumix S9完整细节曝光
[电视剧] [庆余年 第二季] [更至9集] [WEB-MKV] [国语中字] [4K]
[游戏娱乐] 《漫野奇谭》v1.16.535中文版
[游戏娱乐] 《奥西里斯:新黎明》v1.5.67中文版
[游戏娱乐] 《倾覆之国:最后一战》v1.0.0中文版
[文件传输] 【Android/IOS/Win】互传 EasyShare 3.6.5 零流量、极速、多平台快捷传输工具
[资料] [大学期末救急课] 猴博士+高斯课堂+斐多课堂,全集视频合集
[云资源] 价值2万元的老男孩Python教程
[书库] 史上最全摄影书推荐(附700本PDF版打包下载)
[云资源] 花了一千多元买的私人健身教程
[下载工具] Internet Download Manager 6.42.7 (IDM)
[影视] 灌篮高手 WEB-DL版下载/Slam Dunk/スラムダンク/灌篮高手:THE FIRST/灌篮高手电影版 2022 The First Slam Dunk 61.35G
[安卓软件] 酷我音乐APP_v10.7.6.4 去广告破解豪华VIP版
[即时通讯] 微信PC版WeChat 3.9.9.43 多开防撤回绿色版
[安卓软件] Solid Explorer文件管理器APP 2.8.38 破解版
[浏览器] Google Chrome v123.0.6312.59便携增强版
[云资源] 价值2万元的老男孩Python教程
[影视] 灌篮高手 WEB-DL版下载/Slam Dunk/スラムダンク/灌篮高手:THE FIRST/灌篮高手电影版 2022 The First Slam Dunk 61.35G
[云资源] 花了一千多元买的私人健身教程
[书库] 史上最全摄影书推荐(附700本PDF版打包下载)
[动画] 北斗神拳(1984) [两季合集] [MKV]
[资料] 抗战阵亡将士资料+续编
[电视剧] 三体 (2024) 全8集 网飞版本 中文字幕 合集
[纪录片] 河西走廊【10集 国语 中文字幕 1080P 10.8G MP4】
[电影] 2024年喜剧片·热辣滚烫 [mp4]
[影视] 铁爪 WEB-DL版下载 2023 The Iron Claw 23.48G
- 最新评论
-
我想看看mw2ddyy 评论于:04-26 好东西阿zfy123123 评论于:04-18 谢谢楼主xiaoqi 评论于:04-12 勿在线解压,勿手机解压,请在电脑上用最新款压缩软件解压!推荐360压缩或者好压CC下载站 评论于:04-10 无法解压啊,客服能不能给个解压教程ravengrey 评论于:04-10 谢谢支持!!CC下载站 评论于:03-26 很棒的资源,感谢分享云体风身 评论于:03-26 感谢分享,好东西云体风身 评论于:03-26 谢谢支持!CC下载站 评论于:03-14 央视精品,感谢付出提供。qwer9009 评论于:03-14
- 热门tag