-1

I'm working on a project in android-studio. But I'm stuck on this point. I've made a request with Okhttp3 and this returns me a Response object. I can retrieve the response as a string (of a json file).

MainActivity.java

client.newCall(request).enqueue(new Callback() {
    @Override
    public void onFailure(Call call, IOException e) {
        e.printStackTrace();
    }

    @Override
    public void onResponse(Call call, Response response) throws IOException {
        if(response.isSuccessful()) {
            String data = response.body().string();
            JsonParser parser = new JsonParser();

            try {
                JsonObject obj = (JsonObject) parser.parse(data);
                String ad = obj.get("features").toString();
                Log.d("msg", data);
                Log.d("msg", ad);
                Log.d("msg", "Works");
            } catch (Exception e) {
                Log.d("msg", "Error");
            }

Response

2019-05-22 10:55:12.785 1301-1631/com.example.triptracker D/msg: {"type":"FeatureCollection","query":[4.35822,51.91962],"features":[{"id":"address.2630076086694170","type":"Feature","place_type":["address"],"relevance":1,"properties":{"accuracy":"point"},"text":"Dotterbloemstraat","place_name":"Dotterbloemstraat 12c, 3135 Vlaardingen, Netherlands","center":[4.358192,51.919666],"geometry":{"type":"Point","coordinates":[4.358192,51.919666]},"address":"12c","context":[{"id":"postcode.7757734261857840","text":"3135"},{"id":"place.6763396954136802","wikidata":"Q210007","text":"Vlaardingen"},{"id":"region.8599455180798270","short_code":"NL-ZH","wikidata":"Q694","text":"Zuid-Holland"},{"id":"country.9349515904622050","short_code":"nl","wikidata":"Q55","text":"Netherlands"}]},{"id":"postcode.7757734261857840","type":"Feature","place_type":["postcode"],"relevance":1,"properties":{},"text":"3135","place_name":"3135, Vlaardingen, Zuid-Holland, Netherlands","bbox":[4.340678,51.909008,4.36899,51.925254],"center":[4.35,51.92],"geometry":{"type":"Point","coordinates":[4.35,51.92]},"context":[{"id":"place.6763396954136802","wikidata":"Q210007","text":"Vlaardingen"},{"id":"region.8599455180798270","short_code":"NL-ZH","wikidata":"Q694","text":"Zuid-Holland"},{"id":"country.9349515904622050","short_code":"nl","wikidata":"Q55","text":"Netherlands"}]},{"id":"place.6763396954136802","type":"Feature","place_type":["place"],"relevance":1,"properties":{"wikidata":"Q210007"},"text":"Vlaardingen","place_name":"Vlaardingen, Zuid-Holland, Netherlands","bbox":[4.270188,51.896102,4.369914,51.951478],"center":[4.35,51.91667],"geometry":{"type":"Point","coordinates":[4.35,51.91667]},"context":[{"id":"region.8599455180798270","short_code":"NL-ZH","wikidata":"Q694","text":"Zuid-Holland"},{"id":"country.9349515904622050","short_code":"nl","wikidata":"Q55","text":"Netherlands"}]},{"id":"region.8599455180798270","type":"Feature","place_type":["region"],"relevance":1,"properties":{"short_code":"NL-ZH","wikidata":"Q694"},"text":"Zuid-Holland","place_name":"Zuid-Holland, Netherlands","bbox":[3.7235244,51.64378,5.150749,52.390802],"center":[4.66667,52],"geometry":{"type":"Point","coordinates":[4.66667,52]},"context":[{"id":"country.9349515904622050","short_code":"nl","wikidata":"Q55","text":"Netherlands"}]},{"id":"country.9349515904622050","type":"Feature","place_type":["country"],"relevance":1,"properties":{"short_code":"nl","wikidata":"Q55"},"text":"Netherlands","place_name":"Netherlands","bbox":[3.1862592,50.750667,7.230902,53.665238],"center":[5.55,52.31667],"geometry":{"type":"Point","coordinates":[5.55,52.31667]}}],"attribution":"NOTICE: © 2019 Mapbox and its suppliers. All rights reserved. Use of this data is subject to the Mapbox Terms of Service (https://www.mapbox.com/about/maps/). This response and the information it contains may not be retained. POI(s) provided by Foursquare."}
2019-05-22 10:55:12.786 1301-1631/com.example.triptracker D/msg: [{"id":"address.2630076086694170","type":"Feature","place_type":["address"],"relevance":1,"properties":{"accuracy":"point"},"text":"Dotterbloemstraat","place_name":"Dotterbloemstraat 12c, 3135 Vlaardingen, Netherlands","center":[4.358192,51.919666],"geometry":{"type":"Point","coordinates":[4.358192,51.919666]},"address":"12c","context":[{"id":"postcode.7757734261857840","text":"3135"},{"id":"place.6763396954136802","wikidata":"Q210007","text":"Vlaardingen"},{"id":"region.8599455180798270","short_code":"NL-ZH","wikidata":"Q694","text":"Zuid-Holland"},{"id":"country.9349515904622050","short_code":"nl","wikidata":"Q55","text":"Netherlands"}]},{"id":"postcode.7757734261857840","type":"Feature","place_type":["postcode"],"relevance":1,"properties":{},"text":"3135","place_name":"3135, Vlaardingen, Zuid-Holland, Netherlands","bbox":[4.340678,51.909008,4.36899,51.925254],"center":[4.35,51.92],"geometry":{"type":"Point","coordinates":[4.35,51.92]},"context":[{"id":"place.6763396954136802","wikidata":"Q210007","text":"Vlaardingen"},{"id":"region.8599455180798270","short_code":"NL-ZH","wikidata":"Q694","text":"Zuid-Holland"},{"id":"country.9349515904622050","short_code":"nl","wikidata":"Q55","text":"Netherlands"}]},{"id":"place.6763396954136802","type":"Feature","place_type":["place"],"relevance":1,"properties":{"wikidata":"Q210007"},"text":"Vlaardingen","place_name":"Vlaardingen, Zuid-Holland, Netherlands","bbox":[4.270188,51.896102,4.369914,51.951478],"center":[4.35,51.91667],"geometry":{"type":"Point","coordinates":[4.35,51.91667]},"context":[{"id":"region.8599455180798270","short_code":"NL-ZH","wikidata":"Q694","text":"Zuid-Holland"},{"id":"country.9349515904622050","short_code":"nl","wikidata":"Q55","text":"Netherlands"}]},{"id":"region.8599455180798270","type":"Feature","place_type":["region"],"relevance":1,"properties":{"short_code":"NL-ZH","wikidata":"Q694"},"text":"Zuid-Holland","place_name":"Zuid-Holland, Netherlands","bbox":[3.7235244,51.64378,5.150749,52.390802],"center":[4.66667,52],"geometry":{"type":"Point","coordinates":[4.66667,52]},"context":[{"id":"country.9349515904622050","short_code":"nl","wikidata":"Q55","text":"Netherlands"}]},{"id":"country.9349515904622050","type":"Feature","place_type":["country"],"relevance":1,"properties":{"short_code":"nl","wikidata":"Q55"},"text":"Netherlands","place_name":"Netherlands","bbox":[3.1862592,50.750667,7.230902,53.665238],"center":[5.55,52.31667],"geometry":{"type":"Point","coordinates":[5.55,52.31667]}}]
2019-05-22 10:55:12.786 1301-1631/com.example.triptracker D/msg: Works

But what I need is the "place_name" which is nested inside "features". I've tried so much, but still don't know how to access "place_name". In JavaScript it's much simpler like features[0].place_name. But in Java it's unknown for me. Can somebody please help.

The result that I want:

"Dotterbloemstraat 12c, 3135 Vlaardingen, Netherlands"
Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
Steven Soekha
  • 721
  • 1
  • 9
  • 20
  • can you show us the models that you have created? – coroutineDispatcher May 22 '19 at 09:24
  • Convert your json response to your modal class. I suggest you please use retofit library instead http https://square.github.io/retrofit/ – Anas Mehar May 22 '19 at 09:25
  • 1) You're using OkHttp without Retrofit. 2) You're randomly guessing code instead of reading how to properly parse a json string. Either implement Retrofit https://square.github.io/retrofit/ or read about json parsing https://www.tutorialspoint.com/android/android_json_parser.htm – Zun May 22 '19 at 09:25
  • try this - **JSONArray array = obj.getJSONArray("features");** **JSONObject main_detail = array.getJSONObject(0);** **String place_name = main_detail.getString("place_name");** – khushbu vadi May 22 '19 at 11:49

1 Answers1

0

you should get the features as Json Array

JsonArray ad = obj.getAsJsonArray("features");
JsonObject ob = ad.get(0).getAsJsonObject();
String placeName = ob.get("place_name").getAsString()
Kou Peto
  • 237
  • 2
  • 7
  • 1
    This gives me the following error: error: method getAsJsonObject in class JsonElement cannot be applied to given types; required: no arguments found: int reason: actual and formal argument lists differ in length – Steven Soekha May 22 '19 at 09:43
  • i edited, try the new version – Kou Peto May 22 '19 at 09:54