-3

Please help! I am trying to parse data from a raw file on loadInBackground() method of LoadCallbacks class. The result must be a String[]. Here is what I have so far and I know it's broken, but I've been playing around with it a lot.

@Override 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;
        }

and the data to parse is something like this:

[
  {
    "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"
    ]
  }
]
Pri
  • 1
  • 3

1 Answers1

0

you should use below code to read the byte data,do not use the InputStream. available() to get the total size to read

    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();
huangsu
  • 66
  • 3