Retrofit 添加返回数据为字符串的转换器

addConverterFactory(new ToStringConverterFactory())

public static class ToStringConverterFactory extends Converter.Factory {        
    static final MediaType MEDIA_TYPE = MediaType.parse("text/plain");       

    @Override        
    public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
         if (String.class.equals(type)) {  
              return new Converter<ResponseBody, String>() {
                    @Override
                    public String convert(ResponseBody value) throws IOException {
                        return value.string();
                    } 
               };
            }
            return null;
        }

        @Override
        public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations,
                                                              Annotation[] methodAnnotations, Retrofit retrofit) {
            if (String.class.equals(type)) {
                return new Converter<String, RequestBody>() {
                    @Override
                    public RequestBody convert(String value) throws IOException {
                        return RequestBody.create(MEDIA_TYPE, value); 
                   }
                };
            }
            return null;
        }
    }

各框架库混淆

用到了一库,混淆的时候编译会出问题,特此写下我用到过的混淆代码

Fragement

#如果引用了v4或者v7包
# Keep the support library
-keep class android.support.v4.** { *; }
-keep interface android.support.v4.** { *; }
# Keep the support library
-keep class android.support.v7.** { *; }
-keep interface android.support.v7.** { *; }

ButterKnife 6.0

-keep class butterknife.** { *; }
-dontwarn butterknife.internal.**
-keep class **$$ViewInjector { *; }
-keepclasseswithmembernames class * {
    @butterknife.* <fields>;
}
-keepclasseswithmembernames class * {
    @butterknife.* <methods>;
}

ButterKnife 7.0

-keep class butterknife.** { *; }
-dontwarn butterknife.internal.**
-keep class **$$ViewBinder { *; }
-keepclasseswithmembernames class * {  
   @butterknife.* <fields>;
}
-keepclasseswithmembernames class * {   
 @butterknife.* <methods>;
}

ActiveAndroid

 -libraryjars libs/activeandroid.jar
 -dontwarn com.activeandroid.**
 -keep class com.activeandroid.** {*;}

EventBus

-keepclassmembers class ** {
    public void onEvent*(**);
}
# Only required if you use AsyncExecutor
-keepclassmembers class * extends de.greenrobot.event.util.ThrowableFailureEvent {
    <init>(java.lang.Throwable);
}

Gson

-keepattributes Signature
-keepattributes *Annotation*
-keep class sun.misc.Unsafe { *; }
-keep class com.google.gson.stream.** { *; }
# Application classes that will be serialized/deserialized over Gson
-keep class com.sunloto.shandong.bean.** { *; }

Retrofit

-dontwarn retrofit.**
-keep class retrofit.** { *; }
-keepattributes Signature
-keepattributes Exceptions

Picasso

-dontwarn com.squareup.okhttp.**

pingyin4j

-dontwarn net.soureceforge.pinyin4j.**
-dontwarn demo.**
-libraryjars libs/pinyin4j-2.5.0.jar
-keep class net.sourceforge.pinyin4j.** { *;}
-keep class demo.** { *;}

华为push

-libraryjars libs/CloudPush_SDK_V2509_hi.jar
-dontwarn com.huawei.**
-keep class com.huawei.** {*;}

友盟统计

-keepclassmembers class * {
    public <init>(org.json.JSONObject);
}
-keep public class [您的应用包名].R$*{
    public static final int *;
}
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

友盟自动更新

-keep public class [您的应用包名].R$*{
    public static final int *;
}
-keep public class com.umeng.fb.ui.ThreadView {
}
如果仍存在问题,可以再做如下操作:
# 添加第三方jar包
-libraryjars libs/umeng-sdk.jar
# 以下类过滤不混淆  
-keep public class * extends com.umeng.**
# 以下包不进行过滤
-keep class com.umeng.** { *; }

友盟用户反馈

-keepclassmembers class * {
    public <init>(org.json.JSONObject);
}
#把[您的应用包名] 替换成您自己的包名,如"com.example.R$*"。
-keep public class 您的应用包名.R$*{
    public static final int *;
}

友盟消息推送

-dontshrink
-keep,allowshrinking class com.umeng.message.* {
    public <fields>;
    public <methods>;
}
-keep,allowshrinking class com.umeng.message.protobuffer.MessageResponse$PushResponse$Info {
    public <fields>;
    public <methods>;
}
-keep,allowshrinking class com.umeng.message.protobuffer.MessageResponse$PushResponse$Info$Builder {
    public <fields>;
    public <methods>;
}
-keep,allowshrinking class org.android.agoo.impl.*{
    public <fields>;
    public <methods>;
}
-keep,allowshrinking class org.android.agoo.service.* {*;}
-keep,allowshrinking class org.android.spdy.**{*;}
-keep public class [应用包名].R$*{
    public static final int *;
}
  • 极光推送
-dontwarn cn.jpush.**
-keep class cn.jpush.** { *; }
  • 其他
# 源文件和行号的信息不混淆
-keepattributes SourceFile,LineNumberTable

随便更改ProgressBar进度条样式

  • So Esay,随便更改进度条样式

`android:indeterminateDrawable` 就是这个属性随意改动样式可以加个动画,图片什么的。

这个文章写的很详细

动态更改Html文本用WebView加载

  • 在HTML中,需要更改的位置用占位符表示(名字随便起)
  • 把HTML转化成字符串,动态替换占位符
  • 用loadDataWithBaseURL加载

public static String htmlConvert(Context ctx) {
        StringBuilder sb = null;
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(ctx.getAssets().open("play/view/information.html")));
            sb = new StringBuilder();
            String line = null;
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }

  • 用WebView加载
webView.loadDataWithBaseURL(null, html, "text/html", "utf-8", null);`

WebView属性及一些问题

loadData()和loadDataWithBaseURL()使用的区别

  • loadData()中的html data中不能包含'#', '%', '\', '?'四中特殊字符,出现这种字符就会出现解析错误,显示找不到网页还有部分html代码。
  • 处理方法:我们需要用UrlEncoder编码为%23, %25, %27, %3f 。 可以使用以下两种代码,data为string类型的html代码

(1)webView.loadData(URLEncoder.encode(data, "utf-8"), "text/html", "utf-8");  
(2)webView.loadDataWithBaseURL(null, data, "text/html", "utf-8", null);
    webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);// 设置是否支持Javascript
        webSettings.setDomStorageEnabled(true);// 开启localStorage
        webSettings.setBlockNetworkImage(true);// 是否显示网络图像
        webSettings.setAllowFileAccess(true);// 启用或禁止WebView访问文件数据
        webSettings.setSupportZoom(true);// 设置可以支持缩放
        // webSettings.setBuiltInZoomControls(true);// 设置出现缩放工具
        webSettings.setDefaultTextEncodingName("UTF-8");// 设置在解码时使用的默认编码
        webSettings.setUseWideViewPort(true);//自适应webview的大小 
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);// 设置布局方式, 自适应屏幕 ,LayoutAlgorithm.NARROW_COLUMNS:适应内容大小LayoutAlgorithm.SINGLE_COLUMN:适应屏幕,内容将自动缩放  
    //webSettings.setJavaScriptCanOpenWindowsAutomatically(true);// 支持通过JS     打开新窗口
        webSettings.setSupportMultipleWindows(true);//多窗口
        webSettings.setPluginState(PluginState.ON);//支持插件
        webSettings.setLoadsImagesAutomatically(true);//支持自动加载图片

Android批量打包

通用版的

Android Studio gradle 打渠道包

使用GSON转List

  • 在使用GSON转List的时候,遇到一个问题,传入泛型转List,GSON是无法解析的,GSON不知道你传的是什么,将会按原样返给你。 eg:
public static <T> List<T> getList(String json, Class<T> clasz) {
      return mGson.fromJson(json, new TypeToken<List<T>>() {}.getType());
}
  • 经鲍永章同学Help me,得以解决
  • stackoverflow
  • 具体方法如下:
 public static <T> List<T> getList(String json, Class<T> clasz) {
    return mGson.fromJson(json, new ListOfSomething<T>(clasz));
 }
static class ListOfSomething<T> implements ParameterizedType {

        private Class<?> wrapped;

        public ListOfSomething(Class<T> wrapped) {
            this.wrapped = wrapped;
        }

        public Type[] getActualTypeArguments() {
            return new Type[] { wrapped };
        }

        public Type getRawType() {
            return List.class;
        }

        public Type getOwnerType() {
            return null;
        }
    }