概述
关于百度定位sdk的使用 参考官方文档 https://lbsyun.baidu.com/index.php?title=android-locsdk
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
实现后的效果
布局代码
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.yazz.yaweather.MainActivity"> <TextView android:id="@+id/location_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textSize="16dp" /> <EditText android:id="@+id/et1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_below="@+id/location_view" android:hint="请输入城市名称:" /> <RelativeLayout android:id="@+id/rl1" android:layout_width="match_parent" android:layout_height="100dp" android:layout_below="@id/et1" android:layout_marginTop="20dp"> <TextView android:id="@+id/weather_view" android:layout_width="192dp" android:layout_height="match_parent" android:gravity="center" android:textSize="16dp" /> <ImageView android:id="@+id/iv1" android:layout_width="192dp" android:layout_height="match_parent" android:layout_toRightOf="@+id/weather_view" /> </RelativeLayout> <RelativeLayout android:id="@+id/rl2" android:layout_width="match_parent" android:layout_height="100dp" android:layout_below="@id/rl1" android:layout_marginTop="20dp"> <TextView android:id="@+id/weather1_view" android:layout_width="192dp" android:layout_height="match_parent" android:gravity="center" android:textSize="16dp" /> <ImageView android:id="@+id/iv2" android:layout_width="192dp" android:layout_height="match_parent" android:layout_toRightOf="@+id/weather1_view" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="100dp" android:layout_below="@id/rl2" android:layout_marginTop="20dp"> <TextView android:id="@+id/weather2_view" android:layout_width="192dp" android:layout_height="match_parent" android:gravity="center" android:textSize="16dp" /> <ImageView android:id="@+id/iv3" android:layout_width="192dp" android:layout_height="match_parent" android:layout_toRightOf="@+id/weather2_view" /> </RelativeLayout> <Button android:id="@+id/start_location" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentStart="true" android:text="定位并获取天气" /> <Button android:id="@+id/start_weather" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignTop="@+id/start_location" android:text="获取指定位置天气" /> </RelativeLayout>
定位代码
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 = 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 基于百度定位sdk ,获取城市,使用json解析天气的全部内容,希望文章能够帮你解决Android 基于百度定位sdk ,获取城市,使用json解析天气所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复