JetPack——网络库封装
生活随笔
收集整理的這篇文章主要介紹了
JetPack——网络库封装
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
目錄
- 1、項(xiàng)目層級(jí)
- 2、activity_main.xml
- 3、MainActivity
- 4、AndroidManifest.xml
- 5、build.gradle
1、項(xiàng)目層級(jí)
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(){//開啟線程發(fā)起網(wǎng)絡(luò)請(qǐng)求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() {//在這里進(jìn)行UI操作,將結(jié)果顯示到界面上responseText.setText(response);}});} }4、AndroidManifest.xml
聲明權(quán)限:<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' }總結(jié)
以上是生活随笔為你收集整理的JetPack——网络库封装的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Dialog练习
- 下一篇: 【Jetpack 】