Skip to content

Commit

Permalink
'feat(sample_0):初始化项目'
Browse files Browse the repository at this point in the history
  • Loading branch information
Knight-ZXW authored and zhuoxiuwu committed Apr 25, 2019
1 parent 66a368a commit 8c5443b
Show file tree
Hide file tree
Showing 43 changed files with 954 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/.gitignore
@@ -0,0 +1 @@
/build
33 changes: 33 additions & 0 deletions app/build.gradle
@@ -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'
}
21 changes: 21 additions & 0 deletions app/proguard-rules.pro
@@ -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
@@ -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());
}
}
22 changes: 22 additions & 0 deletions app/src/main/AndroidManifest.xml
@@ -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>
73 changes: 73 additions & 0 deletions app/src/main/java/com/knight/sample/MainActivity.java
@@ -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);
}
});
}
}
13 changes: 13 additions & 0 deletions app/src/main/java/com/knight/sample/NetCallback.java
@@ -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);
}
64 changes: 64 additions & 0 deletions app/src/main/java/com/knight/sample/RestService.java
@@ -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 app/src/main/java/com/knight/sample/entity/BaseResponse.java
@@ -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 app/src/main/java/com/knight/sample/entity/GankEntity.java
@@ -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;
}
@@ -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>>> {

}
@@ -0,0 +1,7 @@
package com.knight.sample.entity;

import java.util.List;
import java.util.Map;

public class XianduResponse extends BaseResponse<List<GankEntity>> {
}

0 comments on commit 8c5443b

Please sign in to comment.