zoukankan      html  css  js  c++  java
  • 每日总结

    1.继续昨天的代码验证

    获取数据类:GetData.java:

    /**
     * Created by Jay on 2015/9/7 0007.
     */
    public class GetData {
        // 定义一个获取网络图片数据的方法:
        public static byte[] getImage(String path) throws Exception {
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            // 设置连接超时为5秒
            conn.setConnectTimeout(5000);
            // 设置请求类型为Get类型
            conn.setRequestMethod("GET");
            // 判断请求Url是否成功
            if (conn.getResponseCode() != 200) {
                throw new RuntimeException("请求url失败");
            }
            InputStream inStream = conn.getInputStream();
            byte[] bt = StreamTool.read(inStream);
            inStream.close();
            return bt;
        }
    
        // 获取网页的html源代码
        public static String getHtml(String path) throws Exception {
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(5000);
            conn.setRequestMethod("GET");
            if (conn.getResponseCode() == 200) {
                InputStream in = conn.getInputStream();
                byte[] data = StreamTool.read(in);
                String html = new String(data, "UTF-8");
                return html;
            }
            return null;
        }
    }

    MainActivity.java

    public class MainActivity extends AppCompatActivity {
    
        private TextView txtMenu, txtshow;
        private ImageView imgPic;
        private WebView webView;
        private ScrollView scroll;
        private Bitmap bitmap;
        private String detail = "";
        private boolean flag = false;
        private final static String PIC_URL = "https://ww2.sinaimg.cn/large/7a8aed7bgw1evshgr5z3oj20hs0qo0vq.jpg";
        private final static String HTML_URL = "https://www.baidu.com";
    
        // 用于刷新界面
        private Handler handler = new Handler() {
            public void handleMessage(android.os.Message msg) {
                switch (msg.what) {
                    case 0x001:
                        hideAllWidget();
                        imgPic.setVisibility(View.VISIBLE);
                        imgPic.setImageBitmap(bitmap);
                        Toast.makeText(MainActivity.this, "图片加载完毕", Toast.LENGTH_SHORT).show();
                        break;
                    case 0x002:
                        hideAllWidget();
                        scroll.setVisibility(View.VISIBLE);
                        txtshow.setText(detail);
                        Toast.makeText(MainActivity.this, "HTML代码加载完毕", Toast.LENGTH_SHORT).show();
                        break;
                    case 0x003:
                        hideAllWidget();
                        webView.setVisibility(View.VISIBLE);
                        webView.loadDataWithBaseURL("", detail, "text/html", "UTF-8", "");
                        Toast.makeText(MainActivity.this, "网页加载完毕", Toast.LENGTH_SHORT).show();
                        break;
                    default:
                        break;
                }
            }
    
            ;
        };
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            setViews();
        }
    
        private void setViews() {
            txtMenu = (TextView) findViewById(R.id.txtMenu);
            txtshow = (TextView) findViewById(R.id.txtshow);
            imgPic = (ImageView) findViewById(R.id.imgPic);
            webView = (WebView) findViewById(R.id.webView);
            scroll = (ScrollView) findViewById(R.id.scroll);
            registerForContextMenu(txtMenu);
        }
    
        // 定义一个隐藏所有控件的方法:
        private void hideAllWidget() {
            imgPic.setVisibility(View.GONE);
            scroll.setVisibility(View.GONE);
            webView.setVisibility(View.GONE);
        }
    
        @Override
        // 重写上下文菜单的创建方法
        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
            MenuInflater inflator = new MenuInflater(this);
            inflator.inflate(R.menu.menus, menu);
            super.onCreateContextMenu(menu, v, menuInfo);
        }
    
        // 上下文菜单被点击是触发该方法
        @Override
        public boolean onContextItemSelected(MenuItem item) {
            switch(item.getItemId()){case R.id.one:newThread(){publicvoid run(){try{byte[] data =GetData.getImage(PIC_URL);
                                bitmap =BitmapFactory.decodeByteArray(data,0, data.length);}catch(Exception e){
                                e.printStackTrace();}
                            handler.sendEmptyMessage(0x001);};}.start();break;case R.id.two:newThread(){publicvoid run(){try{
                                detail =GetData.getHtml(HTML_URL);}catch(Exception e){
                                e.printStackTrace();}
                            handler.sendEmptyMessage(0x002);};}.start();break;case R.id.three:if(detail.equals("")){Toast.makeText(MainActivity.this,"先请求HTML先嘛~",Toast.LENGTH_SHORT).show();}else{
                        handler.sendEmptyMessage(0x003);}break;}returntrue;}}
  • 相关阅读:
    在Salesforce中创建Approval Process
    用C#基于WCF创建TCP的Service供Client端调用
    用 C# 实现一个简单的 Rest Service 供外部调用
    在Salesforce中将 Decimal 数据转换成美元格式
    在Asp.Net MVC中PartialView与EditorFor和DisplayFor的区别
    在Salesforce中对某一个Object添加自定义的Button和Link
    【LeetCode】227. Basic Calculator II
    【LeetCode】226. Invert Binary Tree
    【LeetCode】225. Implement Stack using Queues
    【LeetCode】224. Basic Calculator
  • 原文地址:https://www.cnblogs.com/chenghaixiang/p/14912007.html
Copyright © 2011-2022 走看看