根据城市名获取天气

这是我自己的笔记,如果有侵犯到谁的著作权,请留言。

public static void getWeatherDatafromNet(String cityCodeName) {
final String addressName = "http://wthrcdn.etouch.cn/weather_mini?city=" + cityCodeName;
final String address = "http://wthrcdn.etouch.cn/WeatherApi?citykey=" + cityCodeName;
Log.d("Address:", addressName);
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection urlConnection = null;
try {
URL url = new URL(addressName);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setConnectTimeout(8000);
urlConnection.setReadTimeout(8000);
InputStream in = urlConnection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuffer sb = new StringBuffer();
String str;
while ((str = reader.readLine()) != null) {
sb.append(str);
Log.d("date from url", str);
}
String response = sb.toString();
Log.d("response", response);
EventBus.getDefault().post(response);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
需要网络权限

你可能感兴趣的:(根据城市名获取天气)