[安卓] Android OKHttp发送get网络请求实例
作者:精品下载站 日期:2020-09-23 00:00:00 浏览:86 分类:编程开发
今天总算把安卓的网络请求弄了一下了。
获取的是我自己做的接口: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
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[短剧合集] 2025年06月2日 精选+付费短剧推荐39部
[软件合集] 25年6月2日 精选软件18个
[软件合集] 25年6月1日 精选软件15个
[短剧合集] 2025年06月1日 精选+付费短剧推荐59部
[短剧] 2025年05月31日 精选+付费短剧推荐58部
[软件合集] 25年5月31日 精选软件66个
[电影] 黄沙漫天(2025) 4K.EDRMAX.杜比全景声 / 4K杜比视界/杜比全景声
[风口福利] 短视频红利新风口!炬焰创作者平台重磅激励来袭
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[美图] 2W美女个美女小姐姐,饱眼福
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag