目录
如何生成Google-service.json 文件。
Json和GSON区别
json是一种数据格式,便于数据传输、存储、交换。
gson是一种组件库,可以把java对象数据转换成json数据格式。
Json是什么
GSON是Google提供的用来在Java对象和JSON数据之间进行映射的Java类库。可以将一个Json字符转成一个Java对象,或者将一个Java转化为Json字符串。
特点:
快速、高效 代码量少、简洁 面向对象 数据传递和解析方便 创建方式:Gson gson = new gson();
Gson提供了public String toJson(Objcet obj)方法,可以将对象转化为json字符串。
Json是一种数据格式,便于数据传输、存储、交换;
Gson是一种组件库,可以把java对象数据转换成json数据格式。
如何生成Google-service.json 文件。
打开你的fieabase网址:console.firebase.google.com/u/0/
你应该有自己的项目地址。
创建一个新的项目,或者在你已经有的项目中加入新的app都可以。这个gson文件都是根据你的包名去生成的。在一个项目中可以加很多的App生成的json文件是根据项目去生成的,一个项目有多个app,在你生成的json文件中会有不项目中所有app的配置,当一个项目只有一个App的时候,在json文件中只有一个app的配置。
目录
1.新建assert文件目录。
2.GSon文件解析:
1.新建assert文件目录。
方式很所,在这我仅仅提供最便捷的一种。选中app目录右键, new ,Floder ,Assert Floder。这样就行了。
[] getAssetPicPath(Context context) {
AssetManager am = context.getAssets();
String[] path = null;
try {
path = am.list(""); // ""获取所有,填入目录获取该目录下所有资源
} catch (IOException e) {
e.printStackTrace();
}
File [] pciPaths =new File[11];
for (int i = 0; i < path.length; i++) {
if ((path[i].endsWith(".txt") || path[i].endsWith(".jpg")) && path[i].startsWith("sy")) { // 根据图片特征找出图片
File file = new File(path[i]);
pciPaths[i]=file;
}
}
return pciPaths;
2.GSon文件解析:
首先依赖包,在app 的gradle中加入:
compile 'com.squareup.retrofit2:converter-gson:2.2.0'
String defaultCode = getJson("call2_ad.json", this);
Gson gson = new Gson();
//把JSON格式的字符串转为List
List<CodeBean> list = gson.fromJson(defaultCode, new TypeToken<List<CodeBean>>() {
}.getType());
public static String getJson(String{
//将json数据变成字符串
StringBuilder stringBuilder = new StringBuilder();
try {
//获取assets资源管理器
AssetManager assetManager = context.getAssets();
//通过管理器打开文件并读取
BufferedReader bf = new BufferedReader(new InputStreamReader(assetManager.open(fileName), "utf-8"));
String line;
while ((line = bf.readLine()) != null) {
stringBuilder.append(line);
}
} catch (IOException e) {
e.printStackTrace();
}
return stringBuilder.toString();
}
只要是将assert目录中的文件用assetmanager进行文件解析转换成List就行了。
public class CodeBean {
private String country_code;
private List<AdCountBean> ad_count;
private int start_time;
private int end_time;
public String getCountry_code() {
return country_code;
}
public void setCountry_code(String) {
this.country_code = country_code;
}
public List<AdCountBean> getAd_count() {
return ad_count;
}
public void setAd_count(List<AdCountBean> ad_count) {
this.ad_count = ad_count;
}
public int getStart_time() {
return start_time;
}
public void setStart_time(int start_time) {
this.start_time = start_time;
}
public int getEnd_time() {
return end_time;
}
public void setEnd_time(int end_time) {
this.end_time
public class AdCountBean {
/**
* home : 20
* exit : 20
* apply_theme : 20
* call_deatail : 20
* cloud : 3
* theme : 0
* dialog_time : 30
* dialog_need_ad : true
*/
private int home;
@SerializedName("exit")
private int exit_;
private int apply_theme;
@SerializedName("call_deatail")
private int call_deatail_;
@SerializedName("cloud")
private int cloud_;
@SerializedName("theme")
private int theme_;
@SerializedName("dialog_time")
private int dialog_time_;
@SerializedName("dialog_need_ad")
private boolean dialog_need_ad;
public int getHome() {
return home;
}
public void setHome(int home) {
this.home = home;
}
public int getExit_() {
return exit_;
}
public void setExit_(int exit_) {
this.exit_ = exit_;
}
public int getCloud_() {
return cloud_;
}
public void setCloud_(int cloud_) {
this.cloud_ = cloud_;
}
public int getDialog_time_() {
return dialog_time_;
}
public void setDialog_time_(int dialog_time_) {
this.dialog_time_ = dialog_time_;
}
public int getApply_theme() {
return apply_theme;
}
public void setApply_theme(int apply_theme) {
this.apply_theme = apply_theme;
}
public int getCall_deatail_() {
return call_deatail_;
}
public void setCall_deatail_(int call_deatail_) {
this.call_deatail_ = call_deatail_;
}
public int getTheme_() {
return theme_;
}
public void setTheme_(int theme_) {
this.theme_ = theme_;
}
public boolean isDialog_need_ad() {
return dialog_need_ad;
}
public void setDialog_need_ad(boolean) {
this.dialog_need_ad = dialog_need_ad;
}
@Override
public String toString() {
return "AdCountBean{" +
"home=" + home +
", exit=" + exit_ +
", apply_theme=" + apply_theme +
", call_deatail=" + call_deatail_ +
", cloud=" + cloud_ +
", theme=" + theme_ +
", dialog_time=" + dialog_time_ +
", dialog_need_ad=" + dialog_need_ad +
'}';
}
}
你自己打印log看看就知道了,这是我目前的理解。