-1

hi sir i have seen the whole stack overflow but did not found aswer please help me i am getting json data from api and but i am not able to get few items from this i am getting this error many times. if there is any question or solution that i missed then please help me to solve please..

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 = "";
            Log.e(TAG, "doInBackground: BUFFER" + reader.read());
            while ((line = reader.readLine()) != null) {
                buffer.append(line + "\n");
                Log.e(TAG, "> " + line);   //here u ll get whole response...... :-)
                Log.e(TAG, "doInBackground: length " + line);
            }

            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 result1) {
        super.onPostExecute(result1);
        String result = result1.toString();
        if (pd.isShowing()) {
            pd.dismiss();
        }
        Log.e(TAG, "onPostExecute: " + result);

        try {

            JSONArray jArray = new JSONArray(result);
            for (int i = 0; i < jArray.length(); i++) {

                JSONObject jObject = jArray.getJSONObject(i);
                String name = jObject.getString("name");
                Log.e(TAG, "onPostExecute: ");

            } // End Loop
            Log.e(TAG, "onPostExecute: jsion " + jArray.length());
        } catch (JSONException e) {
            Log.e(TAG, "onPostExecute Error: " + e.toString());
        }
    }
}

here is the calling function of that with api

        new JsonTask().execute("https://api.iextrading.com/1.0/ref-data/symbols");
Zahid Iqbal
  • 394
  • 6
  • 12
  • 1
    i want to get only name and symbol for each item please help me any body.. – Zahid Iqbal Nov 09 '20 at 18:18
  • 1
    Does this answer your question? [How to parse JSON in Java](https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – Savior Nov 09 '20 at 18:26
  • 1
    sir problem is that api gives only json data not with some response like as shown "post" etc otherwise i will solve this – Zahid Iqbal Nov 09 '20 at 18:35
  • 1
    "posts": [ { "post_id": "123456789012_123456789012", "actor_id": "1234567890", "picOfPersonWhoPosted": "http://example.com/photo.jpg", "nameOfPersonWhoPosted": "Jane Doe", "message": "Sounds cool. Can't wait to see it!", "likesCount": "2", "comments": [], "timeOfPost": "1234567890" } ] – Zahid Iqbal Nov 09 '20 at 18:37
  • api dont return like that – Zahid Iqbal Nov 09 '20 at 18:37

1 Answers1

2

https://api.iextrading.com/1.0/ref-data/symbols returns a flat JSONArray with 9290 JSONObjects.

There's nothing wrong with the code you posted, it runs fine on my end - the only thing I would change is remove the need for a StringBuffer, and you don't need to append "\n".. JSON reads as a single line.

 String line = "";
 String returned = "";
 while ((line = reader.readLine()) != null) {
      returned = line;
 }
 return returned;

You are saying you are missing some data - that is because the returned data has missing items. You can see this by putting the full JSONArray return string into a notepad++ or something else that can easily format the array for you, you will see that some "name" are missing for some of the JSONObjects..

for example - object number 9241

symbol : ZIONP
name :
date : 2020-11-09
isEnabled : true
type : N/A
iexId : 7674

You are trying to get the name, you won't get the name - the data isn't there to get.

buradd
  • 1,271
  • 1
  • 13
  • 19
  • sir issue comes when i want to read get single item in post execute – Zahid Iqbal Nov 10 '20 at 15:19
  • please help me in post execute to read other data as symbol only from the jsonArray of objects. i upvoted you because you understands me and give me time and i am thankful to you for this – Zahid Iqbal Nov 10 '20 at 15:37
  • change String name = jObject.getString("name"); to String name = jObject.optString("name"); - then you can check if the name is blank, if so, then you can jObject.getString("symbol"); – buradd Nov 10 '20 at 16:48
  • sir sorry disturbing you but getting this error in catch pase of post execute >>>>>>>>onPostExecute Error: org.json.JSONException: Value {"symbol":"A","name":"AGILENT TECHNOLOGIES INC","date":"2020-11-10","isEnabled":true,"type":"N\/A","iexId":"2"} of type org.json.JSONObject cannot be converted to JSONArray – Zahid Iqbal Nov 10 '20 at 17:22
  • did you update your code to remove the StringBuffer and not add "\n" to the line? – buradd Nov 10 '20 at 18:10
  • yes sir i added but same,actually sir error comes because of this dont know why >> JSONArray jArray = new JSONArray(result); if i comment this then error gone – Zahid Iqbal Nov 11 '20 at 16:03