1

I'm trying to take the JSON from BITTREX and parse it and present it to the screen in Android Studio. This works for me with test JSON I made myself plus other requests i have made using the same API. However, when i go to use the actual request I need i get the following error :

JSONException: Value null of type org.json.JSONObject$1 cannot be converted to JSONArray

This is the request used: https://bittrex.com/api/v1.1/public/getmarketsummaries

API Documentation

Here is the Code :

public class fetchData extends AsyncTask<Void,Void,Void> {

String data=""; //all json lines after loop

String dataParsed ="";
String singleParsed =""; //parsed attributes
@Override
protected Void doInBackground(Void... voids) {
    //Background Thread i.e API request
    try {
        URL url = new URL("https://bittrex.com/api/v1.1/public/getmarketsummaries\n");
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        InputStream inputStream = httpURLConnection.getInputStream(); //read data in from the connection
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); //Buff reader to read the inputstream (otherwise we get ints)

         String line ="";
        //Loop that reads all lines and represents them to as a string
         while(line != null) {
             line = bufferedReader.readLine(); //read line of json and assign to "line" if not null
             data = data + line;
         }
             JSONArray myJsonArray = new JSONArray(data);  //store json in a json array
             for (int i = 0; i < myJsonArray.length(); i++) {
                    //Itterate through the array and get each object i.e btc,ltc
                     JSONObject myJsonObject = (JSONObject) myJsonArray.get(i);
                     //Single JSON object parsed
                     singleParsed = "Coin" + myJsonObject.get("MarketName") + "\n" +
                             "high" + myJsonObject.get("High") + "\n" +
                             "low" + myJsonObject.get("Low") + "\n" +
                             "volume" + myJsonObject.get("Volume") + "\n" +
                             "last" + myJsonObject.get("Last") + "\n" +
                             "basevolume" + myJsonObject.get("BaseVolume") + "\n" +
                             "time" + myJsonObject.get("Timestamp") + "\n" +
                             "bid" + myJsonObject.get("Bid") + "\n" +
                             "ask" + myJsonObject.get("Ask") + "\n" +
                             "openbuyorders" + myJsonObject.get("OpenBuyOrders") + "\n" +
                             "opensellorders" + myJsonObject.get("OpenSellOrders") + "\n" +
                             "prevday" + myJsonObject.get("PrevDay") + "\n" +
                             "created" + myJsonObject.get("Created") + "\n";

                     dataParsed = dataParsed + singleParsed + "\n";
             }

    }catch(MalformedURLException e ){
    e.printStackTrace();
     } catch (IOException e) {
    e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return null;
}
@Override
protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);
    //UI thread
    MainActivity.data.setText(this.dataParsed);
  }
}

Any thoughts would be greatly appreciated. Thanks :)

**UPDATE - SOLVED **

I added the following line before the loop and it solved the issue.

    //target the "result" Array of objects(BTC,LTC,ETH) and map them to a JsonArray for parsing
            JSONArray myJsonArray = myJsonObj.getJSONArray("result");

2 Answers 2

1

The exception is perfectly valid. Your trying to convert json object into json array. Try below code

remove "\n" character at the end.

URL url = new URL("https://bittrex.com/api/v1.1/public/getmarketsummaries\n")

add below logs

while(line != null) {
             line = bufferedReader.readLine(); //read line of json and assign to "line" if not null
             data = data + line;
         }
Log.debug("api_response","api-response->"+data);

and try below code

if(data!= null){ // add this if condition too.

JSONObject jsonObj = new JSONObject(data);
JSONArray myJsonArray = jsonObj.getJSONArray("result"); ;  //store json in a json array
             for (int i = 0; i < myJsonArray.length(); i++) {
                    //Itterate through the array and get each object i.e btc,ltc
                     JSONObject myJsonObject = (JSONObject) myJsonArray.get(i);
Sign up to request clarification or add additional context in comments.

9 Comments

Please check the updated ans. i suspect api is returning " null " value. Better try some logging the response.
@DavidDooley if your issue got resolved can you accept and upvote ans please ?
Did that and still nothing :( yes i will upvote "If" i get it working
what does log data printing in logs as api response?
i cant find where to see that - i used postman to check and the API request is valid
|
1

The json data returned by the API is in the following format:

{
  "success": true,
  "message": "",
  "result": [
    {
    },
    {
    }
  ]
}

So you need to get the whole data as JSONObject first, then from it you can extract the JSONArray with the "result" key.

The code is something like this:

// get the JSONObject from the data
JSONObject jsonObject = new JSONObject(data);

// then you get the array with result key
JSONArray myJsonArray = jsonObject.getJSONArray("result");             
for (int i = 0; i < myJsonArray.length(); i++) {
  // now you can process the item here.
}

UPDATE

The above code is working. The remaining problem is there is a typo in your key. You're using "Timestamp" but the existing key is "TimeStamp". Here is the working code:

public class FetchData extends AsyncTask<Void,Void,Void> {

  String data=""; //all json lines after loop

  String dataParsed ="";
  String singleParsed =""; //parsed attributes
  @Override
  protected Void doInBackground(Void... voids) {
    //Background Thread i.e API request
    try {
      URL url = new URL("https://bittrex.com/api/v1.1/public/getmarketsummaries");
      HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
      InputStream inputStream = httpURLConnection.getInputStream(); //read data in from the connection
      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); //Buff reader to read the inputstream (otherwise we get ints)

      String line ="";
      //Loop that reads all lines and represents them to as a string
      while(line != null) {
        line = bufferedReader.readLine(); //read line of json and assign to "line" if not null
        data = data + line;
        Log.d("DATA", "line = " + line);
      }

      Log.d("DATA", "construct data = " + data);
      JSONObject jsonObject = new JSONObject(data);
      JSONArray myJsonArray = jsonObject.getJSONArray("result");
      for (int i = 0; i < myJsonArray.length(); i++) {
        //Itterate through the array and get each object i.e btc,ltc
        JSONObject myJsonObject = (JSONObject) myJsonArray.get(i);
        //Single JSON object parsed
        singleParsed = "Coin" + myJsonObject.get("MarketName") + "\n" +
            "high" + myJsonObject.get("High") + "\n" +
            "low" + myJsonObject.get("Low") + "\n" +
            "volume" + myJsonObject.get("Volume") + "\n" +
            "last" + myJsonObject.get("Last") + "\n" +
            "basevolume" + myJsonObject.get("BaseVolume") + "\n" +
            "time" + myJsonObject.get("TimeStamp") + "\n" +
            "bid" + myJsonObject.get("Bid") + "\n" +
            "ask" + myJsonObject.get("Ask") + "\n" +
            "openbuyorders" + myJsonObject.get("OpenBuyOrders") + "\n" +
            "opensellorders" + myJsonObject.get("OpenSellOrders") + "\n" +
            "prevday" + myJsonObject.get("PrevDay") + "\n" +
            "created" + myJsonObject.get("Created") + "\n";

        dataParsed = dataParsed + singleParsed + "\n";
      }

    }catch(MalformedURLException e ){
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } catch (JSONException e) {
      e.printStackTrace();
    }
    return null;
  }
  @Override
  protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);
    //UI thread
    //MainActivity.data.setText(this.dataParsed);
    Log.d("DATA", "data = " + this.dataParsed);
  }
}

1 Comment

@DavidDooley: There is a typo in your code. Please check my updated answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.