簡體   English   中英

使用凌空字符串請求

[英]String request using volley

我正在嘗試從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);
    }
}

這是我得到的錯誤:

 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

我認為這與我采用String Request而不是jsonArrayRequest有關。

我也想將假設LTC,比特幣,ETH的數據檢索到自定義列表視圖。 我如何進入數據數組並使用其密鑰獲取每個硬幣的信息,例如-“ LTC”,“ BTC”,“ ETH”等。

我可能可以自己弄清楚,但需要此解析錯誤的幫助。

謝謝

采用

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

您的響應是JSONObject而不是JSONArray

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM