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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

安卓 原生okhttp使用get与post获取网络数据

發布時間:2023/12/13 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 安卓 原生okhttp使用get与post获取网络数据 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

網址

https://square.github.io/okhttp/

配置

依賴

在module的build.gradle中:

implementation 'com.squareup.okhttp3:okhttp:3.14.7'implementation 'com.squareup.okio:okio:1.17.5'

AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />

xml文件

<?xml version="1.0" encoding="utf-8"?> <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=".test.TestOKHttpActivity"><Buttonandroid:id="@+id/btn_get_post"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="get和post" /><TextViewandroid:id="@+id/tv_result"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="xx"android:textSize="15sp"/> </LinearLayout>

java代碼

public class TestOKHttpActivity extends AppCompatActivity implements View.OnClickListener {private Button btn_get_post;private TextView tv_result;private OkHttpClient client = new OkHttpClient();//設置編碼類型public static final MediaType JSON= MediaType.get("application/json; charset=utf-8");//get和post請求標識private static final int GET = 1;private static final int POST = 2;//傳遞消息private Handler handler = new Handler() {@Overridepublic void handleMessage(@NonNull Message msg) {super.handleMessage(msg);//區分傳遞的是get還是post消息switch (msg.what) {case GET://獲取數據tv_result.setText((String) msg.obj);break;case POST:tv_result.setText((String) msg.obj);break;}}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.test_okhttp);init();}private void init() {//使用原生的okhttp請求網絡數據,get和postbtn_get_post = findViewById(R.id.btn_get_post);tv_result = findViewById(R.id.tv_result);btn_get_post.setOnClickListener(this);}@Overridepublic void onClick(View view) {switch (view.getId()) {//使用原生的okhttp請求網絡數據,get和postcase R.id.btn_get_post://getDataFromGet();//開啟子線程,獲取getgetDataFromPost();//使用post獲取數據break;}}//從子線程中獲取網絡數據private void getDataFromGet() {String url = "http://47.108.249.115:8080/LuShuDao0.0.2-1.0-SNAPSHOT/UserInfo/loginUser";new Thread() {@Overridepublic void run() {super.run();String result = null;try {result = get(url);Log.e("Test", result);Message message = Message.obtain();message.what = GET;message.obj = result;handler.sendMessage(message);} catch (IOException e) {e.printStackTrace();}}}.start();}/*okhttp必須在子線程執行get請求url:請求網址*/private String get(String url) throws IOException {Request request = new Request.Builder().url(url).build();try (Response response = client.newCall(request).execute()) {return response.body().string();}}//post請求,上傳數據,在子線程中執行private String post(String url, String json) throws IOException {RequestBody body = RequestBody.create(JSON, json);Request request = new Request.Builder().url(url).post(body).build();try (Response response = client.newCall(request).execute()) {return response.body().string();}}//從子線程中獲取網絡數據private void getDataFromPost() {String url = "http://47.108.249.115:8080/LuShuDao0.0.2-1.0-SNAPSHOT/UserInfo/loginUser";new Thread() {@Overridepublic void run() {super.run();String result = null;try {//json是上傳數據result = post(url, "");Log.e("Test", result);Message message = Message.obtain();message.what = POST;message.obj = result;handler.sendMessage(message);} catch (IOException e) {e.printStackTrace();}}}.start();} }

總結

原生okhttp比較麻煩,使用第三方庫去封裝使用

總結

以上是生活随笔為你收集整理的安卓 原生okhttp使用get与post获取网络数据的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。