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

  • 相关阅读:
    机器学习: t-Stochastic Neighbor Embedding 降维算法 (二)
    数学辨异 —— 泰勒展开与等比数列求和
    HDU 4705 Y
    C#实现的内存分页机制的一个实例
    java程序获得SqlServer数据表的表结构
    GLSL中的各种变量总结
    HTTP协议学习
    Jedis中的一致性hash
    C语言数据结构----双向链表
    ios7毛玻璃效果实现
  • 原文地址:https://www.cnblogs.com/fansen/p/5148945.html
Copyright © 2020-2023  润新知