2020-03-18 android studio读取main/

2020-03-18  本文已影响0人  xiaohuage

目标:读取main/assets文件夹下ksdk_config.json文件内容

ksdk_config.json文件内容:

{

//  "appid": "cnjjws",

  "appid": "twxxwn",

//    "url": "http://10.0.10.67:8000", //url 国内 url_cn  海外url_tw  自定义 写具体地址

  "url": "url_cn",

  "region": "cn",  //region 国内 cn 海外 tw  其他配置错误

  "screenOrientation": "landscape",

  "debug": true,

  "platforms": {

"login": [

{

"name": "linelogin",

        "enable": false,

        "platformId": ""

      },

      {

"name": "facebooklogin",

        "enable": false,

        "platformId": ""

      },

      {

"name": "googlelogin",

        "enable": false,

        "platformId": ""

      }

],

    "pay": [

{

"name": "alipay",

        "enable": true,

        "platformId": ""  //支付宝支付中 platformId 为空字符串

      },

      {

"name": "wechatpay",

        "enable": true,

        "platformId": ""  //微信支付中 platformId 微信微信注册的app_id

      },

      {

"name": "unionpay",

        "enable": true,

        "platformId": ""

      },

      {

"name": "googlepay",

        "enable": true,

        "platformId": "",

//        "publicKey": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlHBmNAHdW498Nk4so4Bg3PoTWTlIjntzb6MHkP5YEOWHiF3l+9SssFlQ+6lMFw2hWif+P6ngEO0EL8+xX4YkcFOOVs4lOitTzOF40rom9ZElfMWH0Ia03faYIZttl6ZDCa1Gzsj5u0dPFNXkfIcrqmniw8uTnKgHCEbUZqdxkxK0mdSdutYwDZQgXSLJTdWxYzUTxG9cTzoHYOelN3lg9Ddr8SpZHwqtdJNyl+bW7BDn7mIhY9KSahPA86HTDdo5frM6Vg+A31CISDrGAaWL7KYnM5Q728RwTbFZ2ZoFAPcVhfYePcpuD6H33q/OL2vO2rarIq0c7Au3VZeY8vWIAQIDAQAB/wPK6VCJ6Rhs9NEKOSFzma+a7127wHYLnbesky7SZth1O387Jh/HBt/zGOfQssmMg1l/JuWGu+q2IzmqllREeJwRbk6W+2j2FX6T+95b1ERTFoIWT7QiSmZb4TsbkEPS3Gdns4TrmiA1xQJOBjrSoQ73Cp5vssbbhGMMHG9qZdhN3NHahfZp51Y3GTHyj+z//VP1Ujm3Qdo9Yz2IadNUw8A7UA4jt/5Im9HnX+w5V8dAHMpq7cClxTZYJFbs6wxes7RYYjj3gzowIDAQAB",

        "publicKey":"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxDw885SigQealjCCgub7Bwn4TFE60a9nxTO6oWuDtcMdD/DHUJnAksygHOEXq+U13HRfRP7h7+HkKrAZ1e1HEX/wrFZfyyU2mjz/15Z3mf3OyGN9w7cE192jXnn/jUiR9jd7kk3ugawuHEw6fHpWamIOPk8kI0qZLgqeEGPF1Fx2HootjcXRQw3DZu3xQhI9AHxj5LV6Zx6jhx/nIXE2qKVXbWS1r2EXnGoRjJTihPbWfvSfDiHUIOBVylcQclm1mRmYOqbeH9uCXPqgUuJ9eC2sgkotwRgrJkF3ucGwJC3DT/miaWTeMf3ag8JW1py/GwGgc21sIAX25xxb/MhbcwIDAQAB",

        "products": [

"qyjytier301",

          "qyjytier302",

          "qyjytier101",

          "qyjytier102",

          "qyjytier103",

          "qyjytier104",

          "qyjytier105",

          "qyjytier106",

          "qyjytier107",

          "qyjytier201",

          "qyjytier202",

          "qyjytier203",

          "qyjytier204",

          "qyjytier205",

          "qyjytier206",

          "qyjytier207",

          "qyjytier208",

          "qyjytier209",

          "qyjytier210",

          "qyjytier211",

          "qyjytier212",

          "qyjytier213",

          "qyjytier214",

          "qyjytier215",

          "qyjytier216",

          "qyjytier217",

          "qyjytier218",

          "qyjytier219",

          "qyjytier220",

          "qyjytier221",

          "qyjytier222",

          "qyjytier223",

          "qyjytier224",

          "qyjytier225",

          "qyjytier226",

          "qyjytier227",

          "qyjytier228"

        ]

},

      {

"name": "mycardpay",

        "enable": true,

        "platformId": "",

        "resPay": "name_alipay",

        "payCountry": "TW",

        "productType": "card"

      },

      {

"name": "bluepay",

        "enable": true,

        "platformId": ""

      }

]

}

}

思路:首先需要用java接口读取到ksdk_config.json文件,刚好有一个AssetManager对象的open()方法可以实现这一目标,读取到文件后接下来就需要读取文件中的内容,那么就需要用到IO读取文件内容了,好了,代码如下:

public class MainActivityextends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

AssetManager assetManager = getAssets(); //初始化AssetManager获得该对象

        String fileName ="ksdk_config.json";

        try {

InputStream inputStream = assetManager.open(fileName);//读取到.json文件了,生成了一个流对象InputStream 

//接下来是流的读入操作,读入到一个StringBuilder对象中存储

            InputStreamReader inputStreamReader =new InputStreamReader(inputStream);

            BufferedReader bufferedReader =new BufferedReader(inputStreamReader);

            StringBuilder stringBuilder =new StringBuilder();

            String line;

            while (null != (line = bufferedReader.readLine())) {

stringBuilder.append(line);

            }

//打印下StringBuilder对象中存储的内容,看是不是和ksdk_config.json文件中的内容是一样的

            Log.e(TAG, stringBuilder.toString());

        }catch (IOException e) {

e.printStackTrace();

        }

}

}

打印结果如下:

看不到后面的内容是不,我给你复制,粘贴出来这一句打印内容:

03-18 17:13:45.053 3713-3713/com.kaiqigu.ksdkdemo E/MainActivity: { // "appid": "cnjjws", "appid": "twxxwn",// "url": "http://10.0.10.67:8000", //url 国内 url_cn 海外url_tw 自定义 写具体地址 "url": "url_cn", "region": "cn", //region 国内 cn 海外 tw 其他配置错误 "screenOrientation": "landscape", "debug": true, "platforms": { "login": [ { "name": "linelogin", "enable": false, "platformId": "" }, { "name": "facebooklogin", "enable": false, "platformId": "" }, { "name": "googlelogin", "enable": false, "platformId": "" } ], "pay": [ { "name": "alipay", "enable": true, "platformId": "" //支付宝支付中 platformId 为空字符串 }, { "name": "wechatpay", "enable": true, "platformId": "" //微信支付中 platformId 微信微信注册的app_id }, { "name": "unionpay", "enable": true, "platformId": "" }, { "name": "googlepay", "enable": true, "platformId": "",// "publicKey": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlHBmNAHdW498Nk4so4Bg3PoTWTlIjntzb6MHkP5YEOWHiF3l+9SssFlQ+6lMFw2hWif+P6ngEO0EL8+xX4YkcFOOVs4lOitTzOF40rom9ZElfMWH0Ia03faYIZttl6ZDCa1Gzsj5u0dPFNXkfIcrqmniw8uTnKgHCEbUZqdxkxK0mdSdutYwDZQgXSLJTdWxYzUTxG9cTzoHYOelN3lg9Ddr8SpZHwqtdJNyl+bW7BDn7mIhY9KSahPA86HTDdo5frM6Vg+A31CISDrGAaWL7KYnM5Q728RwTbFZ2ZoFAPcVhfYePcpuD6H33q/OL2vO2rarIq0c7Au3VZeY8vWIAQIDAQAB/wPK6VCJ6Rhs9NEKOSFzma+a7127wHYLnbesky7SZth1O387Jh/HBt/zGOfQssmMg1l/JuWGu+q2IzmqllREeJwRbk6W+2j2FX6T+95b1ERTFoIWT7QiSmZb4TsbkEPS3Gdns4TrmiA1xQJOBjrSoQ73Cp5vssbbhGMMHG9qZdhN3NHahfZp51Y3GTHyj+z//VP1Ujm3Qdo9Yz2IadNUw8A7UA4jt/5Im9HnX+w5V8dAHMpq7cClxTZYJFbs6wxes7RYYjj3gzowIDAQAB", "publicKey":"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxDw885SigQealjCCgub7Bwn4TFE60a9nxTO6oWuDtcMdD/DHUJnAksygHOEXq+U13HRfRP7h7+HkKrAZ1e1HEX/wrFZfyyU2mjz/15Z3mf3OyGN9w7cE192jXnn/jUiR9jd7kk3ugawuHEw6fHpWamIOPk8kI0qZLgqeEGPF1Fx2HootjcXRQw3DZu3xQhI9AHxj5LV6Zx6jhx/nIXE2qKVXbWS1r2EXnGoRjJTihPbWfvSfDiHUIOBVylcQclm1mRmYOqbeH9uCXPqgUuJ9eC2sgkotwRgrJkF3ucGwJC3DT/miaWTeMf3ag8JW1py/GwGgc21sIAX25xxb/MhbcwIDAQAB", "products": [ "qyjytier301", "qyjytier302", "qyjytier101", "qyjytier102", "qyjytier103", "qyjytier104", "qyjytier105", "qyjytier106", "qyjytier107", "qyjytier201", "qyjytier202", "qyjytier203", "qyjytier204", "qyjytier205", "qyjytier206", "qyjytier207", "qyjytier208", "qyjytier209", "qyjytier210", "qyjytier211", "qyjytier212", "qyjytier213", "qyjytier214", "qyjytier215", "qyjytier216", "qyjytier217", "qyjytier218", "qyjytier219", "qyjytier220", "qyjytier221", "qyjytier222", "qyjytier223", "qyjytier224", "qyjytier225", "qyjytier226", "qyjytier227", "qyjytier228" ] }, { "name": "mycardpay", "enable": true, "platformId": "", "resPay": "name_alipay", "payCountry": "TW", "productType": "card" }, { "name": "bluepay", "enable": true, "platformId": "" } ] }}

对比一下ksdk_config.json文件中的内容,发现完全一致,OK,用java语言实现读取assets文件夹下的.json文件内容的操作就完成了。

上一篇下一篇

猜你喜欢

热点阅读