zoukankan      html  css  js  c++  java
  • 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、向嵌入式基板的移植,以及中间件、驱动的开发

  • 相关阅读:
    私有 composer 包创建
    随机数是如何生成的
    TCP 三次握手的意义
    何为真何为假
    Python流程控制语句详解
    Python类中装饰器classmethod,staticmethod,property,
    函数进阶
    初识函数
    文件操作
    is ==小数据池编码解码
  • 原文地址:https://www.cnblogs.com/fansen/p/5148945.html
Copyright © 2011-2022 走看看