这几天由于要频繁地使用一些天气数据接口,但是每次都要频繁的打开网页,略显繁琐,故就自己做了两个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);
}
}
}
下面看一下运行结果吧:
- 第二种
第二种主要是调用了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();
}
}
}
}
下面来看一下程序运行的结果吧:
其实获取json数据只是其优点的一点点,下面的截图便可以证明JEditPane的强大
//你可以在下面的链接中找到这两款工具[下载地址](http://download.csdn.net/detail/marksinoberg/9288665),希望能帮到你。
- 总结:
虽然做的这两个软件没什么大的用处,但是确实让我看到了许多组件的强大之处。这其实也是砸告诫我们,要多学习,多看API文档,才能熟能生巧。如果有哪里做得不好的话,还望广大博友不吝赐教,让我们一起进步吧!