Java编写的接口测试工具

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

                       

这几天由于要频繁地使用一些天气数据接口,但是每次都要频繁的打开网页,略显繁琐,故就自己做了两个json数据获取的小工具。


  • 第一个

先来看看第一个吧,思路是使用一个网络流的处理,将返回的json字符串数据输出到屏幕上,代码如下:

package Simple;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;public class JsonTools extends JFrame{    /**     *      */    private static final long serialVersionUID = 1L;    JTextField tf=null;    JButton button=null,clear=null;    JTextArea ta=null;    public JsonTools(){        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("\t\t\tThis TextArea has been Cleared! \n\n\t\t\tYou can begin your next test!");                tf.grabFocus();            }        });    }    public static void main(String []args){        new JsonTools();    }    @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);        }    }}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133

下面看一下运行结果吧:
第一种方式的截图


  • 第二种
    第二种主要是调用了java的内核处理机制,这就省去了我们自定义方法处理网络流数据信息了。代码如下:
package WebBase;import java.awt.BorderLayout;import java.awt.Color;import java.awt.FlowLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.IOException;import javax.swing.JButton;import javax.swing.JEditorPane;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextField;public class JEditPaneToWebView extends JFrame{    /**     *      */    private static final long serialVersionUID = 1L;    JTextField tf=null;    JButton test=null,clear=null;    JEditorPane editorPane =null;    public JEditPaneToWebView(){        tf=new JTextField(28);        test=new JButton("submit");        clear=new JButton("Clear");        init();        test.addActionListener(new MyActionListener());        clear.addActionListener(new ActionListener(){            @Override            public void actionPerformed(ActionEvent e) {                // TODO Auto-generated method stub                tf.setText("");                editorPane.setText("<body align='center'>\t\t\tThis TextArea has been Cleared! <BT><BR><br>\t\t\tYou can begin your next test!</body>");                tf.grabFocus();            }        });    }    public static void main(String []args){        new JEditPaneToWebView();    }    @SuppressWarnings("deprecation")    public void init(){        this.setTitle("Json 字符串获取工具 WebView");        this.setSize(800,450);        this.setVisible(true);        this.setDefaultCloseOperation(EXIT_ON_CLOSE);        this.setLocationRelativeTo(null);        editorPane=new JEditorPane();        editorPane.setEditable(false);        editorPane.setBackground(Color.WHITE);        JScrollPane scrollPane = new JScrollPane(editorPane,                                 JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,                                 JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);        JPanel p=new JPanel();        p.setLayout(new FlowLayout());        p.add(new JLabel("URL:"));        p.add(tf);        p.add(test);        p.add(clear);        tf.setFocusable(true);        test.setNextFocusableComponent(tf);        this.add(p,BorderLayout.NORTH);        this.add(scrollPane,BorderLayout.CENTER);    }    class MyActionListener implements ActionListener{        @Override        public void actionPerformed(ActionEvent e) {            // TODO Auto-generated method stub//          http://www.weather.com.cn/data/cityinfo/101010100.html            editorPane.setContentType("text/html;charset=utf-8");            try {                editorPane.setPage(tf.getText().trim().toString());            } catch (IOException e1) {                // TODO Auto-generated catch block                e1.printStackTrace();            }        }    }}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95

下面来看一下程序运行的结果吧:
第二种方式截图
其实获取json数据只是其优点的一点点,下面的截图便可以证明JEditPane的强大
第二种方式截图


//你可以在下面的链接中找到这两款工具[下载地址](http://download.csdn.net/detail/marksinoberg/9288665),希望能帮到你。
  
  
  • 1

  • 总结:
    虽然做的这两个软件没什么大的用处,但是确实让我看到了许多组件的强大之处。这其实也是砸告诫我们,要多学习,多看API文档,才能熟能生巧。如果有哪里做得不好的话,还望广大博友不吝赐教,让我们一起进步吧!
           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow
这里写图片描述
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值