1. 程式人生 > >Java編寫的接口測試工具

Java編寫的接口測試工具

html detail awt class ride json數據 bmi stub 網頁

這幾天由於要頻繁地使用一些天氣數據接口,但是每次都要頻繁的打開網頁,略顯繁瑣,故就自己做了兩個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文檔,才能熟能生巧。如果有哪裏做得不好的話,還望廣大博友不吝賜教,讓我們一起進步吧!

再分享一下我老師大神的人工智能教程吧。零基礎!通俗易懂!風趣幽默!還帶黃段子!希望你也加入到我們人工智能的隊伍中來!https://blog.csdn.net/jiangjunshow

Java編寫的接口測試工具