You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android天气应用无法通过Volley获取OpenWeather API的JSON数据

解决Volley调用OpenWeather API无响应的问题

嘿,我看了你的代码,发现几个很容易忽略的问题,这应该就是你拿不到响应的原因,咱们一个个来解决:

1. 最关键的遗漏:请求没加入Volley队列

你创建了JsonObjectRequest但没有把它添加到Volley的RequestQueue里,这就相当于你写好了快递单却没交给快递员——请求根本没发出去!

给你的代码加上这两步:

// 获取RequestQueue实例(建议在Application类里单例化,避免重复创建)
RequestQueue queue = Volley.newRequestQueue(this);
// 将请求添加到队列,这才会触发网络请求
queue.add(jsonObjectRequest);

2. 完善错误监听,排查问题根源

你现在的ErrorListener是空的,就算请求失败也不知道是啥原因。赶紧加上错误日志,方便排查:

new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Log.e("WEATHER_ERROR", "请求失败原因: " + error.getMessage());
        // 如果有网络响应状态码,也打印出来
        if (error.networkResponse != null) {
            Log.e("WEATHER_ERROR", "HTTP状态码: " + error.networkResponse.statusCode);
        }
        error.printStackTrace();
    }
}

3. 把HTTP改成HTTPS

现在OpenWeather API推荐使用HTTPS,而且高版本Android默认限制明文HTTP请求,你的URL用的是http,大概率会被拦截。修改URL:

String url = "https://api.openweathermap.org/data/2.5/weather?q=bangalore,IN&appid=899bd4a7869e57cdaac65346d0659463";

4. 检查网络权限

确保你在AndroidManifest.xml里添加了网络权限,没有这个权限,APP根本没法发起网络请求:

<uses-permission android:name="android.permission.INTERNET" />

修改后的完整代码参考

currentTemperatureField = (TextView)findViewById(R.id.current_temperature_field);
String url = "https://api.openweathermap.org/data/2.5/weather?q=bangalore,IN&appid=899bd4a7869e57cdaac65346d0659463";

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest (Request.Method.GET, url, null, 
    new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            currentTemperatureField.setText("Response: " + response.toString());
            try{
                JSONObject mainJSONObject = response.getJSONObject("main");
                // 注意:OpenWeather返回的temp默认是开尔文,如果你需要摄氏度,记得转换(temp - 273.15)
                String temp = mainJSONObject.getString("temp");
                currentTemperatureField.setText(temp);
                Log.v("WEATHER","Response: " + response.toString());
            } catch(Exception e){
                e.printStackTrace();
                currentTemperatureField.setText("解析数据出错");
            }
        }
    }, 
    new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("WEATHER_ERROR", "请求失败: " + error.getMessage());
            if (error.networkResponse != null) {
                Log.e("WEATHER_ERROR", "状态码: " + error.networkResponse.statusCode);
            }
            error.printStackTrace();
            currentTemperatureField.setText("请求天气数据失败");
        }
    });

// 别忘了添加到请求队列!
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(jsonObjectRequest);

另外提个小细节:OpenWeather返回的温度默认是开尔文单位,如果你需要显示摄氏度,记得做个转换:Double.parseDouble(temp) - 273.15,再转成字符串显示。

内容的提问来源于stack exchange,提问作者rashmi singh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 07:49:32