Last active
October 28, 2019 02:36
-
-
Save kaleai/2853c097762a203cd1d024c1f846dcc3 to your computer and use it in GitHub Desktop.
SonarDemo
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
apply plugin: 'com.android.application' | |
repositories { | |
google() | |
jcenter() | |
// 1. add facebook maven | |
maven { url 'https://dl.bintray.com/facebook/maven/' } | |
} | |
android { | |
compileSdkVersion 28 | |
defaultConfig { | |
applicationId "kale.myapplication" | |
minSdkVersion 16 | |
targetSdkVersion 28 | |
// 2. ndk support | |
ndk { | |
abiFilters "armeabi-v7a", "x86" | |
} | |
} | |
} | |
// 处理support版本和build tools版本的问题,如果运行没有问题则无需配置 | |
configurations.all { | |
resolutionStrategy.eachDependency { DependencyResolveDetails details -> | |
def requested = details.requested | |
if (requested.group == "com.android.support") { | |
if (!requested.name.startsWith("multidex")) { | |
details.useVersion "26.+" | |
} | |
} | |
} | |
} | |
dependencies { | |
implementation 'com.android.support:appcompat-v7:28.0.0-alpha3' | |
debugImplementation 'com.facebook.sonar:sonar:0.0.9' // add sonar lib | |
} | |
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
public class HttpManager { | |
private static final String TAG = "HttpManager"; | |
private static HttpManager mInstance = null; | |
private OkHttpClient httpClient; | |
public static HttpManager getInstance() { | |
if (mInstance == null) { | |
synchronized (HttpManager.class) { | |
if (mInstance == null) { | |
mInstance = new HttpManager(); | |
} | |
} | |
} | |
return mInstance; | |
} | |
private HttpManager() { | |
httpClient = new OkHttpClient.Builder() | |
.connectTimeout(3, TimeUnit.SECONDS) | |
.readTimeout(3, TimeUnit.SECONDS) | |
.writeTimeout(3, TimeUnit.MINUTES) | |
.build(); | |
} | |
public void setInterceptor(Interceptor interceptor) { | |
httpClient = httpClient.newBuilder().addInterceptor(interceptor).build(); | |
} | |
public void sendRequest(String url) { | |
Request request = new Request.Builder().url(url).build(); | |
httpClient.newCall(request).enqueue(new Callback() { | |
@Override | |
public void onFailure(@NonNull Call call, @NonNull IOException e) { | |
Log.e(TAG, "onFailure: " + call.request().url()); | |
} | |
@Override | |
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException { | |
Log.d(TAG, "onResponse: " + call.request().url()); | |
Log.d(TAG, "onResponse: " + response.body().string()); | |
} | |
}); | |
} | |
} |
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
public class MyApplication extends Application { | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
try { | |
SoLoader.init(this, 0); | |
if (BuildConfig.DEBUG && SonarUtils.shouldEnableSonar(this)) { | |
// create once | |
final SonarClient client = AndroidSonarClient.getInstance(this); | |
// support layout plugin | |
final DescriptorMapping descriptorMapping = DescriptorMapping.withDefaults(); | |
client.addPlugin(new InspectorSonarPlugin(getApplicationContext(), descriptorMapping)); | |
NetworkSonarPlugin networkPlugin = new NetworkSonarPlugin(); | |
SonarOkhttpInterceptor interceptor = new SonarOkhttpInterceptor(networkPlugin); | |
client.addPlugin(networkPlugin); | |
HttpManager.getInstance().setInterceptor(interceptor); | |
client.start(); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
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
// test url list | |
public static final String[] URLS = { | |
"http://gank.io/api/xiandu/categories", | |
"http://gank.io/api/xiandu/category/wow", | |
"http://gank.io/api/xiandu/data/id/appinn/count/10/page/1", | |
"http://gank.io/api/search/query/listview/category/Android/count/10/page/1", | |
"http://gank.io/api/history/content/day/2016/05/11", | |
"http://gank.io/api/day/history" | |
}; | |
HttpManager.getInstance().sendRequest(URLS[new Random().nextInt(URLS.length - 1)]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment