Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
66a368a
commit 8c5443b
Showing
43 changed files
with
954 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
apply plugin: 'com.android.application' | ||
|
||
android { | ||
compileSdkVersion 28 | ||
defaultConfig { | ||
applicationId "com.knight.sample" | ||
minSdkVersion 19 | ||
targetSdkVersion 28 | ||
versionCode 1 | ||
versionName "1.0" | ||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" | ||
} | ||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation fileTree(dir: 'libs', include: ['*.jar']) | ||
implementation 'com.android.support:appcompat-v7:28.0.0-rc02' | ||
implementation 'com.android.support.constraint:constraint-layout:1.1.3' | ||
implementation 'com.squareup.okhttp3:okhttp:3.12.0' | ||
implementation 'com.google.code.gson:gson:2.8.5' | ||
testImplementation 'junit:junit:4.12' | ||
androidTestImplementation 'com.android.support.test:runner:1.0.2' | ||
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' | ||
annotationProcessor 'org.projectlombok:lombok:1.18.2' | ||
|
||
compileOnly 'org.projectlombok:lombok:1.18.2' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
26 changes: 26 additions & 0 deletions
26
app/src/androidTest/java/com/knight/sample/ExampleInstrumentedTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.knight.sample; | ||
|
||
import android.content.Context; | ||
import android.support.test.InstrumentationRegistry; | ||
import android.support.test.runner.AndroidJUnit4; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* Instrumented test, which will execute on an Android device. | ||
* | ||
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a> | ||
*/ | ||
@RunWith(AndroidJUnit4.class) | ||
public class ExampleInstrumentedTest { | ||
@Test | ||
public void useAppContext() { | ||
// Context of the app under test. | ||
Context appContext = InstrumentationRegistry.getTargetContext(); | ||
|
||
assertEquals("com.knight.sample", appContext.getPackageName()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.knight.sample"> | ||
|
||
<uses-permission android: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"> | ||
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.knight.sample; | ||
|
||
import android.support.v7.app.AppCompatActivity; | ||
import android.os.Bundle; | ||
import android.view.View; | ||
import android.widget.TextView; | ||
|
||
import com.google.gson.Gson; | ||
import com.knight.sample.entity.TodayGankResponse; | ||
import com.knight.sample.entity.XianduResponse; | ||
|
||
public class MainActivity extends AppCompatActivity implements View.OnClickListener { | ||
|
||
private Gson gson; | ||
private TextView resultTextView; | ||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_main); | ||
RestService.init(); | ||
gson = new Gson(); | ||
findViewById(R.id.btn_test_1) | ||
.setOnClickListener(this); | ||
findViewById(R.id.btn_test_2) | ||
.setOnClickListener(this); | ||
resultTextView = findViewById(R.id.tv_result); | ||
} | ||
|
||
@Override | ||
public void onClick(View v) { | ||
switch (v.getId()){ | ||
case R.id.btn_test_1: | ||
RestService.todayGank(TodayGankResponse.class, new NetCallback<TodayGankResponse>() { | ||
@Override | ||
public void onFailure(Exception e) { | ||
|
||
} | ||
|
||
@Override | ||
public void onSuccess(TodayGankResponse data) { | ||
showHttpResult(data.toString()); | ||
|
||
} | ||
}); | ||
break; | ||
case R.id.btn_test_2: | ||
RestService.xianduGank(10, 1, XianduResponse.class, new NetCallback<XianduResponse>() { | ||
@Override | ||
public void onFailure(Exception e) { | ||
|
||
} | ||
|
||
@Override | ||
public void onSuccess(XianduResponse data) { | ||
showHttpResult(data.toString()); | ||
|
||
} | ||
}); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
private void showHttpResult(final String msg){ | ||
resultTextView.post(new Runnable() { | ||
@Override | ||
public void run() { | ||
resultTextView.setText(msg); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.knight.sample; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* 项目封装的统一网络请求的回调 | ||
* @param <T> | ||
*/ | ||
public interface NetCallback<T> { | ||
void onFailure(Exception e); | ||
|
||
void onSuccess(T data); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.knight.sample; | ||
|
||
import android.util.Log; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.stream.JsonReader; | ||
|
||
import java.io.IOException; | ||
|
||
import okhttp3.Call; | ||
import okhttp3.Callback; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
|
||
public class RestService { | ||
private static OkHttpClient okHttpClient; | ||
public static void init() { | ||
okHttpClient = new OkHttpClient.Builder() | ||
.build(); | ||
} | ||
|
||
public static<T> void todayGank(Class<T> responseClazz,NetCallback<T> callback) { | ||
Request request = new Request.Builder().url("http://gank.io/api/today") | ||
.get() | ||
.build(); | ||
okHttpClient.newCall(request).enqueue(new WrapperOkHttpCallback<>(responseClazz,callback)); | ||
} | ||
|
||
public static<T> void xianduGank(int count, int page,Class<T> responseClazz,NetCallback<T> callback) { | ||
Request request = new Request.Builder() | ||
.url("http://gank.io/api/xiandu/data/id/appinn/count/" + count + "/page/" + page) | ||
.get().build(); | ||
okHttpClient.newCall(request).enqueue(new WrapperOkHttpCallback<>(responseClazz,callback)); | ||
} | ||
|
||
static class WrapperOkHttpCallback<T> implements Callback { | ||
private static Gson gson = new Gson(); | ||
private Class<T> clazz; | ||
private NetCallback<T> callback; | ||
|
||
public WrapperOkHttpCallback(Class<T> responseClazz, NetCallback<T> netCallback) { | ||
this.clazz = responseClazz; | ||
this.callback = netCallback; | ||
} | ||
|
||
@Override | ||
public void onFailure(Call call, IOException e) { | ||
Log.e("WrapperOkHttpCallback", "onFailure"); | ||
e.printStackTrace(); | ||
callback.onFailure(e); | ||
} | ||
|
||
@Override | ||
public void onResponse(Call call, Response response) throws IOException { | ||
JsonReader jsonReader = gson.newJsonReader(response.body().charStream()); | ||
T entity = gson.getAdapter(clazz).read(jsonReader); | ||
Log.d("response", entity.toString()); | ||
callback.onSuccess(entity); | ||
|
||
} | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
app/src/main/java/com/knight/sample/entity/BaseResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.knight.sample.entity; | ||
|
||
import lombok.Data; | ||
import lombok.ToString; | ||
|
||
@Data | ||
@ToString | ||
public class BaseResponse<T> { | ||
private boolean error; | ||
private T results; | ||
} |
15 changes: 15 additions & 0 deletions
15
app/src/main/java/com/knight/sample/entity/GankEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.knight.sample.entity; | ||
|
||
import lombok.Data; | ||
import lombok.ToString; | ||
|
||
@Data | ||
@ToString | ||
public class GankEntity { | ||
//文章地址 | ||
private String url; | ||
//文章概况描述 | ||
private String desc; | ||
//作者 | ||
private String who; | ||
} |
8 changes: 8 additions & 0 deletions
8
app/src/main/java/com/knight/sample/entity/TodayGankResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.knight.sample.entity; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class TodayGankResponse extends BaseResponse<Map<String, List<GankEntity>>> { | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
app/src/main/java/com/knight/sample/entity/XianduResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.knight.sample.entity; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class XianduResponse extends BaseResponse<List<GankEntity>> { | ||
} |
Oops, something went wrong.