public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView output = (TextView) findViewById(R.id.textView1);
String strJson=null;
String data = "";
try {
InputStream inputStream = getAssets().open("urls.json");
int size = inputStream.available();
byte [] buffer = new byte[size];
inputStream.read(buffer);
inputStream.close();
strJson = new String(buffer,"UTF-8");
// Create the root JSONObject from the JSON string.
JSONObject jsonRootObject = new JSONObject(strJson);
//Get the instance of JSONArray that contains JSONObjects
JSONArray jsonArray = jsonRootObject.optJSONArray("urls");
//Iterate the jsonArray and print the info of JSONObjects
for(int i=0; i < jsonArray.length(); i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
data +=jsonObject;
}
output.setText(data);
} catch (JSONException | IOException e) {e.printStackTrace();}
}
}
Я получил 0 ошибок. Но в textview ничего не отображается. мой файл json выглядит как
{
"urls":
[
"a",
"b",
"c",
"d",
"e"
]
}