简体   繁体   中英

Getting JSON from URL, pick value and store it into the textView

I am starting to learn Android, and I've come to the issue which I cannot resolve: I have URL with JSON object: http://jsonplaceholder.typicode.com/todos I am trying to connect URL in java-androidstudio, then to pick exact value, let's say I want title value from id=1 and place it into my textView (textview id is 'com1')

I've come to this code, which is supposed to put at least id value to the textview....but it is not really doing anything

            String sURL = "http://jsonplaceholder.typicode.com/todos";
            URL url = new URL(sURL);
            URLConnection request = url.openConnection();
            request.connect();
JsonParser jp = new JsonParser(); //from gson
            JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent()));
            JsonObject rootobj = root.getAsJsonObject();
            String idcko = rootobj.get("id").getAsString();

            TextView textElement = (TextView) findViewById(R.id.com1);
            textElement.setText(idcko);

A good place to start is reading about volley , it's a lib that help you manage the requests.

Here is a tutorial on how to use volley Android Volley Tutorial

and a good example can be found here

There are a couple reasons why your code is not behaving as you expect.

First: Make sure you have configured your Android Manifest properly by enabling the INTERNET permission and allowing cleartext traffic.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.sandbox"
            android:targetSandboxVersion="1">
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        andoird:useCleartextTraffix="true"
        ... />

Second: Make sure you are performing your request in an AsyncTask . Android does not allow HTTP requests to run on the main thread. To overcome this, create a new task that extends the AsyncTask abstract class.

class UrlRequestTask extends AsyncTask<Void, Void, Void> {
    protected void doInBackground() {
        String sURL = "http://jsonplaceholder.typicode.com/todos";
            URL url = new URL(sURL);
            URLConnection request = url.openConnection();
            request.connect();
            JsonParser jp = new JsonParser(); //from gson
            JsonElement root = jp.parse(new InputStreamReader((InputStream) 
            request.getContent()));
            JsonObject rootobj = root.getAsJsonObject();
            String idcko = rootobj.get("id").getAsString();

            TextView textElement = (TextView) findViewById(R.id.com1);
            textElement.setText(idcko);
    }
}

You can then invoke your task in any activity's onCreate like so: new UrlRequestTask().execute();

Try these things and see what happens. Post error messages to help myself and others determine exactly what's wrong. I ran into problems when I did the for the first time as well, and these solutions are what helped me.

Cheers!

Edit: formatting

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