Android获取天气预报

完整源代码:获取天气预报(天气,气温,风力...)WebService - 云代码

 

部分核心代码:

	public void getWeather(String cityName) {
		try {
			SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);
			rpc.addProperty("theCityName", cityName);

			HttpTransportSE ht = new HttpTransportSE(URL);
			ht.debug = true;

			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
					SoapEnvelope.VER11);

			envelope.bodyOut = rpc;
			envelope.dotNet = true;
			envelope.setOutputSoapObject(rpc);

			ht.call(SOAP_ACTION, envelope);
			// ht.call(null, envelope);

			SoapObject result = (SoapObject) envelope.bodyIn;
			detail = (SoapObject) result
					.getProperty("getWeatherbyCityNameResult");

			System.out.println("result" + result);
			System.out.println("detail" + detail);
			Toast.makeText(MainActivity.this, detail.toString(),
					Toast.LENGTH_LONG).show();
			parseWeather(detail);

			return;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void parseWeather(SoapObject detail)
			throws UnsupportedEncodingException {
		String date = detail.getProperty(6).toString();
		weatherToday = "今天:" + date.split(" ")[0];
		weatherToday = weatherToday + "\n天气:" + date.split(" ")[1];
		weatherToday = weatherToday + "\n气温:"
				+ detail.getProperty(5).toString();
		weatherToday = weatherToday + "\n风力:"
				+ detail.getProperty(7).toString() + "\n";
		System.out.println("weatherToday is " + weatherToday);
		Toast.makeText(MainActivity.this, weatherToday, Toast.LENGTH_LONG)
				.show();

	}


 

要在 Android 应用中获取天气预报,你可以使用一些第三方天气 API 来实现。以下是一个简单的示例,演示如何使用和解析天气 API 响应: 1. 添加以下依赖项到你的 app build.gradle 文件中: ``` dependencies { implementation 'com.squareup.okhttp3:okhttp:4.9.1' implementation 'com.google.code.gson:gson:2.8.6' } ``` 2. 在你的 Activity 或 Fragment 中,创建一个函数来获取天气数据: ```java private void getWeatherData(String city) { String apiKey = "your_api_key"; // 替换成你的 API Key String url = "https://api.openweathermap.org/data/2.5/weather?q=" + city + "&appid=" + apiKey; OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url(url) .build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { e.printStackTrace(); } @Override public void onResponse(Call call, Response response) throws IOException { if (response.isSuccessful()) { String responseBody = response.body().string(); Gson gson = new Gson(); WeatherData weatherData = gson.fromJson(responseBody, WeatherData.class); // 处理天气数据 // 可以在这里更新 UI } } }); } ``` 3. 创建一个 WeatherData 类,用于解析 API 响应: ```java public class WeatherData { private Main main; private Weather[] weather; public Main getMain() { return main; } public Weather[] getWeather() { return weather; } public class Main { private double temp; public double getTemp() { return temp; } } public class Weather { private String main; private String description; public String getMain() { return main; } public String getDescription() { return description; } } } ``` 4. 在你的 Activity 或 Fragment 中,调用 getWeatherData() 函数,并传入城市名称: ```java getWeatherData("Beijing"); ``` 这个例子使用了 OpenWeatherMap API 来获取天气数据。你需要注册一个账户,获取你自己的 API Key,然后将它替换掉代码中的 "your_api_key"。当然,你也可以使用其他的天气 API,只需要根据 API 的文档来修改代码即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT开发者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值