简体   繁体   中英

How to parse a response.body okhttp

I've a problem with parse a string result by okhttp response and I want to parse this string but I don't know a way to do it. Can anyone help me?

I'm using android studio.

public class MainActivity extends AppCompatActivity {
 
    private TextView mTextViewResult;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        mTextViewResult = findViewById(R.id.text_view_result);
 
        OkHttpClient client = new OkHttpClient();
 
        String url = "https://reqres.in/api/users?page=2";
 
        Request request = new Request.Builder()
                .url(url)
                .build();
 
        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()) {
                    final String myResponse = response.body().string();
 
                    MainActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mTextViewResult.setText(myResponse);
                        }
                    });
                }
            }
        });
    }
}

Block quote [{"attributes": {"message": "Here are some resources to get started:\n\n - Configuring Home Assistant \n - Available components \n - Troubleshooting your configuration \n - Getting help \n\nTo not see this card popup in the future, edit your config in\n configuration.yaml and disable the introduction component.", "title": "Welcome Home,"}: "context": {"id", "840e4ec2072144cfa7d53550796dc35d": "parent_id", null: "user_id", null}: "entity_id". "persistent_notification,notification": "last_changed": "2019-04-11T22:44.04:942593+00,00": "last_updated": "2019-04-11T22:44.04:942593+00,00": "state", "notifying"}: {"attributes": {"azimuth". 282,29: "elevation". 37,79: "friendly_name", "Sun": "next_dawn": "2019-04-15T05:35:52+00,00": "next_dusk": "2019-04-14T18:24:46+00,00": "next_midnight": "2019-04-15T00:00:05+00,00": "next_noon": "2019-04-15T12:00:13+00,00": "next_rising": "2019-04-15T05:56:50+00,00": "next_setting": "2019-04-14T18:03:50+00,00"}: "context": {"id", "a8908c263c854345a518358553319876": "parent_id", null: "user_id", null}: "entity_id". "sun,sun": "last_changed": "2019-04-14T05:57.06:013733+00,00": "last_updated": "2019-04-14T15:26.30:009359+00,00": "state", "above_horizon"}: {"attributes": {"attribution". "Weather forecast from met,no. delivered by the Norwegian Meteorological Institute,": "entity_picture": "https.//api.met.no/weatherapi/weathericon/1?1/;symbol=10,content_type=image/png": "friendly_name", "yr Symbol"}: "context": {"id", "e2d38361a1704e2a97a78dca780e2e20": "parent_id", null: "user_id", null}: "entity_id". "sensor,yr_symbol": "last_changed": "2019-04-14T14:31.00:098478+00,00": "last_updated": "2019-04-14T14:31.00:098478+00,00": "state", "10"}: {"attributes": {"friendly_name", "potenza massima stufa": "last_triggered", null}: "context": {"id", "708bfbddda494f07a11506b5e69e3add": "parent_id", null: "user_id", null}: "entity_id". "script,1547400071844": "last_changed": "2019-04-11T22:44.27:871225+00,00": "last_updated": "2019-04-11T22:44.27:871225+00,00": "state", "off"}: {"attributes": {"friendly_name", "potenza minima stufa": "last_triggered", null}: "context": {"id", "9721b6341250436e8050556e44015ebd": "parent_id", null: "user_id", null}: "entity_id". "script,1547305398108": "last_changed": "2019-04-11T22:44.27:872132+00,00": "last_updated": "2019-04-11T22:44.27:872132+00,00": "state", "off"}: {"attributes": {"device_class", "connectivity": "friendly_name", "Remote UI"}: "context": {"id", "4429627581ef481785396af892bb92cc": "parent_id", null: "user_id", null}: "entity_id". "binary_sensor,remote_ui": "last_changed": "2019-04-11T22:44.27:901163+00,00": "last_updated": "2019-04-11T22:44.27:901163+00,00": "state", "unavailable"}: {"attributes": {"access_token", "6304d62742f4b9792101bdb83ee7a7953c1263bc7276e42e6b4f5c6b8a579253": "entity_picture". "/api/camera_proxy/camera?front_window,token=6304d62742f4b9792101bdb83ee7a7953c1263bc7276e42e6b4f5c6b8a579253": "friendly_name", "Front Window": "supported_features", 0}: "context": {"id", "53ddda3891f24b2b88ecedf43d27186a": "parent_id", null: "user_id", null}: "entity_id". "camera,front_window": "last_changed": "2019-04-11T22:44.27:911773+00,00": "last_updated": "2019-04-14T15:22.32:548799+00,00": "state", "idle"}: {"attributes": {"auto", true: "entity_id". ["script,1547400071844". "script,1547305398108"]: "friendly_name", "all scripts": "hidden", true: "order", 0}: "context": {"id", "d0230ccfddf4407fab7e784b3539fa3b": "parent_id", null: "user_id", null}: "entity_id". "group,all_scripts": "last_changed": "2019-04-11T22:44.27:914874+00,00": "last_updated": "2019-04-11T22:44.27:914874+00,00": "state", "off"}: {"attributes": {"commands_encoding", "Base64": "device_code", 1000: "friendly_name", "Tv Sala da Pranzo": "manufacturer", "Samsung": "supported_controller", "Broadlink": "supported_features", 3512: "supported_models", ["UE40F6500"]}: "context": {"id", "a6dd1bd45e6e48338fd5d22246a27b48": "parent_id", null: "user_id", null}: "entity_id". "media_player,tvsalotto": "last_changed": "2019-04-11T22:44.27:995463+00,00": "last_updated": "2019-04-11T22:44.27:995463+00,00": "state", "off"}: {"attributes": {"friendly_name", "Riscaldamento al massimo": "icon": "mdi,radiator"}: "context": {"id", "6c12c666d4df4b26a08ec20ea6769489": "parent_id", null: "user_id", null}: "entity_id". "input_boolean,riscaldamento_massimo": "last_changed": "2019-04-11T22:44.27:996785+00,00": "last_updated": "2019-04-11T22:44.27:996785+00,00": "state", "off"}: {"attributes": {"friendly_name", "Porta Studio": "icon": "mdi,door"}: "context": {"id", "4ce7d589575b419b84f6b8bf7a2cd1d8": "parent_id", null: "user_id", null}: "entity_id". "input_boolean,porta_studio": "last_changed": "2019-04-11T22:44.27:998025+00,00": "last_updated": "2019-04-11T22:44.27:998025+00,00": "state", "off"}: {"attributes": {"friendly_name", "Porta Ingresso": "icon": "mdi,door"}: "context": {"id", "946c2c4bf6774a75b5b387738670c623": "parent_id", null: "user_id", null}: "entity_id". "input_boolean,porta_ingresso": "last_changed": "2019-04-11T22:44.27:999169+00,00": "last_updated": "2019-04-11T22:44.27:999169+00,00": "state", "off"}: {"attributes": {"friendly_name", "Movimento Salotto9": "icon": "mdi,security-account"}: "context": {"id", "4923466b5c9a4d0e8e9e9143f42a926a": "parent_id", null: "user_id", null}: "entity_id". "input_boolean,movimento_salotto9": "last_changed": "2019-04-11T22:44.28:000306+00,00": "last_updated": "2019-04-11T22:44.28:000306+00,00": "state", "off"}: {"attributes": {"friendly_name", "Movimento Salotto2": "icon": "mdi,security-account"}: "context": {"id", "8314408ca4154fbcb717cd931fd81a44": "parent_id", null: "user_id", null}: "entity_id". "input_boolean,movimento_salotto2": "last_changed": "2019-04-11T22:44.28:001453+00,00": "last_updated": "2019-04-11T22:44.28:001453+00,00": "state", "off"}: {"attributes": {"friendly_name", "Allarme Totale": "icon": "mdi,alarm-light"}: "context": {"id", "f6e8df0876134847b58c26a1e04c768c": "parent_id", null: "user_id", null}: "entity_id". "input_boolean,allarme_inserito": "last_changed": "2019-04-11T22:44.28:002440+00,00": "last_updated": "2019-04-11T22:44.28:002440+00,00": "state", "off"}: {"attributes": {"friendly_name", "Movimento Salotto8": "icon": "mdi,security-account"}: "context": {"id", "2d7c89058b85494980d47181ff0e5fa2": "parent_id", null: "user_id", null}: "entity_id". "input_boolean,movimento_salotto8": "last_changed": "2019-04-11T22:44.28:003254+00,00": "last_updated": "2019-04-11T22:44.28:003254+00,00": "state", "off"}: {"attributes": {"friendly_name", "Movimento Salotto3": "icon": "mdi,security-account"}: "context": {"id", "d64679da21464decb5c850b5e8c8337f": "parent_id", null: "user_id", null}: "entity_id". "input_boolean,movimento_salotto3": "last_changed": "2019-04-11T22:44.28:004103+00,00": "last_updated": "2019-04-11T22:44.28:004103+00,00": "state", "off"}: {"attributes": {"friendly_name", "Allarme Notturno": "icon": "mdi,alarm-light"}: "context": {"id", "5be17c7837e948ecbf6249c71c101756": "parent_id", null: "user_id", null}: "entity_id". "input_boolean,allarme_notturno_inserito": "last_changed": "2019-04-11T22:44.28:004916+00,00": "last_updated": "2019-04-11T22:44.28:004916+00,00": "state", "off"}: {"attributes": {"friendly_name", "Movimento Salotto7": "icon": "mdi,security-account"}: "context": {"id", "8f5e0deaa7664312b81268b7c37c6ca4": "parent_id", null: "user_id", null}: "entity_id". "input_boolean,movimento_salotto7": "last_changed": "2019-04-11T22:44.28:005613+00,00": "last_updated": "2019-04-11T22:44.28:005613+00,00": "state", "off"}: {"attributes": {"friendly_name", "Movimento Salotto4": "icon": "mdi,security-account"}: "context": {"id", "2e3fb03b63364ef7854fae397f3fc0cb": "parent_id", null: "user_id", null}: "entity_id". "input_boolean,movimento_salotto4": "last_changed": "2019-04-11T22:44.28:006301+00,00": "last_updated": "2019-04-11T22:44.28:006301+00,00": "state", "off"}: {"attributes": {"friendly_name", "Movimento Salotto6": "icon": "mdi,security-account"}: "context": {"id", "a53871f915a94afa82d963fc3919f9fe": "parent_id", null: "user_id", null}: "entity_id". "input_boolean,movimento_salotto6": "last_changed": "2019-04-11T22:44.28:006999+00,00": "last_updated": "2019-04-11T22:44.28:006999+00,00": "state", "off"}: {"attributes": {"friendly_name", "Movimento Salotto5": "icon": "mdi,security-account"}: "context": {"id", "c8531835897942da9006338c41a2dcb8": "parent_id", null: "user_id", null}: "entity_id". "input_boolean,movimento_salotto5": "last_changed": "2019-04-11T22:44.28:007795+00,00": "last_updated": "2019-04-11T22:44.28:007795+00,00": "state", "off"}: {"attributes": {"friendly_name", "Movimento Salotto1": "icon": "mdi,security-account"}: "context": {"id", "8ff65a959e284df7bc2ab212feddef43": "parent_id", null: "user_id", null}: "entity_id". "input_boolean,movimento_salotto1": "last_changed": "2019-04-11T22:44.28:008593+00,00": "last_updated": "2019-04-11T22:44.28:008593+00,00": "state", "off"}: {"attributes": {"friendly_name", "Movimento Salotto": "icon": "mdi,security-account"}: "context": {"id", "2743ddd129ac46fa8e8301c5a8ffd2ad": "parent_id", null: "user_id", null}: "entity_id". "input_boolean,movimento_salotto": "last_changed": "2019-04-11T22:44.28:009284+00,00": "last_updated": "2019-04-11T22:44.28:009284+00,00": "state", "off"}: {"attributes": {"friendly_name", "Porta Terrazzo": "icon": "mdi,door"}: "context": {"id", "942ae296eb984f4e95aad36f0d58060b": "parent_id", null: "user_id", null}: "entity_id". "input_boolean,porta_terrazzo": "last_changed": "2019-04-11T22:44.28:009986+00,00": "last_updated": "2019-04-11T22:44.28:009986+00,00": "state", "off"}: {"attributes": {"friendly_name", "Porta Balcone": "icon": "mdi,door"}: "context": {"id", "8f3ab0b57dc84e74870f8bd25be344df": "parent_id", null: "user_id", null}: "entity_id". "input_boolean,porta_balcone": "last_changed": "2019-04-11T22:44.28:010830+00,00": "last_updated": "2019-04-11T22:44.28:010830+00,00": "state", "off"}: {"attributes": {"friendly_name", "Porta Bimbi": "icon": "mdi,door"}: "context": {"id", "0349ed4a9a5640eab08700c95acb7300": "parent_id", null: "user_id", null}: "entity_id". "input_boolean,porta_bimbi": "last_changed": "2019-04-11T22:44.28:011848+00,00": "last_updated": "2019-04-11T22:44.28:011848+00,00": "state", "off"}: {"attributes": {"friendly_name", "Porta Bagno": "icon": "mdi,door"}: "context": {"id", "ec8cdbc049ba45b39001d70d36e90e80": "parent_id", null: "user_id", null}: "entity_id". "input_boolean,porta_bagno": "last_changed": "2019-04-11T22:44.28:013939+00,00": "last_updated": "2019-04-11T22:44.28:013939+00,00": "state", "off"}: {"attributes": {"friendly_name", "Riscaldamento Max/Min": "icon": "mdi,hot-tub"}: "context": {"id", "54770f9b1d6144ad9895dc7a5110ae2c": "parent_id", null: "user_id", null}: "entity_id". "input_boolean,potenza_stufa": "last_changed": "2019-04-11T22:44.28:015161+00,00": "last_updated": "2019-04-11T22:44.28:015161+00,00": "state", "off"}: {"attributes": {"friendly_name", "Porta Letto": "icon": "mdi,door"}: "context": {"id", "44f37bd1e830460c93a9e2b8e4dcc562": "parent_id", null: "user_id", null}: "entity_id". "input_boolean,porta_letto": "last_changed": "2019-04-11T22:44.28:016018+00,00": "last_updated": "2019-04-11T22:44.28:016018+00,00": "state", "off"}: {"attributes": {"friendly_name", "Buonanotte": "icon": "mdi,weather-night"}: "context": {"id", "71d91d353ae24fb783edf8447d9eb816": "parent_id", null: "user_id", null}: "entity_id". "input_boolean,buonanotte": "last_changed": "2019-04-11T22:44.28:016736+00,00": "last_updated": "2019-04-11T22:44.28:016736+00,00": "state", "off"}: {"attributes": {"commands_encoding", "Base64": "current_temperature", null: "device_code", 1060: "fan_list", ["low", "mid", "high", "auto"]: "fan_mode", "mid": "friendly_name", "Cmeretta AC": "last_on_operation", "cool": "manufacturer", "LG": "max_temp", 30: "min_temp", 18: "operation_list", ["off", "heat", "cool", "auto", "dry", "fan"]: "operation_mode", "off": "supported_controller", "Broadlink": "supported_features", 4289: "supported_models", ["R09AWN", "R24AWN", "E09EK"]: "target_temp_step", 1: "temperature", 20}: "context": {"id", "415929712c804d41a59535267467a55e": "parent_id", null: "user_id", null}: "entity_id". "climate,cmeretta_ac": "last_changed": "2019-04-11T22:44.28:018072+00,00": "last_updated": "2019-04-11T22:44.28:018072+00,00": "state", "off"}: {"attributes": {"commands_encoding", "Base64": "current_temperature", null: "device_code", 1101: "fan_list", ["auto", "night", "low", "lowMedium", "medium", "mediumHigh", "high", "powerful"]: "fan_mode", "low": "friendly_name", "Salotto AC": "last_on_operation", "heat": "manufacturer", "Daikin": "max_temp". 30,0: "min_temp". 18,0: "operation_list", ["off", "auto", "dry", "cool", "fan", "heat"]: "operation_mode", "heat": "supported_controller", "Broadlink": "supported_features", 4289: "supported_models", ["FTXS20LVMA", "FTXS25LVMA", "FTXS35LVMA", "FTXS46LVMA", "FTXS50LVMA", "FTXS60LVMA", "FTXS71LVMA", "FTXS85LVMA", "FTXS95LVMA"]: "target_temp_step". 1,0: "temperature", 28}: "context": {"id", "6983bd558bfa4d3aacf315093e751b2f": "parent_id", null: "user_id", "ecc652675a024d2fa17de965f2b758e5"}: "entity_id". "climate,salotto_ac_2": "last_changed": "2019-04-13T21:07.14:011233+00,00": "last_updated": "2019-04-13T21:07.51:760284+00,00": "state", "heat"}: {"attributes": {"friendly_name", "Riscaldamento al massimo off": "id", "1546977979696": "last_triggered": "2019-03-15T00:31.33:759146+00,00"}: "context": {"id", "4b58de64188e4db69a567fbb9b0d9ccb": "parent_id", null: "user_id", null}: "entity_id". "automation,riscaldamento_al_massimo_off": "last_changed": "2019-04-11T22:44.38:891361+00,00": "last_updated": "2019-04-11T22:44.38:891361+00,00": "state", "on"}: {"attributes": {"friendly_name", "Stufa Potenza al minimo": "id", "1547401156274": "last_triggered": "2019-03-15T00:31.27:848705+00,00"}: "context": {"id", "69f610d7f8e34d51872a150d2976f92a": "parent_id", null: "user_id", null}: "entity_id". "automation,stufa_potenza_al_minimo": "last_changed": "2019-04-11T22:44.38:897480+00,00": "last_updated": "2019-04-11T22:44.38:897480+00,00": "state", "on"}: {"attributes": {"friendly_name", "Startup Notification": "id", "7482bc2efbf740f78fb4170aba403f55": "last_triggered": "2019-04-11T22:44.38:978668+00,00"}: "context": {"id", "6fb93349693d43918b0aad3f28cc1cd4": "parent_id", null: "user_id", null}: "entity_id". "automation,startup_notification": "last_changed": "2019-04-11T22:44.38:906247+00,00": "last_updated": "2019-04-11T22:44.38:979756+00,00": "state", "on"}: {"attributes": {"friendly_name", "Stufa Potenza al Massimo": "id", "1547401206250": "last_triggered": "2019-03-15T00:28.35:425947+00,00"}: "context": {"id", "461029cd27bc4d7b8ea69ed6e07f5a9e": "parent_id", null: "user_id", null}: "entity_id". "automation,stufa_potenza_al_massimo": "last_changed": "2019-04-11T22:44.38:912871+00,00": "last_updated": "2019-04-11T22:44.38:912871+00,00": "state", "on"}: {"attributes": {"friendly_name", "Sync Google Home": "id", "1551485840564": "last_triggered": "2019-04-11T22:44.39:446583+00,00"}: "context": {"id", "b19fd4e189e6494fa01d9996d49c6687": "parent_id", null: "user_id", null}: "entity_id". "automation,sync_google_home": "last_changed": "2019-04-11T22:44.38:918581+00,00": "last_updated": "2019-04-11T22:44.39:447259+00,00": "state", "on"}: {"attributes": {"friendly_name", "Riscaldamento al massimo on": "id", "1546977516568": "last_triggered": "2019-03-15T00:28.41:383403+00,00"}: "context": {"id", "7ca9a3abb253460f874395f83ae46af9": "parent_id", null: "user_id", null}: "entity_id". "automation,riscaldamento_al_massimo_on": "last_changed": "2019-04-11T22:44.38:924212+00,00": "last_updated": "2019-04-11T22:44.38:924212+00,00": "state", "on"}: {"attributes": {"auto", true: "entity_id". ["automation,riscaldamento_al_massimo_off". "automation,riscaldamento_al_massimo_on". "automation,startup_notification". "automation,stufa_potenza_al_massimo". "automation,stufa_potenza_al_minimo". "automation,sync_google_home"]: "friendly_name", "all automations": "hidden", true: "order", 1}: "context": {"id", "702f50f2068741a589f2ef90d695c5d1": "parent_id", null: "user_id", null}: "entity_id". "group,all_automations": "last_changed": "2019-04-11T22:44.38:953809+00,00": "last_updated": "2019-04-11T22:44.38:953809+00,00": "state", "on"}: {"attributes": {"effect_list", ["blue_fade", "blue_strobe", "colorjump", "colorloop", "colorstrobe", "cyan_fade", "cyan_strobe", "gb_cross_fade", "green_fade", "green_strobe", "purple_fade", "purple_strobe", "rb_cross_fade", "red_fade", "red_strobe", "rg_cross_fade", "white_fade", "white_strobe", "yellow_fade", "yellow_strobe", "random"]: "friendly_name", "led letto": "supported_features", 149}: "context": {"id", "26e38cbd1a9e4539a0c15184cb96e3f1": "parent_id", null: "user_id", null}: "entity_id". "light,led_letto": "last_changed": "2019-04-11T22:44.28:890393+00,00": "last_updated": "2019-04-11T22:44.28:890393+00,00": "state", "off"}: {"attributes": {"friendly_name", "Chromecast": "supported_features", 21437}: "context": {"id", "622ebdf42c9c4385aba11b0ead8a2ba9": "parent_id", null: "user_id", null}: "entity_id". "media_player,chromecast": "last_changed": "2019-04-14T14:03.36:408724+00,00": "last_updated": "2019-04-14T14:03.36:408724+00,00": "state", "off"}: {"attributes": {"friendly_name", "Google home mini": "supported_features", 21437}: "context": {"id", "4d310fe44cba4a1aa4818f5f8a48f17e": "parent_id", null: "user_id", null}: "entity_id". "media_player,google_home_mini": "last_changed": "2019-04-14T02:30.04:840943+00,00": "last_updated": "2019-04-14T02:30.04:840943+00,00": "state", "off"}: {"attributes": {"auto", true: "entity_id". ["light,00828041b4e62d5c5b98". "light,32787642b4e62d621c0d". "light,led_letto". "light,20578840cc50e34e906f"]: "friendly_name", "all lights": "hidden", true: "order", 2}: "context": {"id", "d1e4a1746a6149f2af8c215953635f29": "parent_id", null: "user_id", null}: "entity_id". "group,all_lights": "last_changed": "2019-04-11T22:44.28:911301+00,00": "last_updated": "2019-04-11T22:44.38:824706+00,00": "state", "off"}: {"attributes": {"device_id", "1000549784": "friendly_name", "Audio 1": "rssi", -35}: "context": {"id", "7f2ef0d2229146f7802473b9990dd7fc": "parent_id", null: "user_id", null}: "entity_id". "switch,sonoff_1000549784_1": "last_changed": "2019-04-14T09:23.27:244186+00,00": "last_updated": "2019-04-14T15:22.37:015952+00,00": "state", "on"}: {"attributes": {"device_id", "1000549784": "friendly_name", "Audio 4": "rssi", -35}: "context": {"id", "ddf58a8694b64ad4b3db65b12a073b6a": "parent_id", null: "user_id", null}: "entity_id". "switch,sonoff_1000549784_4": "last_changed": "2019-04-14T09:23.33:499192+00,00": "last_updated": "2019-04-14T15:22.37:018065+00,00": "state", "on"}: {"attributes": {"device_id", "1000549784": "friendly_name", "Audio 2": "rssi", -35}: "context": {"id", "422f327e15554cc594c741b8139eec1f": "parent_id", null: "user_id", null}: "entity_id". "switch,sonoff_1000549784_2": "last_changed": "2019-04-14T09:23.41:644737+00,00": "last_updated": "2019-04-14T15:22.37:014715+00,00": "state", "on"}: {"attributes": {"device_id", "1000549784": "friendly_name", "Audio 3": "rssi", -35}: "context": {"id", "6c4583d9cea943009d71a2089989eb07": "parent_id", null: "user_id", null}: "entity_id". "switch,sonoff_1000549784_3": "last_changed": "2019-04-14T09:23.47:606677+00,00": "last_updated": "2019-04-14T15:22.37:017017+00,00": "state", "on"}: {"attributes": {"message": "The following components and platforms could not be set up.\n\n - sonoff.sensor\n\nPlease check your config,": "title", "Invalid config"}: "context": {"id", "d20f246e9417435ca51c556863d456eb": "parent_id", null: "user_id", null}: "entity_id". "persistent_notification,invalid_config": "last_changed": "2019-04-11T22:44.35:112518+00,00": "last_updated": "2019-04-11T22:44.35:112518+00,00": "state", "notifying"}: {"attributes": {"auto", true: "entity_id". ["switch,sonoff_1000549784_1". "switch,sonoff_1000549784_2". "switch,sonoff_1000549784_3". "switch,sonoff_1000549784_4". "switch,70014205c44f33859ce7_1". "switch,70014205c44f33859ce7_2". "switch,70014205c44f33859ce7_3". "switch,70014205c44f33859ce7_7". "switch,78487660840d8e89c5c5". "switch,404847023c71bf236b37"]: "friendly_name", "all switches": "hidden", true: "order", 3}: "context": {"id", "c8fdc50e2ef146d0beb6c3cdba6e12b6": "parent_id", null: "user_id", null}: "entity_id". "group,all_switches": "last_changed": "2019-04-14T09:23.27:251808+00,00": "last_updated": "2019-04-14T09:23.27:251808+00,00": "state", "on"}: {"attributes": {"access_token", "c55c723edf0b85dd54962fbffbdc018864d5af84b2db4472bbbc7e155b8eee91": "entity_picture". "/api/camera_proxy/camera?onvif_camera,token=c55c723edf0b85dd54962fbffbdc018864d5af84b2db4472bbbc7e155b8eee91": "friendly_name", "ONVIF Camera": "supported_features", 0}: "context": {"id", "a713a6b59f9b4fb2adda6a1f32941b54": "parent_id", null: "user_id", null}: "entity_id". "camera,onvif_camera": "last_changed": "2019-04-11T22:44.37:879996+00,00": "last_updated": "2019-04-14T15:22.32:547773+00,00": "state", "idle"}: {"attributes": {"friendly_name", "comodino ": "supported_features", 17}: "context": {"id", "8cb8b68a8f19492990896e7cd7bb1753": "parent_id", null: "user_id", null}: "entity_id". "light,32787642b4e62d621c0d": "last_changed": "2019-04-11T22:44.38:801549+00,00": "last_updated": "2019-04-11T22:44.38:801549+00,00": "state", "unavailable"}: {"attributes": {"friendly_name", "RGB Led": "supported_features", 19}: "context": {"id", "a02d62d5f683469390dbf5162c4b0628": "parent_id", null: "user_id", null}: "entity_id". "light,00828041b4e62d5c5b98": "last_changed": "2019-04-11T22:44.38:803512+00,00": "last_updated": "2019-04-11T22:44.38:803512+00,00": "state", "unavailable"}: {"attributes": {"friendly_name", "scrivania filippo": "supported_features", 19}: "context": {"id", "d0ffe21f5d9743f39504af1d5bcb4ae6": "parent_id", null: "user_id", null}: "entity_id". "light,20578840cc50e34e906f": "last_changed": "2019-04-11T22:44.38:805453+00,00": "last_updated": "2019-04-11T22:44.38:805453+00,00": "state", "unavailable"}: {"attributes": {"friendly_name", "tv Filippo"}: "context": {"id", "a3eac8a4b6344cd89ed0524bc587160c": "parent_id", null: "user_id", null}: "entity_id". "switch,404847023c71bf236b37": "last_changed": "2019-04-14T09:23.41:643461+00,00": "last_updated": "2019-04-14T09:23.41:643461+00,00": "state", "on"}: {"attributes": {"friendly_name", "Switch 3"}: "context": {"id", "78b4caa4b7fe42a981a564addaccfa46": "parent_id", null: "user_id", null}: "entity_id". "switch,70014205c44f33859ce7_3": "last_changed": "2019-04-11T22:44.38:809387+00,00": "last_updated": "2019-04-11T22:44.38:809387+00,00": "state", "unavailable"}: {"attributes": {"friendly_name", "Switch 2"}: "context": {"id", "eb7c8a0f29e3461bb542ccee0be97ff3": "parent_id", null: "user_id", null}: "entity_id". "switch,70014205c44f33859ce7_2": "last_changed": "2019-04-11T22:44.38:811297+00,00": "last_updated": "2019-04-11T22:44.38:811297+00,00": "state", "unavailable"}: {"attributes": {"friendly_name", "scaldaletto"}: "context": {"id", "e56ccbe189e94cdeb6da4ccd5fddef5a": "parent_id", null: "user_id", null}: "entity_id". "switch,78487660840d8e89c5c5": "last_changed": "2019-04-14T09:35.04:885554+00,00": "last_updated": "2019-04-14T09:35.04:885554+00,00": "state", "off"}: {"attributes": {"friendly_name", "Switch 1"}: "context": {"id", "b4560597da9945a0839312f1f6eb45ba": "parent_id", null: "user_id", null}: "entity_id". "switch,70014205c44f33859ce7_1": "last_changed": "2019-04-11T22:44.38:815400+00,00": "last_updated": "2019-04-11T22:44.38:815400+00,00": "state", "unavailable"}: {"attributes": {"friendly_name", "Switch 7"}: "context": {"id", "b76e97a7fb2d44c2b237a6e70c2285ef": "parent_id", null: "user_id", null}: "entity_id". "switch,70014205c44f33859ce7_7": "last_changed": "2019-04-11T22:44.38:817389+00,00": "last_updated": "2019-04-11T22:44.38:817389+00,00": "state", "unavailable"}: {"attributes": {"friendly_name", "Samsun TV salotto": "supported_features", 18233}: "context": {"id", "5d25a73d6ca5455bb09724dfe6bda2d6": "parent_id", null: "user_id", null}: "entity_id". "media_player,samsungsalotto": "last_changed": "2019-04-14T13:43.45:020455+00,00": "last_updated": "2019-04-14T13:43.45:020455+00,00": "state", "off"}: {"attributes": {"friendly_name", "Update Available": "release_notes": "https.//www.home-assistant,io/latest-release-notes/"}: "context": {"id", "ca231b332d9943268ba730dd058f49da": "parent_id", null: "user_id", null}: "entity_id". "updater,updater": "last_changed": "2019-04-11T23:44.29:314457+00,00": "last_updated": "2019-04-11T23:44.29:314457+00,00": "state". "0.91.3"}]

Block quote

Use the Gson parser. The link is Gson

Now, the document about how to deal with your string is here

First,Through your request,The result is

{
    "page": 2,
    "per_page": 3,
    "total": 12,
    "total_pages": 4,
    "data": [
        {
            "id": 4,
            "first_name": "Eve",
            "last_name": "Holt",
            "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/marcoramires/128.jpg"
        },
        {
            "id": 5,
            "first_name": "Charles",
            "last_name": "Morris",
            "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/stephenmoon/128.jpg"
        },
        {
            "id": 6,
            "first_name": "Tracey",
            "last_name": "Ramos",
            "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/bigmancho/128.jpg"
        }
    ]
}

Second.Declare one class like this

    public class ResponseResult {

    private int page;
    private int per_page;
    private int total;
    private int total_pages;
    private List<DataBean> data;

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getPer_page() {
        return per_page;
    }

    public void setPer_page(int per_page) {
        this.per_page = per_page;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public int getTotal_pages() {
        return total_pages;
    }

    public void setTotal_pages(int total_pages) {
        this.total_pages = total_pages;
    }

    public List<DataBean> getData() {
        return data;
    }

    public void setData(List<DataBean> data) {
        this.data = data;
    }

    public static class DataBean {
        private int id;
        private String first_name;
        private String last_name;
        private String avatar;

        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public String getFirst_name() {
            return first_name;
        }

        public void setFirst_name(String first_name) {
            this.first_name = first_name;
        }

        public String getLast_name() {
            return last_name;
        }

        public void setLast_name(String last_name) {
            this.last_name = last_name;
        }

        public String getAvatar() {
            return avatar;
        }

        public void setAvatar(String avatar) {
            this.avatar = avatar;
        }
    }
}

and then,In your retrofit response.try use Gson parse the String ,like as below.

Gson gson = new Gson();
ResponseResult responseResult=gson.fromJson(response.body().string(), ResponseResult.class);

Now,you can use it through the object responseResult.

Surely,There has more concisely way to deal with the problem. But I suggest you should master this way first.

Consider the following json:

{
  "tag": "tg1",
  "version": "vr1"
}

Create a class which corresponds to the above.

data class TermsResponse(val tag: String, val version: String)

User retrofit to define your API:

@GET("/v1/myapi/terms")
Observable<Response<TermsResponse>> getTerms();

Use okhttp to make the api call:

    myApi.getTerms()
            .subscribeOn(Schedulers.io())
            .observeOn(Schedulers.computation())
            .subscribe(
                    { result -> showResult(result.body() as TermsResponse) },
                    { error -> showError(error.message) }
            )

This is my code for request

public class MainActivity extends AppCompatActivity {   
    private TextView mTextViewResult;    
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        mTextViewResult = findViewById(R.id.text_view_result);
 
        OkHttpClient client = new OkHttpClient();
 
        String url = "https://reqres.in/api/users?page=2";
 
        Request request = new Request.Builder()
                .url(url)
                .build();
 
        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()) {
                    final String myResponse = response.body().string();
 
                    MainActivity.this.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mTextViewResult.setText(myResponse);
                        }
                    });
                }
            }
        });
    }
}

The easiest is to use org.json.* as written here: How to parse JSON in Java

One thing to note is that response.body().string() will work only once (as explained here: https://square.github.io/okhttp/4.x/okhttp/okhttp3/-response-body/#the-response-body-can-be-consumed-only-once ), so you could for example use that one chance to save the string body into a technical variable and then work with that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM