Android|Android 基于百度定位sdk ,获取城市,使用json解析天气
关于百度定位sdk的使用 参考官方文档 https://lbsyun.baidu.com/index.php?title=android-locsdk
【Android|Android 基于百度定位sdk ,获取城市,使用json解析天气】JSON 解析 参考了 https://blog.csdn.net/double2hao/article/details/68482900
使用的 天气接口 http://wthrcdn.etouch.cn/weather_mini?city=北京
demo下载:https://download.csdn.net/download/weixin_42447313/10483975
实现后的效果
文章图片
文章图片
布局代码
定位代码
public Context context; public static String City= ""; public LocationClient mLocationClient = null; //LocationClient类是定位SDK的核心类 public BDLocationListener myListener = new MyLocationListener(); public BDLocationUtils(Context context){ this.context = context; }public void doLocation(){ //声明LocationClient类 mLocationClient = new LocationClient(context.getApplicationContext()); //注册监听函数 mLocationClient.registerLocationListener( myListener ); //初始化定位 initLocation(); //调用initLocation方法 }private void initLocation(){ LocationClientOption option = new LocationClientOption(); option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy); //可选,默认高精度,设置定位模式,高精度,低功耗,仅设备 option.setIsNeedAddress(true); //可选,设置是否需要地址信息,默认不需要 option.setOpenGps(true); //可选,默认false,设置是否使用gps mLocationClient.setLocOption(option); }private class MyLocationListener implements BDLocationListener { @Override public void onReceiveLocation(BDLocation location) { //获取定位结果 StringBuffer sb = new StringBuffer(256); sb.append(location.getCity()); //获取城市 City = location.getCity().replace("市",""); //replace 切割字符 去掉“市” } } }
解决安卓6.0以上权问题:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (checkSelfPermission(Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED || checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED || checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // 申请一个(或多个)权限,并提供用于回调返回的获取码(用户定义) requestPermissions(new String[]{ Manifest.permission.READ_PHONE_STATE, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE,}, BAIDU_READ_PHONE_STATE); } bdLocationUtils.doLocation(); //开启定位 bdLocationUtils.mLocationClient.start(); //开始定位 textView.setText("当前位置:"+BDLocationUtils.City); } else {Log.e("Permissions", "已经有权限了"); }
@Override public void onRequestPermissionsResult(int requestCode,String[] permissions, int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions,grantResults); switch(requestCode) { //requestCode即所声明的权限获取码,在checkSelfPermission时传入 case 1: BAIDU_READ_PHONE_STATE: if(grantResults[0] == PackageManager.PERMISSION_GRANTED) { //获取到权限,做相应处理 //调用定位SDK应确保相关权限均被授权,否则会引起定位失败 bdLocationUtils.doLocation(); //开启定位 bdLocationUtils.mLocationClient.start(); //开始定位 } else{ //没有获取到权限,做特殊处理 Log.e("Permissions","没有权限定位失败"); } break; default: break; } }
JSON解析
mRequestQueue = Volley.newRequestQueue(this); final String city = BDLocationUtils.City; String city_encode = null; try { city_encode = URLEncoder.encode(city,"utf-8"); //城市名转为中文
} catch (UnsupportedEncodingException e) { e.printStackTrace(); } JsonObjectRequest mJsonObjectRequest = new JsonObjectRequest( "http://wthrcdn.etouch.cn/weather_mini?city="+city_encode,// 根据城市名获取天气JSon
null, new Response.Listener< JSONObject>() { @Override public void onResponse(JSONObject response) { try { JSONObject data = https://www.it610.com/article/new JSONObject(response.getString("data")); JSONArray forecast = data.getJSONArray("forecast"); JSONObject todayWeather = forecast.getJSONObject(0); String wendu = data.getString("wendu") + "\n"; //String ganmao = data.getString("ganmao") + "\n"; String high = todayWeather.getString("high") + "\n"; String low = todayWeather.getString("low") + "~"; String date = todayWeather.getString("date") + ""; String type = todayWeather.getString("type")+"\n"; String city = data.getString("city") + "\n"; textView1.setText(city+date+type+"实时温度:"+wendu + low + high );
天气图标:
if (todayWeather.getString("type").toString().contains("多云")){ imageView1.setImageResource(R.drawable.duoyun1); }else if(todayWeather.getString("type").toString().contains("晴")){ imageView1.setImageResource(R.drawable.qingtian); }else if(todayWeather.getString("type").toString().contains("阴")){ imageView1.setImageResource(R.drawable.yintian);
推荐阅读
- android第三方框架(五)ButterKnife
- 基于微信小程序带后端ssm接口小区物业管理平台设计
- Android中的AES加密-下
- 基于|基于 antd 风格的 element-table + pagination 的二次封装
- 带有Hilt的Android上的依赖注入
- android|android studio中ndk的使用
- 基于爱,才会有“愿望”当“要求”。2017.8.12
- Android事件传递源码分析
- RxJava|RxJava 在Android项目中的使用(一)
- Android7.0|Android7.0 第三方应用无法访问私有库