loadInBackground()方法从原始文件夹中解析数据

时间:2017-06-01 01:02:48

标签: android android-loadermanager

请帮忙!我试图解析LoadCallbacks类的loadInBackground()方法中的原始文件中的数据。结果必须是String []。这是我到目前为止所知道的,我知道它已经坏了,但我一直在玩它很多。

@覆盖             public String [] loadInBackground(){

            String json;
            try {
                InputStream is = getResources().openRawResource(R.raw.countries);
                int size = is.available();
                byte[] buffer = new byte[size];
                is.read(buffer);
                is.close();
                json = new String(buffer, "UTF-8");

                JSONArray rootArray = new JSONArray(json);

                countryName = new String[rootArray.length()];

                String[] newData = new String[rootArray.length()];
                for (int i = 0; i < rootArray.length(); i++) {
                    JSONObject c = rootArray.getJSONObject(i);
                    String name = c.getString("name");
                    String nativeName = c.getString("nativeName");


                    String[] newData = {name, nativeName};

                    return newData;
                }


            } catch (Exception e) {
                e.printStackTrace();
                return null;
        }

并且要解析的数据是这样的:

[
  {
    "name": "Afghanistan",
    "topLevelDomain": [
      ".af"
    ],
    "alpha2Code": "AF",
    "alpha3Code": "AFG",
    "callingCodes": [
      "93"
    ],
    "capital": "Kabul",
    "altSpellings": [
      "AF",
      "Afġānistān"
    ],
    "relevance": "0",
    "region": "Asia",
    "subregion": "Southern Asia",
    "translations": {
      "de": "Afghanistan",
      "es": "Afganistán",
      "fr": "Afghanistan",
      "ja": "アフガニスタン",
      "it": "Afghanistan"
    },
    "population": 26023100,
    "latlng": [
      33.0,
      65.0
    ],
    "demonym": "Afghan",
    "area": 652230.0,
    "gini": 27.8,
    "timezones": [
      "UTC+04:30"
    ],
    "borders": [
      "IRN",
      "PAK",
      "TKM",
      "UZB",
      "TJK",
      "CHN"
    ],
    "nativeName": "افغانستان",
    "numericCode": "004",
    "currencies": [
      "AFN"
    ],
    "languages": [
      "ps",
      "uz",
      "tk"
    ]
  },
  {
    "name": "Åland Islands",
    "topLevelDomain": [
      ".ax"
    ],
    "alpha2Code": "AX",
    "alpha3Code": "ALA",
    "callingCodes": [
      "358"
    ],
    "capital": "Mariehamn",
    "altSpellings": [
      "AX",
      "Aaland",
      "Aland",
      "Ahvenanmaa"
    ],
    "relevance": "0",
    "region": "Europe",
    "subregion": "Northern Europe",
    "translations": {
      "de": "Åland",
      "es": "Alandia",
      "fr": "Åland",
      "ja": "オーランド諸島",
      "it": "Isole Aland"
    },
    "population": 28875,
    "latlng": [
      60.116667,
      19.9
    ],
    "demonym": "Ålandish",
    "area": 1580.0,
    "gini": null,
    "timezones": [
      "UTC+02:00"
    ],
    "borders": [],
    "nativeName": "Åland",
    "numericCode": "248",
    "currencies": [
      "EUR"
    ],
    "languages": [
      "sv"
    ]
  }
]

1 个答案:

答案 0 :(得分:0)

你应该使用下面的代码来读取字节数据,不要使用InputStream。 available()获取要读取的总大小

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    byte[] buffer = new byte[4096];
    while (true) {
        int read = in.read(buffer);
        if (read == -1) {
            break;
        }
        out.write(buffer, 0, read);
    }
    try{
      out.close();
    }catch(IOException e){// no op

    }
    byte[] data=out.toByteArray();