author:wangyimiao

desc:网络请求样例DEMO
master
yimiao 3 years ago
parent e7aa8f71cd
commit 5d8da4bff7

@ -39,9 +39,23 @@ android {
} }
dependencies { dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1' implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1' implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
implementation project(path: ':commonLib') implementation project(path: ':commonLib')
// kotlin
implementation rootProject.ext.dependencies.kotlin
// okhttp
implementation rootProject.ext.dependencies.okhttp
// retrofit
implementation rootProject.ext.dependencies.retrofit
// retrofitgson
implementation rootProject.ext.dependencies.converter_gson
// rxjava
implementation rootProject.ext.dependencies.rxjava
// rxAndroid
implementation rootProject.ext.dependencies.rxandroid
// retrofitrxjava
implementation rootProject.ext.dependencies.retrofit_rxjava
} }

@ -3,6 +3,7 @@
package="com.yinuo.commonlibtest"> package="com.yinuo.commonlibtest">
<application <application
android:name=".MyApplication"
android:allowBackup="true" android:allowBackup="true"
android:icon="@mipmap/ic_launcher" android:icon="@mipmap/ic_launcher"
android:label="@string/app_name" android:label="@string/app_name"

@ -3,15 +3,46 @@ package com.yinuo.commonlibtest;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import com.common.commonlib.net.RequestCallBack;
import com.yinuo.commonlibtest.bean.ArticlesResponse;
import com.yinuo.commonlibtest.databinding.ActivityMainBinding; import com.yinuo.commonlibtest.databinding.ActivityMainBinding;
import com.yinuo.commonlibtest.manager.InternetLoader;
import org.jetbrains.annotations.Nullable;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
ActivityMainBinding binding;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater()); binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot()); setContentView(binding.getRoot());
initView();
}
private void initView() {
binding.tv.setOnClickListener(l -> {
InternetLoader loader = new InternetLoader();
loader.getHomeArticles(0, new RequestCallBack<ArticlesResponse>() {
@Override
public void onResult(ArticlesResponse result) {
Log.d(TAG, result.getData().toString());
}
@Override
public void onError(@Nullable String error) {
Log.d(TAG, "onError");
}
@Override
public void onComplete() {
Log.d(TAG, "onComplete");
}
});
});
} }
} }

@ -0,0 +1,12 @@
package com.yinuo.commonlibtest
import android.app.Application
import com.yinuo.commonlib.CommonApplication
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
CommonApplication.intLibs(this)
}
}

@ -0,0 +1,43 @@
package com.yinuo.commonlibtest.bean
import android.os.Parcelable
import kotlinx.android.parcel.Parcelize
/**
* 文章数据类
*/
@Parcelize
data class Article(var apkLink: String = ""): Parcelable {
var audit: Int = 0
var author: String = ""
var canEdit: Boolean = false
var chapterId: Int = 0
var chapterName: String = ""
var collect: Boolean = false
var courseId: Int = 0
var desc: String = ""
var descMd: String = ""
var envelopePic: String = ""
var fresh: Boolean = false
var host: String = ""
var id: Long = 0
var link: String = ""
var niceDate: String = ""
var niceShareDate: String = ""
var origin: String = ""
var prefix: String = ""
var projectLink: String = ""
var publishTime: Long = 0
var realSuperChapterId: Long = 0
var selfVisible: Int = 0
var shareDate: Long = 0
var shareUser: String = ""
var superChapterId: Long = 0
var superChapterName: String = ""
var tags: List<ArticleTag>? = null
var title: String = ""
var type: Int = 0
var userId: Long = 0
var visible: Int = 0
var zan: Int = 0
}

@ -0,0 +1,10 @@
package com.yinuo.commonlibtest.bean
import android.os.Parcelable
import kotlinx.android.parcel.Parcelize
@Parcelize
data class ArticleTag(
var name: String? = "",
var url: String? = ""
) : Parcelable

@ -0,0 +1,15 @@
package com.yinuo.commonlibtest.bean
import android.os.Parcelable
import com.yinuo.commonlibtest.bean.Article
import kotlinx.android.parcel.Parcelize
@Parcelize
data class Articles(var curPage: Int) : Parcelable {
var datas: List<Article>? = null
var offset: Int = 0
var over: Boolean = false
var pageCount: Long = 0
var size: Int = 0
var total: Long = 0
}

@ -0,0 +1,8 @@
package com.yinuo.commonlibtest.bean
import android.os.Parcelable
import com.yinuo.commonlib.net.bean.BaseResponse
import kotlinx.android.parcel.Parcelize
@Parcelize
data class ArticlesResponse(var data: Articles? = null): Parcelable, BaseResponse()

@ -0,0 +1,27 @@
package com.yinuo.commonlibtest.manager
import com.common.commonlib.net.RequestCallBack
import com.yinuo.commonlib.net.BaseObserve
import com.yinuo.commonlibtest.bean.ArticlesResponse
import io.reactivex.Observable
import retrofit2.http.GET
import retrofit2.http.Headers
import retrofit2.http.Path
class InternetLoader : BaseObserve<InternetLoader.HomeApi>() {
var homeApi: HomeApi
init {
homeApi = initService(HomeApi::class.java)
}
fun getHomeArticles(pageName: Int, callBack: RequestCallBack<ArticlesResponse>) {
observe(homeApi.getHomeArticles(pageName), callBack)
}
interface HomeApi {
@Headers("baseurl:debug1")
@GET("/article/list/{pageNum}/json")
fun getHomeArticles(@Path("pageNum") pageNum: Int): Observable<ArticlesResponse>
}
}

@ -7,6 +7,7 @@
tools:context=".MainActivity"> tools:context=".MainActivity">
<TextView <TextView
android:id="@+id/tv"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Hello World!" android:text="Hello World!"

@ -2,4 +2,5 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yinuo.commonlib"> package="com.yinuo.commonlib">
<uses-permission android:name="android.permission.INTERNET" />
</manifest> </manifest>

@ -1,10 +1,11 @@
package com.yinuo.commonlib package com.yinuo.commonlib
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.app.Application
import android.content.Context import android.content.Context
@SuppressLint("StaticFieldLeak") @SuppressLint("StaticFieldLeak")
object CommonApplication { object CommonApplication : Application() {
private var commonContext: Context? = null private var commonContext: Context? = null
fun intLibs(context: Context) { fun intLibs(context: Context) {

@ -1,7 +1,9 @@
package com.common.commonlib.net package com.yinuo.commonlib.net
import android.net.ParseException import android.net.ParseException
import android.util.Log import android.util.Log
import com.common.commonlib.net.RequestCallBack
import com.common.commonlib.net.RequestManager
import com.google.gson.JsonParseException import com.google.gson.JsonParseException
import io.reactivex.Observable import io.reactivex.Observable
import io.reactivex.Observer import io.reactivex.Observer

Loading…
Cancel
Save