Take a look guys, what am I doing wrong?

try { JSONArray jArray = new JSONArray(result); for(int i=0; i < jArray.length(); i++){ JSONObject data = jArray.getJSONObject(i); list.setText(data.getJSONArray("price").getString(i)+" "+data.getJSONArray("description").getString(i)+"\n"); } 

You can take a look at the package here http://m.renault-sokol.ru/api/getmodels/

There is nothing in the response on the screen in the application. Permishn on the Internet is, requests are sent, the package receives 100% of the emulator, but parse in any way.

  • one
    According to the above URL is not given JSON array, and JSON object. - falstaf
  • Then how would you parse him like that? - dll
  • @falstaf is right. The root element of what is given on the link is JSONObject, not JSONArray - Vladyslav Matviienko
  • Is the question still open? - dramf

1 answer 1

Hello!

If I understand correctly, then json is obtained from the api-server.

Recently, for such tasks I use libraries from square ( http://square.imtqy.com/ ) For this - Retrofit ( http://square.imtqy.com/retrofit/ ) and GSON.

Adding a single line of .addConverterFactory(GsonConverterFactory.create()) automatically converts JSON to the desired elements. Simple, fast, convenient)

 Retrofit retrofit = (new Retrofit.Builder()) .baseUrl(URL_SERVER) .addConverterFactory(GsonConverterFactory.create()) .build(); public void initData() { GitHubService service = retrofit.create(GitHubService .class); Call<List<Repo>> call = service.listRepos("user"); call.enqueue(new Callback<List<Repo>>() { @Override public void onResponse(Call<List<Repo>> call, Response<List<Repo>> response) { for (Repo repo: response.body()) { adapter.add(repo); } } @Override public void onFailure(Call<List<Repo>> call, Throwable t) { } }); }