1

I would like to log the value of some tags in a JSON file. Here you are my data source: http://data.nba.net/10s/prod/v1/2016/players.json

I managed to get the entire stream of data using code found here: Get JSON Data from URL Using Android? I post it so it may be easier for you to check my code:

    Button btnHit;
TextView txtJson;
ProgressDialog pd;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnHit = (Button) findViewById(R.id.btnHit);
    txtJson = (TextView) findViewById(R.id.tvJsonItem);

    btnHit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new JsonTask().execute("http://data.nba.net/10s/prod/v1/2016/players.json");
        }
    });


}


private class JsonTask extends AsyncTask<String, String, String> {

    protected void onPreExecute() {
        super.onPreExecute();

        pd = new ProgressDialog(MainActivity.this);
        pd.setMessage("Please wait");
        pd.setCancelable(false);
        pd.show();
    }

    protected String doInBackground(String... params) {


        HttpURLConnection connection = null;
        BufferedReader reader = null;

        try {
            URL url = new URL(params[0]);
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();


            InputStream stream = connection.getInputStream();

            reader = new BufferedReader(new InputStreamReader(stream));

            StringBuffer buffer = new StringBuffer();
            String line = "";

            while ((line = reader.readLine()) != null) {
                buffer.append(line+"\n");
                Log.d("Response: ", "> " + line);   //here u ll get whole response...... :-)

            }

            return buffer.toString();


        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        if (pd.isShowing()){
            pd.dismiss();
        }
        txtJson.setText(result);

    }
}
}

My problem is that i can't get a single tag from the source I chose. For example, how could I get first name and surname of every player in that stream and log it?

Thank you for your time and consideration.

  • 2
    First turn the whole thing into a JSON object. SONObject mainObject = new JSONObject(Your_Sring_data); Then you can obtain the league object: JSONObject league = mainObject.getJsonObject("league"); and from there you can get the array of players, something like leage.getJSONArray("standard"). I haven't tested this, it may have typos as I just typed it out of my head quick. But its a good starting point – Zee Jan 29 '19 at 12:20
  • Could you please send me some working code please? I have no experience using this so it would be better to have working code first. Thank you – Eminent Emperor Penguin Jan 29 '19 at 12:31
  • 2
    Try to use Retrofit or okHttp libraries to reduce complexity. [Retrofit Example](http://www.pratikbutani.com/2016/05/android-tutorial-json-parsing-using-retrofit-part-1/) or Download code from : [Retrofit_Example_Part_1](https://github.com/pratikbutani/Retrofit_Example_Part_1) – Pratik Butani Jan 29 '19 at 12:32

1 Answers1

2

First of all i would advise you to use any http library for android (okHttp, volley..)

But if you still want to use the way you implemented it you need to make some changes here:

while ((line = reader.readLine()) != null) {
            buffer.append(line);
            Log.d("Response: ", "> " + line);   //here u ll get whole response...... :-)

        }

        String json = buffer.toString();
try {
  String json = "";
  JSONObject jsonObject = new JSONObject(json);
  JSONObject league = jsonObject.getJSONObject("league");
  JSONArray standard = league.getJSONArray("standard");
  for (int i = 0;i<standard.length();i++){
    JSONObject item = standard.getJSONObject(i);
    String name = item.getString("firstName");
    String lastName= item.getString("lastName");
  }
} catch (JSONException e) {
  e.printStackTrace();
}
Maksim Novikov
  • 835
  • 6
  • 18