public class MainActivity extends AppCompatActivity {
public LocationClient mLocationClient = null;
public BDLocationListener myListener = new MyLocationListener();
TextView textView;
String mCity = "";//存放当前城市
String mCurrentLocation = "";//当前位置信息
String mTem = "";//当前温度
TextView mCitytv;//显示城市
TextView mWeathertv;//显示天气
TextView mTemtv;//显示温度
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//绑定控件
findview();
//获取位置
getLocation();
}
private void findview() {
mCitytv = (TextView) findViewById(R.id.x_CurrentCity);
mWeathertv = (TextView) findViewById(R.id.x_CurrentWeather);
mTemtv = (TextView) findViewById(R.id.x_CurrentTem);
}
/*
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
Bundle bundle = msg.getData();
String location = bundle.getString("位置");
//将位置传入异步线程中请求天气信息
new ASyncUploadImage().execute(location);
}
};
*/
private void getLocation() {
//获取地理位置管理器
mLocationClient = new LocationClient(getApplicationContext()); //声明LocationClient类
initLocation();
mLocationClient.registerLocationListener(myListener); //注册监听函数
mLocationClient.start();
mLocationClient.requestLocation();
}
private void initLocation() {
LocationClientOption option = new LocationClientOption();
option.setIsNeedAddress(true);
option.setOpenGps(true);
option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy
);//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备
option.setCoorType("gcj02");//可选,默认gcj02,设置返回的定位结果坐标系
//int span = 1000;
//option.setScanSpan(span);//可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的
mLocationClient.setLocOption(option);
}
public class MyLocationListener implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
//Receive Location
//获取城市
String city = location.getCity();
/*Bundle bundle = new Bundle();
bundle.putString("位置", city);
Message msg=Message.obtain();
msg.setData(bundle);
msg.what = 0;
handler.sendMessage(msg);*/
// handler.sendEmptyMessage(0);
mCitytv.setText(city);
ASyncUploadImage as = new ASyncUploadImage();
as.execute(city);
}
}
private class ASyncUploadImage extends AsyncTask {
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//处理接收到的json字符串 这个方法是在UI线程执行,所以能直接更新界面
getJson(s);
}
@Override
protected String doInBackground(String... params) {
//返回接收到的数据
return getData(params[0]);
}
}
private String getData(String string) {
//将传进来的城市名转为utf-8格式
String strUTF8 = null;
try {
strUTF8 = URLDecoder.decode(string, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//拼接url地址
String url = "http://op.juhe.cn/onebox/weather/query?cityname=" + strUTF8 + "&key=a7c1f7ebf5f9484e8e5722f54e66d227";
Log.i("TAG", "url=" + url);
InputStreamReader isr;
String result = "";
try {
//获取url的输入流
isr = new InputStreamReader(new URL(url).openStream(), "utf-8");
BufferedReader br = new BufferedReader(isr);
String line = "";
//将输入流中的数据保存到字符串result中
while ((line = br.readLine()) != null) {
result += line;
}
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
private void getJson(String json) {
//json字符串处理
try {
JSONObject jsonObject = new JSONObject(json);
String reason = jsonObject.getString("reason");
Log.i("TAG", "reason=" + reason);
String result = jsonObject.getString("result");
Log.i("TAG", "result=" + result);
JSONObject jsonResult = new JSONObject(result);
String data = jsonResult.getString("data");
Log.i("TAG", "data=" + data);
JSONObject jsonData = new JSONObject(data);
String realtime = jsonData.getString("realtime");
Log.i("TAG", "realtime=" + realtime);
JSONObject jsonRealtime = new JSONObject(realtime);
String weather = jsonRealtime.getString("weather");
JSONObject jsonWeather = new JSONObject(weather);
String info = jsonWeather.getString("info");
Log.i("TAG", "info=" + info);
String temperature = jsonWeather.getString("temperature");
Log.i("TAG", "temperature=" + temperature);
//将读取到的信息显示到界面
//显示温度
mTemtv.setText(temperature);
//显示天气
mWeathertv.setText(info);
} catch (JSONException e) {
e.printStackTrace();
}
}
}