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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

JetPack——网络库封装

發布時間:2025/3/13 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JetPack——网络库封装 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

  • 1、項目層級
  • 2、activity_main.xml
  • 3、MainActivity
  • 4、AndroidManifest.xml
  • 5、build.gradle

1、項目層級

2、activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><Buttonandroid:id="@+id/send_request"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Send Request"/><ScrollViewandroid:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:id="@+id/response_text"android:layout_width="match_parent"android:layout_height="wrap_content" /></ScrollView></LinearLayout>

3、MainActivity

package com.zz.networktest;import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView;import java.io.IOException;import okhttp3.FormBody; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response;public class MainActivity extends AppCompatActivity implements View.OnClickListener {TextView responseText;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button sendRequest = (Button) findViewById(R.id.send_request);responseText = (TextView) findViewById(R.id.response_text);sendRequest.setOnClickListener(this);}@Overridepublic void onClick(View v) {if(v.getId() == R.id.send_request){sendRequestWithOkHttp();}}private void sendRequestWithOkHttp(){//開啟線程發起網絡請求new Thread(new Runnable() {@Overridepublic void run() {try{OkHttpClient client = new OkHttpClient();Request request = new Request.Builder().url("http://12.34.56.78:80/index.html").build();Response response = client.newCall(request).execute();String responseata = response.body().string();showResponse(responseata);/* RequestBody requestBody = new FormBody.Builder().add("username","admin").add("password","123456").build();*/} catch (IOException e) {e.printStackTrace();}}}).start();}private void showResponse(final String response){runOnUiThread(new Runnable() {@Overridepublic void run() {//在這里進行UI操作,將結果顯示到界面上responseText.setText(response);}});} }

4、AndroidManifest.xml

聲明權限:<uses-permission android:name="android.permission.INTERNET"/>

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.zz.networktest"><uses-permission android:name="android.permission.INTERNET"/><applicationandroid: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"><activity android:name=".MainActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>

5、build.gradle

添加依賴包:compile 'com.squareup.okhttp3:okhttp:3.4.1'

apply plugin: 'com.android.application'android {compileSdkVersion 25buildToolsVersion "25.0.3"defaultConfig {applicationId "com.zz.networktest"minSdkVersion 15targetSdkVersion 25versionCode 1versionName "1.0"testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"}buildTypes {release {minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'}} }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:25.3.1'compile 'com.android.support.constraint:constraint-layout:1.0.2'testCompile 'junit:junit:4.12'compile 'com.squareup.okhttp3:okhttp:3.4.1' }

總結

以上是生活随笔為你收集整理的JetPack——网络库封装的全部內容,希望文章能夠幫你解決所遇到的問題。

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