API输出在文本视图中不显示

发布于 2025-01-23 19:43:44 字数 2129 浏览 0 评论 0原文

我试图制作一个非常简单的天气应用,以显示某个城市的温度。

但是,当我按下按钮以获取温度时,文本不会改变。

如果我尝试获得温度

我的代码:


public class MainActivity extends AppCompatActivity {

    TextView textView;
    RequestQueue requestQueue;
    Button button;
    EditText etCityName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = findViewById(R.id.textView);
        button = findViewById(R.id.button);
        etCityName = findViewById(R.id.etCityName);

        requestQueue = Volley.newRequestQueue(this);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jsonParse();
            }
        });
    }
    public void jsonParse() {
        String url = "https://api.openweathermap.org/data/2.5/weather?q=" + etCityName.getText() + "&appid=token";

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) { ;
                        try {
                            JSONArray jsonArray = response.getJSONArray("main");

                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject url = jsonArray.getJSONObject(i);

                                String temp = url.getString("temp");
                                textView.setText(temp);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
         }
        });
        requestQueue.add(request);
    }
}

I tried to make a very simple weather app that shows the temperature of a certain city.

But when I press the button to get the temperature, the text doesn't change.

Here is what happens if I try to get the temperature

My code:


public class MainActivity extends AppCompatActivity {

    TextView textView;
    RequestQueue requestQueue;
    Button button;
    EditText etCityName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = findViewById(R.id.textView);
        button = findViewById(R.id.button);
        etCityName = findViewById(R.id.etCityName);

        requestQueue = Volley.newRequestQueue(this);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jsonParse();
            }
        });
    }
    public void jsonParse() {
        String url = "https://api.openweathermap.org/data/2.5/weather?q=" + etCityName.getText() + "&appid=token";

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) { ;
                        try {
                            JSONArray jsonArray = response.getJSONArray("main");

                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject url = jsonArray.getJSONObject(i);

                                String temp = url.getString("temp");
                                textView.setText(temp);
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
         }
        });
        requestQueue.add(request);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

霓裳挽歌倾城醉 2025-01-30 19:43:44

使用etcityname.getText()。toString

而不是etcityname.getText()在您的jsonparse()方法中

use etCityName.getText().toString

instead of etCityName.getText() in your jsonparse() method

老街孤人 2025-01-30 19:43:44
public class MainActivity extends AppCompatActivity {

    TextView textView;
    RequestQueue requestQueue;
    Button button;
    EditText etCityName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = findViewById(R.id.textView);
        button = findViewById(R.id.button);
        etCityName = findViewById(R.id.etCityName);

        requestQueue = Volley.newRequestQueue(this);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (etCityName.getText().toString().trim().isEmpty()) {
                 etCityName.setError("Required");
                }else{
                      jsonParse();
                }
            }
        });
    }
    public void jsonParse() {
        String url = "https://api.openweathermap.org/data/2.5/weather?q=" + etCityName.getText().toString().trim() + "&appid=token";

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) { ;
                        try {
                                String temp = response.getDouble("temp").toString();
                                textView.setText(temp);
                            
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
         }
        });
        requestQueue.add(request);
    }
}
public class MainActivity extends AppCompatActivity {

    TextView textView;
    RequestQueue requestQueue;
    Button button;
    EditText etCityName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = findViewById(R.id.textView);
        button = findViewById(R.id.button);
        etCityName = findViewById(R.id.etCityName);

        requestQueue = Volley.newRequestQueue(this);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (etCityName.getText().toString().trim().isEmpty()) {
                 etCityName.setError("Required");
                }else{
                      jsonParse();
                }
            }
        });
    }
    public void jsonParse() {
        String url = "https://api.openweathermap.org/data/2.5/weather?q=" + etCityName.getText().toString().trim() + "&appid=token";

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) { ;
                        try {
                                String temp = response.getDouble("temp").toString();
                                textView.setText(temp);
                            
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
         }
        });
        requestQueue.add(request);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文