• JAVA------12.app 测试api接口


    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.ScrollPaneConstants;
    
    /**
     * 调用url 解析
     * @author caipei
     *
     */
    public class UrlUtil extends JFrame{
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        JTextField tf=null;
        JButton button=null,clear=null;
        JTextArea ta=null;
    
        public UrlUtil(){
            tf=new JTextField(28);
            button=new JButton("submit");
            clear=new JButton("Clear");
            ta=new JTextArea();
            init();
            button.addActionListener(new MyActionListener());
            clear.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    // TODO Auto-generated method stub
                    tf.setText("");
                    ta.setText("			This TextArea has been Cleared! 
    
    			You can begin your next test!");
                    tf.grabFocus();
                }
            });
        }
    
        public static void main(String []args){
            new UrlUtil();
        }
    
        @SuppressWarnings("deprecation")
        public void init(){
            this.setTitle("Json 字符串获取工具");
            this.setSize(800,450);
            this.setVisible(true);
            this.setDefaultCloseOperation(EXIT_ON_CLOSE);
            this.setLocationRelativeTo(null);
    
            JScrollPane js=new JScrollPane(ta);
            js.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
    
            JPanel p=new JPanel();
            p.setLayout(new FlowLayout());
            p.add(new JLabel("URL:"));
            p.add(tf);
            p.add(button);
            p.add(clear);
            tf.setFocusable(true);
            button.setNextFocusableComponent(tf);
    
            this.add(p,BorderLayout.NORTH);
            this.add(js,BorderLayout.CENTER);
        }
    
        public static String getJsonString(String url){
            String result="";
            URLConnection conn=null;
            BufferedReader reader=null;
            try{
                URL website=new URL(url.trim());
                conn=(URLConnection) website.openConnection();
                reader=new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));
    
                String line="";
                StringBuffer sb=new StringBuffer();
                while((line=reader.readLine())!=null){
                    sb.append(line);
                }
    
                result=sb.toString();
            }catch(MalformedURLException urlException){
                result+=urlException.getMessage();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                result+=e.getMessage();
                e.printStackTrace();
            }catch(Exception total){
                result+=total.getMessage();
                total.printStackTrace();
            }finally{
                if(conn!=null){
                    conn=null;
                }
                if(reader!=null){
                    try {
                        reader.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        result+=e.getMessage();
                        e.printStackTrace();
                    }
                    reader=null;
                }
            }
            return result;
        }
    
        class MyActionListener implements ActionListener{
    
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                String jsonString=getJsonString(tf.getText().trim().toString());
                ta.setText(jsonString);
            }
    
        }
    }

  • 相关阅读:
    java-集合框架-泛型2-泛型限定
    进程间通信
    多进程编程基础概念
    linux deb 打包流程
    linux RPM 打包流程
    Python 第一個程序
    从注册验证码入手,我保住了30%的流失用户
    为什么Web端登录需要验证码?
    网络验证码的进化:从简单图文到无感验证
    公开课 | 金融知识图谱的应用探索
  • 原文地址:https://www.cnblogs.com/coriander/p/6714922.html
Copyright © 2020-2023  润新知