首页 > 解决方案 > 从 Web 获取 JSON 响应不起作用

问题描述

我正在尝试获取json. 虽然代码过去可以工作,但我对其进行了一些更改,但现在它不起作用。该代码应该兑换货币。

            sendjsonrequest();
            assert tv != null;

            //Request a string response from the URL resource
            StringRequest stringRequest = new StringRequest(Request.Method.GET, "https://free.currencyconverterapi.com/api/v6/convert?q=EUR_ILS&compact=ultra&apiKey=6f4b50fae0c5c91a84bd",
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            // Display the response string.
                            tv.setText("Response is: " + Float.parseFloat(response) * Float.parseFloat(et1.getText().toString()));
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    tv.setText("Oops! That didn't work!");
                }
            });

            //Instantiate the RequestQueue and add the request to the queue
            RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
            queue.add(stringRequest);
            String str1 = et1.getText().toString();
            str = Float.parseFloat(str1);
            float e = f * str;
            String ef = Float.toString(e);
            tv.setText(ef);
        }
    });

    button = findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(Main2Activity.this, MainActivity.class);
            startActivity(intent);
        }
    });

}

public void sendjsonrequest() {//sends the request to the website and retrieves specififc data (exchange rate between EUR to ILS)
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {
                tv1 = response.getString("EUR_ILS");


                f = Float.parseFloat(tv1);
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });
    rq.add(jsonObjectRequest);
}
}

标签: android

解决方案


希望它对你有帮助,你只需要像这样表演

private void loadHeroList() {
    StringRequest stringRequest = new StringRequest(Request.Method.GET, JSON_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    try {
                        //getting the whole json object from the response
                        JSONObject obj = new JSONObject(response);

                        //we have the array named hero inside the object
                        //so here we are getting that json array
                        JSONArray heroArray = obj.getJSONArray("heroes");

                        //now looping through all the elements of the json array
                        for (int i = 0; i < heroArray.length(); i++) {
                            //getting the json object of the particular index inside the array
                            JSONObject heroObject = heroArray.getJSONObject(i);

                            //creating a hero object and giving them the values from json object
                            Heroes hero = new Heroes(heroObject.getString("name"), heroObject.getString("imageurl"));

                            //adding the hero to herolist
                            heroList.add(hero);
                        }

                        //creating custom adapter object
                        ListViewAdapter adapter = new ListViewAdapter(heroList, getApplicationContext());

                        //adding the adapter to listview
                        listView.setAdapter(adapter);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    //displaying the error in toast if occurrs
                    Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });

    //creating a request queue
    RequestQueue requestQueue = Volley.newRequestQueue(this);

    //adding the string request to request queue
    requestQueue.add(stringRequest);
}

推荐阅读