Skip to content

Commit

Permalink
添加测试: 从网上获取数据后展示banner ANR
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouwei committed Jun 10, 2017
1 parent bf521ba commit c07a2a8
Show file tree
Hide file tree
Showing 20 changed files with 759 additions and 4 deletions.
16 changes: 14 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ dependencies {
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 project(':mzbanner')
compile 'com.github.pinguo-zhouwei:MZBannerView:v1.0.2'
compile project(':mzbanner')
// compile 'com.github.pinguo-zhouwei:MZBannerView:v1.0.2'

compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.google.code.gson:gson:2.6.2'
compile 'io.reactivex:rxjava:1.1.0'
compile 'io.reactivex:rxandroid:1.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'

compile 'com.squareup.picasso:picasso:2.5.2'

compile 'com.github.anrwatchdog:anrwatchdog:1.3.0'

}
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zhouwei.mzbannerview">

<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:name=".MZApplication"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/java/com/zhouwei/mzbannerview/AppConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.zhouwei.mzbannerview;

/**
* Created by zhouwei on 17/6/8.
*/

public class AppConfig {
/**
* 服务器地址,应该根据 DEBUG 判断选用哪个环境
*/
public static final String BASE_URL = "https://api.douban.com/v2/movie/";
}
16 changes: 16 additions & 0 deletions app/src/main/java/com/zhouwei/mzbannerview/MZApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.zhouwei.mzbannerview;

import android.app.Application;

/**
* Created by zhouwei on 17/6/10.
*/

public class MZApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
//ANR检测
// new ANRWatchDog().start();
}
}
11 changes: 10 additions & 1 deletion app/src/main/java/com/zhouwei/mzbannerview/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import com.zhouwei.mzbannerview.remote.RemoteTestFragment;

public class MainActivity extends AppCompatActivity {
private Toolbar mToolbar;
@Override
Expand All @@ -21,8 +23,10 @@ protected void onCreate(Bundle savedInstanceState) {
public boolean onMenuItemClick(MenuItem item) {
if(item.getItemId() ==R.id.banner_mode){
switchBannerMode();
}else{
}else if(item.getItemId() == R.id.viewPager_mode){
switchViewPagerMode();
}else if(item.getItemId() == R.id.remote_mode){
switchRemoteMode();
}
return true;
}
Expand All @@ -47,4 +51,9 @@ public void switchViewPagerMode(){
Fragment fragment = NormalViewPagerFragment.newInstance();
getSupportFragmentManager().beginTransaction().replace(R.id.home_container,fragment).commit();
}

public void switchRemoteMode(){
Fragment fragment = RemoteTestFragment.newInstance();
getSupportFragmentManager().beginTransaction().replace(R.id.home_container,fragment).commit();
}
}
18 changes: 18 additions & 0 deletions app/src/main/java/com/zhouwei/mzbannerview/http/BaseResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.zhouwei.mzbannerview.http;

/**
*
* 网络请求结果 基类
* Created by zhouwei on 16/11/10.
*/

public class BaseResponse<T> {
public int status;
public String message;

public T data;

public boolean isSuccess(){
return status == 200;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
package com.zhouwei.mzbannerview.http;

import android.text.TextUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okio.Buffer;

/**
*
* OkHttp 公共参数拦截器
* from: https://github.com/jkyeo/okhttp-basicparamsinterceptor.git
* Created by zhouwei on 16/11/10.
*/

public class BasicParamsInterceptor implements Interceptor {

Map<String, String> queryParamsMap = new HashMap<>();
Map<String, String> paramsMap = new HashMap<>();
Map<String, String> headerParamsMap = new HashMap<>();
List<String> headerLinesList = new ArrayList<>();

private BasicParamsInterceptor() {

}

@Override
public Response intercept(Chain chain) throws IOException {

Request request = chain.request();
Request.Builder requestBuilder = request.newBuilder();

// process header params inject
Headers.Builder headerBuilder = request.headers().newBuilder();
if (headerParamsMap.size() > 0) {
Iterator iterator = headerParamsMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
headerBuilder.add((String) entry.getKey(), (String) entry.getValue());
}
}

if (headerLinesList.size() > 0) {
for (String line: headerLinesList) {
headerBuilder.add(line);
}
requestBuilder.headers(headerBuilder.build());
}
// process header params end


// process queryParams inject whatever it's GET or POST
if (queryParamsMap.size() > 0) {
request = injectParamsIntoUrl(request.url().newBuilder(), requestBuilder, queryParamsMap);
}

// process post body inject
if (paramsMap.size() > 0) {
if (canInjectIntoBody(request)) {
FormBody.Builder formBodyBuilder = new FormBody.Builder();
for(Map.Entry<String, String> entry : paramsMap.entrySet()) {
formBodyBuilder.add((String) entry.getKey(), (String) entry.getValue());
}

RequestBody formBody = formBodyBuilder.build();
String postBodyString = bodyToString(request.body());
postBodyString += ((postBodyString.length() > 0) ? "&" : "") + bodyToString(formBody);
requestBuilder.post(RequestBody.create(MediaType.parse("application/x-www-form-urlencoded;charset=UTF-8"), postBodyString));
}
}

request = requestBuilder.build();
return chain.proceed(request);
}
private boolean canInjectIntoBody(Request request) {
if (request == null) {
return false;
}
if (!TextUtils.equals(request.method(), "POST")) {
return false;
}
RequestBody body = request.body();
if (body == null) {
return false;
}
MediaType mediaType = body.contentType();
if (mediaType == null) {
return false;
}
if (!TextUtils.equals(mediaType.subtype(), "x-www-form-urlencoded")) {
return false;
}
return true;
}

// func to inject params into url
private Request injectParamsIntoUrl(HttpUrl.Builder httpUrlBuilder, Request.Builder requestBuilder, Map<String, String> paramsMap) {
if (paramsMap.size() > 0) {
Iterator iterator = paramsMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
httpUrlBuilder.addQueryParameter((String) entry.getKey(), (String) entry.getValue());
}
requestBuilder.url(httpUrlBuilder.build());
return requestBuilder.build();
}

return null;
}

private static String bodyToString(final RequestBody request){
try {
final RequestBody copy = request;
final Buffer buffer = new Buffer();
if(copy != null)
copy.writeTo(buffer);
else
return "";
return buffer.readUtf8();
}
catch (final IOException e) {
return "did not work";
}
}

public static class Builder {

BasicParamsInterceptor interceptor;

public Builder() {
interceptor = new BasicParamsInterceptor();
}

public Builder addParam(String key, String value) {
interceptor.paramsMap.put(key, value);
return this;
}

public Builder addParamsMap(Map<String, String> paramsMap) {
interceptor.paramsMap.putAll(paramsMap);
return this;
}

public Builder addHeaderParam(String key, String value) {
interceptor.headerParamsMap.put(key, value);
return this;
}

public Builder addHeaderParamsMap(Map<String, String> headerParamsMap) {
interceptor.headerParamsMap.putAll(headerParamsMap);
return this;
}

public Builder addHeaderLine(String headerLine) {
int index = headerLine.indexOf(":");
if (index == -1) {
throw new IllegalArgumentException("Unexpected header: " + headerLine);
}
interceptor.headerLinesList.add(headerLine);
return this;
}

public Builder addHeaderLinesList(List<String> headerLinesList) {
for (String headerLine: headerLinesList) {
int index = headerLine.indexOf(":");
if (index == -1) {
throw new IllegalArgumentException("Unexpected header: " + headerLine);
}
interceptor.headerLinesList.add(headerLine);
}
return this;
}

public Builder addQueryParam(String key, String value) {
interceptor.queryParamsMap.put(key, value);
return this;
}

public Builder addQueryParamsMap(Map<String, String> queryParamsMap) {
interceptor.queryParamsMap.putAll(queryParamsMap);
return this;
}

public BasicParamsInterceptor build() {
return interceptor;
}

}
}
19 changes: 19 additions & 0 deletions app/src/main/java/com/zhouwei/mzbannerview/http/Fault.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.zhouwei.mzbannerview.http;

/**
* 异常处理类,将异常包装成一个 Fault ,抛给上层统一处理
* Created by zhouwei on 16/11/17.
*/

public class Fault extends RuntimeException {
private int errorCode;

public Fault(int errorCode,String message){
super(message);
errorCode = errorCode;
}

public int getErrorCode() {
return errorCode;
}
}
Loading

0 comments on commit c07a2a8

Please sign in to comment.