十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
①取出地址中的返回值(getWeatherReader方法)
专注于为中小企业提供网站设计、成都网站制作服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业永吉免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了成百上千家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
②解析json格式的字符串
③形成你要展示的天气预报效果
public static String getWeatherReader() {//取得接口字符串
String currentLine = "";
String strReturn = "";
URL url = null;
HttpURLConnection conn = null;
InputStream in = null;
BufferedReader buff = null;
try {
url = new URL("");
System.out.println(url.toURI());
//打开地址链接
conn = (HttpURLConnection)url.openConnection();
conn.connect();
//接收数据
in = conn.getInputStream();
//如有乱码注意编码方式,如:UTF-8
buff = new BufferedReader(new InputStreamReader(in, "gb2312"));
while((currentLine = buff.readLine()) != null) {
strReturn += currentLine;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
in.close();
buff.close();
} catch (IOException e) {
return "8EF0000";
}
}
return strReturn;
}
package com.nrzc.weatherstation;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import java.util.Timer;
import java.util.TimerTask;
/**
* 环境传感器
* 气象站
*/
public class MainActivity extends AppCompatActivity {
private SensorManager sensorManager;
private TextView temperatureTextView;
private TextView pressureTextView;
private TextView lightTextView;
private float currentTemperature=Float.NaN;
private float currentPressure=Float.NaN;
private float currentLight=Float.NaN;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
Timer updateTimer=new Timer("weatherUpdate");
updateTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
updateGUI();
}
},0,1000);
}
private void init(){
temperatureTextView=(TextView)findViewById(R.id.temperature);
pressureTextView=(TextView)findViewById(R.id.pressure);
lightTextView=(TextView)findViewById(R.id.light);
sensorManager=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
}
private final SensorEventListener tempSensorEventListener=new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
currentTemperature=event.values[0];
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
private final SensorEventListener pressureSensorEventListener=new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
currentPressure=event.values[0];
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
private final SensorEventListener lightSensorEventListener=new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
currentLight=event.values[0];
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
@Override
protected void onResume() {
super.onResume();
Sensor lightSensor=sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
if (lightSensor!=null)
sensorManager.registerListener(lightSensorEventListener,
lightSensor,
SensorManager.SENSOR_DELAY_NORMAL);
else
lightTextView.setText("Light Sensor Unavailable");
Sensor pressureSensor=sensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE);
if (pressureSensor!=null)
sensorManager.registerListener(pressureSensorEventListener,
pressureSensor,SensorManager.SENSOR_DELAY_NORMAL);
else
pressureTextView.setText("Barometer Unavailable");
Sensor temperatureSensor=sensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);
if (temperatureSensor!=null)
sensorManager.registerListener(tempSensorEventListener,
temperatureSensor,
SensorManager.SENSOR_DELAY_NORMAL);
else
temperatureTextView.setText("Thermometer Unavailable");
}
@Override
protected void onPause() {
sensorManager.unregisterListener(pressureSensorEventListener);
sensorManager.unregisterListener(tempSensorEventListener);
sensorManager.unregisterListener(lightSensorEventListener);
super.onPause();
}
private void updateGUI(){
runOnUiThread(new Runnable() {
@Override
public void run() {
if(!Float.isNaN(currentPressure)){
pressureTextView.setText(currentPressure+"hPa");
pressureTextView.invalidate();
}
if (!Float.isNaN(currentLight)){
String lightStr="Sunny";
if (currentLight=SensorManager.LIGHT_CLOUDY)
lightStr="night";
else if (currentLight=SensorManager.LIGHT_OVERCAST)
lightStr="Cloudy";
else if (currentLight=SensorManager.LIGHT_SUNLIGHT)
lightStr="Overcast";
lightTextView.setText(lightStr);
lightTextView.invalidate();
}
if (!Float.isNaN(currentTemperature)){
temperatureTextView.setText(currentTemperature+"C");
temperatureTextView.invalidate();
}
}
});
}
}
//通过中国天气api调用
private String getWeatherInfo2(){
StringBuilder info = new StringBuilder();
try {
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httget = new HttpGet("");
ResponseHandlerString responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httget, responseHandler);
System.out.println(responseBody);
JsonParser jp = new JsonParser();
JsonElement jse = jp.parse(responseBody);
JsonObject jso = jse.getAsJsonObject().get("weatherinfo").getAsJsonObject();
// String updTime = jso.get("fchh").getAsString();
// if(updTime != null){
// //温度
// String j = jso.get("temp1").getAsString();//今天
// String m = jso.get("temp2").getAsString();//明天
// //天气情况
// String j_weather = jso.get("weather1").getAsString();//今天
// String m_weather = jso.get("weather2").getAsString();//明天
// //风向风力
// String j_wind = jso.get("wind1").getAsString();//今天
// String m_wind = jso.get("wind2").getAsString();//明天
// info.append("今天:").append(j).append(" ").append(j_weather).append(" ").append(j_wind).append("\n");
// info.append("明天:").append(m).append(" ").append(m_weather).append(" ").append(m_wind).append("\n");
// }
String updTime = jso.get("fchh").getAsString();
if(updTime != null){
if(!updTime.trim().equals("18")){
//温度
String j = jso.get("temp1").getAsString();//今天
String m = jso.get("temp2").getAsString();//明天
//天气情况
String j_weather = jso.get("weather1").getAsString();//今天
String m_weather = jso.get("weather2").getAsString();//明天
//风向风力
String j_wind = jso.get("wind1").getAsString();//今天
String m_wind = jso.get("wind2").getAsString();//明天
info.append("今天:").append(j).append(" ").append(j_weather).append(" ").append(j_wind).append("\n");
info.append("明天:").append(m).append(" ").append(m_weather).append(" ").append(m_wind).append("\n");
}else{
//18
//温度
String temp1 = jso.get("temp1").getAsString();//今天
String temp2 = jso.get("temp2").getAsString();//今天
String temp3 = jso.get("temp3").getAsString();//今天
String j = temp1.split("~")[1] + "~" + temp2.split("~")[0];
String m = temp2.split("~")[1] + "~" + temp3.split("~")[0];//明天
//天气情况
String weather1 = jso.get("weather1").getAsString();
String weather2 = jso.get("weather2").getAsString();
String weather3 = jso.get("weather3").getAsString();
String j_weather = "";
String j_weather_part1 = "";
String j_weather_part2 = "";
//判断是否有转
if(weather1.indexOf("转") 0){
//有
j_weather_part1 = weather1.split("转")[1];
}else{
j_weather_part1 = weather1;
}
if(weather2.indexOf("转") 0){
//有
j_weather_part2 = weather2.split("转")[0];
}else{
j_weather_part2 = weather2;
}
if(j_weather_part1.equalsIgnoreCase(j_weather_part2)){
j_weather = j_weather_part1;//今天
}else{
j_weather = j_weather_part1 + "转" + j_weather_part2;//今天
}
String m_weather = "";
String m_weather_part1 = "";
String m_weather_part2 = "";
//判断是否有转
if(weather2.indexOf("转") 0){
//有
m_weather_part1 = weather2.split("转")[1];
}else{
m_weather_part1 = weather2;
}
if(weather3.indexOf("转") 0){
//有
m_weather_part2 = weather3.split("转")[0];
}else{
m_weather_part2 = weather3;
}
if(m_weather_part1.equalsIgnoreCase(m_weather_part2)){
m_weather = m_weather_part1;//今天
}else{
m_weather = m_weather_part1 + "转" + m_weather_part2;//明天
}
//风向风力
String j_wind = jso.get("wind2").getAsString();//今天
String m_wind = jso.get("wind3").getAsString();//明天
info.append("今天:").append(j).append(" ").append(j_weather).append(" ").append(j_wind).append("\n");
info.append("明天:").append(m).append(" ").append(m_weather).append(" ").append(m_wind).append("\n");
}
}
} catch (Exception e) {
}
return info.toString();
}