• Android 天气应用开发


    百度 API Store中很多免费的天气API,因此写一个天气应用相对变得很容易。

    首先尝试API给的接口,接受返回数据。

    public class MainActivity extends Activity implements View.OnClickListener, Runnable {
        static String TAG = "Weather";
        Button getWeather;
        TextView showJson;
    
        private static Handler myHandler;
        Thread networkThread;
    
    
        String httpUrl = "http://apis.baidu.com/heweather/weather/free";
        String httpArg = "city=dalian";
        String jsonResult;
        boolean threadIsAlive;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            getWeather = (Button) findViewById(R.id.getWeather);
            getWeather.setOnClickListener(this);
            showJson = (TextView) findViewById(R.id.showJson);
            showJson.setMovementMethod(ScrollingMovementMethod.getInstance());
    
            networkThread = new Thread(this);
    
            myHandler = new Handler(new Handler.Callback() {
                @Override
                public boolean handleMessage(Message msg) {
                    showJson.setText(jsonResult);
                    return true;
                }
            });
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.getWeather:
                    if (!threadIsAlive) {
                        networkThread.start();
                        threadIsAlive = true;
                    }
                    break;
                default:
                    break;
            }
        }
    
    
        @Override
        public void run() {
            jsonResult = request(httpUrl, httpArg);
            myHandler.sendEmptyMessage(0);
    //        Log.i(TAG, jsonResult);
        }
    
        /**
         * @param httpUrl :请求接口
         * @param httpArg :参数
         * @return 返回结果
         */
        public static String request(String httpUrl, String httpArg) {
            BufferedReader reader;
            String result = null;
            StringBuffer sbf = new StringBuffer();
            httpUrl = httpUrl + "?" + httpArg;
    
            try {
                URL url = new URL(httpUrl);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                // 填入apikey到HTTP header
                connection.setRequestProperty("apikey", "apikey请在百度开发者个人中心-个人信息中查看");
                connection.connect();
                InputStream is = connection.getInputStream();
                reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                String strRead;
                while ((strRead = reader.readLine()) != null) {
                    sbf.append(strRead);
                    sbf.append("
    ");
                }
                reader.close();
                result = sbf.toString();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return result;
        }
    
    }

    a、Android的框架及其优缺点掌握,每层的优化点明确

    b、开发的方式设计review参加

    c、只能端末开发的风险、技术等调查。主要着眼于技术及与技术相关的实例、注意事

    项、业界动态等。每月一次介绍及分享。

    D、向嵌入式基板的移植,以及中间件、驱动的开发

  • 相关阅读:
    移动端摇一摇与重力感应事件
    百度的js日历
    wow.js
    pc网页中嵌入百度地图
    微信小程序之倒计时插件 wxTimer
    IE常见的兼容处理
    particles.js使用及配置
    微信小程序之swiper组件高度自适应
    js获取元素的滚动高度,和距离顶部的高度
    vue实现移动端触屏拖拽功能
  • 原文地址:https://www.cnblogs.com/fansen/p/5148945.html
Copyright © 2020-2023  润新知