Android 从web获取JSON响应不起作用

Android 从web获取JSON响应不起作用,android,Android,我正在尝试获取一个json。虽然代码过去是有效的,但我对它做了一点修改,现在它不起作用了。密码应该是用来兑换钱的 sendjsonrequest(); assert tv != null; //Request a string response from the URL resource StringRequest stringRequest = new StringRequest(Request.

我正在尝试获取一个
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);
}
}
sendjsonrequest();
断言电视!=无效的
//从URL资源请求字符串响应
StringRequest StringRequest=新的StringRequest(Request.Method.GET)https://free.currencyconverterapi.com/api/v6/convert?q=EUR_ILS&compact=ultra&apiKey=6f4b50fae0c5c91a84bd",
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
//显示响应字符串。
tv.setText(“响应是:”+Float.parseFloat(响应)*Float.parseFloat(et1.getText().toString()));
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
setText(“哎呀!那没用!”);
}
});
//实例化RequestQueue并将请求添加到队列中
RequestQueue=Volley.newRequestQueue(getApplicationContext());
添加(stringRequest);
字符串str1=et1.getText().toString();
str=Float.parseFloat(str1);
浮点数e=f*str;
字符串ef=Float.toString(e);
tv.setText(ef);
}
});
按钮=findViewById(R.id.button);
setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
意向意向=新意向(Main2Activity.this、MainActivity.class);
星触觉(意向);
}
});
}
public void sendjsonrequest(){//将请求发送到网站并检索指定的IFC数据(欧元对ILS的汇率)
JsonObjectRequest JsonObjectRequest=新的JsonObjectRequest(Request.Method.GET,url,null,new Response.Listener()){
@凌驾
公共void onResponse(JSONObject响应){
试一试{
tv1=response.getString(“EUR_ILS”);
f=浮点。解析浮点(tv1);
}捕获(JSONException e){
e、 printStackTrace();
}
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
}
});
rq.add(jsonObjectRequest);
}
}

希望这对您有所帮助,您只需像这样执行即可

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);
}
private void loadHeroList(){
StringRequest StringRequest=新的StringRequest(Request.Method.GET,JSON_URL,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
试一试{
//从响应中获取整个json对象
JSONObject obj=新的JSONObject(响应);
//我们在对象中有一个名为hero的数组
//这里我们得到了json数组
JSONArray heroArray=obj.getJSONArray(“英雄”);
//现在遍历json数组的所有元素
for(int i=0;i
public void sendjsonrequest(){
StringRequest StringRequest=新建StringRequest(Request.Method.GET,url,new Response.Listener()){
@凌驾
公共void onResponse(字符串响应){
试一试{
JSONObject JSONObject=新JSONObject(响应);
String tv1=jsonObject.getString(“EUR_ILS”);
}捕获(JSONException e){
e、 printStackTrace();
}
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
}
});
RequestQueue RequestQueue=Volley.newRequestQueue(getApplicationContext());
添加(stringRequest);
}

使用改装,您可以很容易地从web获得响应

请按照以下链接设置改装代码:-


哪一行导致错误?那是什么?你能解释一下吗?你能告诉我们logcat错误吗?你改变了什么?这是我们需要猜测的测验吗?
 public void sendjsonrequest() {
    StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                JSONObject jsonObject = new JSONObject(response);
                String tv1 = jsonObject.getString("EUR_ILS");
            } catch (JSONException e) {
                e.printStackTrace();
            }

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

        }
    });
    RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
    requestQueue.add(stringRequest);
}