0

I am trying to get data of Crypto coins from https://min-api.cryptocompare.com/data/all/coinlist.

Coins.java

public class coins extends AppCompatActivity {
List<coinlist> coinList;
ListView listView;
TextView empty;
//String coin_name,symbol;
//Double price,market_cap,tot_marketcap,port,onehour,sevendays,twenty4hour,coin;
DecimalFormat df = new DecimalFormat("###,###,###,###,###.##");
final Context mContext = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_coins);
    setTitle("Coins");
    listView = (ListView) findViewById(R.id.list_alts);
    coinList = new ArrayList<>();
    empty = (TextView) findViewById(R.id.alt_empty);
    listfill();
    //coinList.add(new ico(c.getString(0), c.getString(3), c.getString(4), c.getDouble(1), c.getDouble(2),c.getDouble(5),df.format(prof)));
    if(coinList.isEmpty()){
        listView.setVisibility(View.GONE);
        empty.setVisibility(View.VISIBLE);
    }
    else {
        MyAltsAdapter adapter = new MyAltsAdapter(this, R.layout.list_alts, coinList);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView TV_name = (TextView) view.findViewById(R.id.name);

                //String name = TV_name.getText().toString();
                //Intent intent = new Intent(coins.this, personalView.class);
                //intent.putExtra(itemname, name);
                //startActivity(intent);
            }
        });
    }

}

private void listfill() {

    String URL_ROOT = GlobalClass.API_URL;

    RequestQueue queue = Volley.newRequestQueue(this);
    StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_ROOT, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            if (response != null) {
                System.out.println(response);
            }
            try {
                JSONArray jsonArray = new JSONArray(response);

                JSONObject jsonObject = jsonArray.getJSONObject(0);
                String code = jsonObject.getString("Response");
                if (code.equals("Success")) {
                    Toast.makeText(coins.this, "Success", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(coins.this, code, Toast.LENGTH_SHORT).show();
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }


        }
    },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    System.out.println(String.valueOf(volleyError));

                }
            }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            // return super.getParams();
            Map<String, String> params = new HashMap<String, String>();
            return super.getParams();
        }
    };
    queue.add(stringRequest);
   // Controller.getPermission().addToRequestQueue(stringRequest);
    }
}

This is the error I am getting:

 02-11 19:05:11.740 32158-32158/com.example.nikko.icopersonal W/System.err: org.json.JSONException: Value {"Response":"Success","Message":"Coin list succesfully returned! This api is moving to https:\/\/min-api.cryptocompare.com\/data\/all\/coinlist, please change the path.","BaseImageUrl":"https:\/\/www.cryptocompare.com",...**(its really big)**
02-11 19:05:11.759 32158-32158/com.example.nikko.icopersonal W/System.err:     at org.json.JSON.typeMismatch(JSON.java:111)
02-11 19:05:11.759 32158-32158/com.example.nikko.icopersonal W/System.err:     at org.json.JSONArray.<init>(JSONArray.java:96)
02-11 19:05:11.759 32158-32158/com.example.nikko.icopersonal W/System.err:     at org.json.JSONArray.<init>(JSONArray.java:108)
02-11 19:05:11.759 32158-32158/com.example.nikko.icopersonal W/System.err:     at com.example.nikko.icopersonal.coins$2.onResponse(coins.java:85)
02-11 19:05:11.759 32158-32158/com.example.nikko.icopersonal W/System.err:     at com.example.nikko.icopersonal.coins$2.onResponse(coins.java:78)
02-11 19:05:11.759 32158-32158/com.example.nikko.icopersonal W/System.err:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
02-11 19:05:11.759 32158-32158/com.example.nikko.icopersonal W/System.err:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
02-11 19:05:11.759 32158-32158/com.example.nikko.icopersonal W/System.err:     at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
02-11 19:05:11.759 32158-32158/com.example.nikko.icopersonal W/System.err:     at android.os.Handler.handleCallback(Handler.java:769)
02-11 19:05:11.759 32158-32158/com.example.nikko.icopersonal W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:98)
02-11 19:05:11.760 32158-32158/com.example.nikko.icopersonal W/System.err:     at android.os.Looper.loop(Looper.java:164)
02-11 19:05:11.760 32158-32158/com.example.nikko.icopersonal W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6540)
02-11 19:05:11.760 32158-32158/com.example.nikko.icopersonal W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
02-11 19:05:11.760 32158-32158/com.example.nikko.icopersonal W/System.err:     at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
02-11 19:05:11.760 32158-32158/com.example.nikko.icopersonal W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
02-11 19:05:11.768 32158-32158/com.example.nikko.icopersonal D/Volley: [2] Request.finish: 3333 ms: [ ] https://min-api.cryptocompare.com/data/all/coinlist 0x3504fd88 NORMAL 1

I think its something to do with me taking String Request instead of jsonArrayRequest.

Also I want to retrieve the data of suppose LTC, Bitcoin, ETH to a custom listview. How do I get inside the data array and get the info for each coin using its key eg - "LTC","BTC","ETH" etc.

I can probably figure that out myself but need help with this parse error.

Thank you

1 Answer 1

2

Use

JSONObject jsonObject = new JSONObject(response);
//String code = jsonObject.getString("Response");
// or better
String code = jsonObject.optString("Response","");

Your response is JSONObject not JSONArray

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.