2017-06-01 1066 views
-3

請幫忙!我正試圖解析來自LoadCallbacks類的loadInBackground()方法上的原始文件的數據。結果必須是一個String []。這是迄今爲止我所知道的,但我知道它已經壞了,但我一直在玩這個遊戲。loadInBackground()方法從原始文件夾解析數據

@覆蓋 公衆的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" 
    ] 
    } 
] 
+0

請格式化您的代碼。 –

回答

0

,你應該使用下面的代碼讀取字節數據,不要使用InputStream。可用()獲取總大小讀取

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();