Middleware Practice IV Web Services Development

An internship purpose

Use Axis 2 to develop Web service, master the basic concepts such as Web service, WSDL, SOAP and the main modules of Web service development.

Two internship requirements

  1. Develop a desktop application using Java Swing or SWT
  2. 3 or more web services provided by the following websites are integrated into the application

http://www.36wu.com/Service.aspx

http://www.webxml.com.cn/zh_cn/web_services.aspx 

The web services that can be integrated are:

  1. Chinese and English bidirectional translation
  2. Mobile phone number attribution query
  3. IP address attribution
  4. city ​​weather forecast
  5. stock quotes
  6. QQ number online status query
  7. Domestic flight schedule query
  8. Train timetable
  9. Foreign exchange-RMB exchange quotation
  10. Chinese TV Shows
  11. Chinese Simplified--Traditional Chinese Character Conversion
  12. China Postal Code--Two-way Query of Address Information
  13. Verification code -- verification code picture
  14. Express inquiry
  15. ID card query
  16. Bus route query
  17. ISBN query
  18. ICP filing query
  1. The interface controls are arranged reasonably and beautifully; the integrated Web service is used as the main business function of the application for users to use.
  2. Package the application and required library files to generate an executable jar file, and double-click the generated jar file to open the application program interface; the program provides correct business functions.

Three internship process

  1. Environment configuration

Install the Web Service plugin

 Install Eclipse-jee; download the latest version of Axis2, Axis2: 

Apache Downloads , select the .zip package of Standard Binary Distribution , that is, the file "axis2-1.4.1-bin.zip" , extract the directory name axis2-1.4.1 , the file structure in the directory is as follows

In the menu bar of Eclipse, Window --> Preferences --> Web Service --> Axis2-->Perferences, select the location of the Axis2 decompression package in the Axis2 runtime location. After setting, click "OK", as follows picture.

2) Integrate Web services and develop clients (take the integration of postal code-address information as an example)

(1) Download WSDL, and download the WSDL file of the Web service that needs to be integrated to the machine

(2) Create an empty Java project, such as MyApplication.

(3) Right-click on "MyApplication", select New--Other, and enter the following wizard interface

Select Web Services client, click next to enter the following wizard interface

In the wizard interface, enter the full network path where the WSDL is located in the Service definition field. Drag the ruler under Client type to Develop client. Click Finish, the wizard can generate the client proxy code. As shown below:

(4) Repeat steps (1) and (3) to generate client proxy code for multiple web services into your application.

(5) Use the class with Proxy in the class name, such as using the class ChinaZipSearchWebServiceSoapProxy in the figure above, to generate an object, and through the object, you can call the business functions provided by the corresponding Web service.

(6) Use Swing or SWT to design the interface, there are text input boxes, query buttons, etc. in the interface for users to input and query. During the query process, call the client proxy class of the Web service to generate objects and access the Web Service. Return the result to the user in a reasonable manner.

(7) Complex (object, custom type, complex type) return result processing, the WebService provided in this internship, when the server defines WSDL and deployment information, does not give enough description information for the returned complex type data, so When the client generates the proxy code, it fails to generate a reasonably detailed class corresponding to returning complex type data. When the server returns complex type data, it just stores the data in an xml file and returns the xml file to the user. We need to parse the xml document to get the details. To parse xml, you can refer to DOM tree parsing in Javascript. Like HTML, XML can construct a DOM tree for parsing.

Take the postal code-address information as an example, the basic process is as follows:

  1. Call the getAddressByZipCode () method of the ChinaZipSearchWebServiceSoapProxy class to obtain the address information according to the zip code. In this example, the address information corresponding to the zip code " 712100 " is obtained.
  2. The return result type is GetAddressByZipCodeResponseGetAddressByZipCodeResult . Call the get_any() method of this class to get an array, which stores the content of the XML file returned by the server . For details, see the attached result.xml .
  3. Get the xml file content from the array and parse it using the DOM API. For the detailed process, see the attached Test.java.

Four experimental codes and results display

1. Chinese and English two-way translation service

public class Test{
	private JFrame frame;
	private JTextField textField;
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Test window = new Test();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	public Test() {
		initialize();
	}
	public void initialize() {
		frame = new JFrame("中英双向翻译");
		frame.setBounds(100, 100, 632, 320);
		frame.setResizable(false);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JPanel panel = new JPanel();
		frame.getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		JLabel lblNewLabel = new JLabel("请输入单词");
		lblNewLabel.setFont(new Font("幼圆", Font.PLAIN, 17));
		lblNewLabel.setBounds(41, 11, 198, 31);
		lblNewLabel.setHorizontalAlignment(SwingConstants.LEFT);
		panel.add(lblNewLabel);
		
		textField = new JTextField();
		textField.setFont(new Font("宋体", Font.PLAIN, 17));
		textField.setBounds(268, 14, 146, 24);
		panel.add(textField);
		textField.setColumns(10);
		final JTextArea textArea = new JTextArea();
		textArea.setFont(new Font("Monospaced", Font.BOLD, 18));
		
		textArea.setBounds(0, 42, 626, 245);
		textArea.setEditable(false);
		panel.add(textArea);
		JButton btnNewButton = new JButton("翻译");
		btnNewButton.setFont(new Font("幼圆", Font.PLAIN, 17));
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		btnNewButton.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				EnglishChineseSoapProxy proxy=new EnglishChineseSoapProxy();
				String[] results;
				StringBuffer sb = new StringBuffer();
				String str=null;
                try {
					results =proxy.translatorString(textField.getText());
					for(int i = 0; i < 2*results.length/5; i++){
					     sb. append(results[i]+";");
					 }
				     for(int i = 2*results.length/5; i < results.length; i++){
				         sb. append(results[i]+"\n");
				     }
				     sb.deleteCharAt(sb.length() - 1); 
				     str =sb.toString();
				} catch (RemoteException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				textArea.setText(str);
			}
		});
		btnNewButton.setBounds(453, 13, 122, 27);
		panel.add(btnNewButton);
	}
}

The running result is shown in the figure:

2. China Postal Code -- Two-way query of address information

public class Test {
	private JFrame frame;
	private JTextField textField;
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Test window = new Test();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	public Test() {
		initialize();
	}
	public void initialize() {
		frame = new JFrame("中国邮政编码地址信息双向查询");
		frame.setBounds(100, 100, 1098, 594);
		frame.setResizable(false);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JPanel panel = new JPanel();
		frame.getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		JLabel lblNewLabel = new JLabel("请输入邮政编码");
		lblNewLabel.setFont(new Font("幼圆", Font.PLAIN, 17));
		lblNewLabel.setBounds(260, 11, 198, 31);
		lblNewLabel.setHorizontalAlignment(SwingConstants.LEFT);
		panel.add(lblNewLabel);
		textField = new JTextField();
		textField.setFont(new Font("宋体", Font.PLAIN, 17));
		textField.setBounds(472, 14, 121, 24);
		panel.add(textField);
		textField.setColumns(10);
		
		final JTextArea textArea = new JTextArea();
		textArea.setFont(new Font("新宋体", Font.PLAIN, 16));
		textArea.setBounds(0, 42, 1092, 517);
		textArea.setEditable(false);
		panel.add(textArea);
		JButton btnNewButton = new JButton("地址信息");
		btnNewButton.setFont(new Font("幼圆", Font.PLAIN, 17));
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		btnNewButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
ChinaZipSearchWebServiceSoapProxy locator=new ChinaZipSearchWebServiceSoapProxy();
				String str = null;
				StringBuffer sb = new StringBuffer();
				try {
					GetAddressByZipCodeResponseGetAddressByZipCodeResult result = locator.getAddressByZipCode(textField.getText(),"");
					MessageElement elements[]=result.get_any();
					NodeList list=elements[1].getElementsByTagName("ZipInfo");
					for(int i=0;i<list.getLength();i++){
						Node node =(Node) list.item(i);
						NodeList children =node.getChildNodes();
						for(int j=0;j<children.getLength();j++)
						{
							sb. append(children.item(j).getFirstChild()+"\n"); 
							str =sb.toString()+"\n";
						}
					}
				} catch (RemoteException e1) {
					e1.printStackTrace();
				}
				textArea.setText(str);
			}
		});
		btnNewButton.setBounds(630, 12, 113, 27);
		panel.add(btnNewButton);
	}
}

The running result is shown in the figure:

3. City weather forecast service

public class Test1{
	private JFrame frame;
	private JTextField textField;
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Test1 window = new Test1();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	public Test1() {
		initialize();
	}
	public void initialize() {
		frame = new JFrame("城市天气预报");
		frame.setBounds(100, 100, 632, 320);
		frame.setResizable(false);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JPanel panel = new JPanel();
		frame.getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("请输入所查城市");
		lblNewLabel.setFont(new Font("幼圆", Font.PLAIN, 17));
		lblNewLabel.setBounds(41, 11, 198, 31);
		lblNewLabel.setHorizontalAlignment(SwingConstants.LEFT);
		panel.add(lblNewLabel);
		
		textField = new JTextField();
		textField.setFont(new Font("宋体", Font.PLAIN, 17));
		textField.setBounds(268, 14, 146, 24);
		panel.add(textField);
		textField.setColumns(10);
		final JTextArea textArea = new JTextArea();
		textArea.setFont(new Font("Monospaced", Font.BOLD, 18));
		textArea.setBounds(0, 42, 626, 245);
		textArea.setEditable(false);
		panel.add(textArea);
		JButton btnNewButton = new JButton("查询");
		btnNewButton.setFont(new Font("幼圆", Font.PLAIN, 17));
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		btnNewButton.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
//核心代码如下:
String[] result;
				StringBuffer sb = new StringBuffer();
				String str=null;
				WeatherWSLocator weather =new WeatherWSLocator();
		        WeatherWSSoapStub stub;
				try {
			stub = (WeatherWSSoapStub) weather.getPort(WeatherWSSoapStub.class);
					result =stub.getWeather("西安",null);
					 for(int i = 0; i < 2*result.length/5; i++){
					     sb. append(result[i]+";");
					 }
				     for(int i = 2*result.length/5; i < result.length; i++){
				         sb. append(result[i]+"\n");
				     }
				     sb.deleteCharAt(sb.length() - 1); 
				     str =sb.toString();
					textArea.setText(str);
				} catch (ServiceException e1) {
					e1.printStackTrace();
				} catch (RemoteException e1) {
					e1.printStackTrace();
				}     
			}
		});
		btnNewButton.setBounds(453, 13, 122, 27);
		panel.add(btnNewButton);
	}
}

The running result is shown in the figure:

 

4. QQ number online status query

public class CheckQQOnlineClient{
	private JFrame frame;
	private JTextField textField;
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CheckQQOnlineClient window = new CheckQQOnlineClient();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	public CheckQQOnlineClient() {
		initialize();
	}
	public void initialize() {
		frame = new JFrame("QQ在线状态查询");
		frame.setBounds(100, 100, 632, 320);
		frame.setResizable(false);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel panel = new JPanel();
		frame.getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("请输入QQ号");
		lblNewLabel.setFont(new Font("幼圆", Font.PLAIN, 17));
		lblNewLabel.setBounds(41, 11, 198, 31);
		lblNewLabel.setHorizontalAlignment(SwingConstants.LEFT);
		panel.add(lblNewLabel);
		
		textField = new JTextField();
		textField.setFont(new Font("宋体", Font.PLAIN, 17));
		textField.setBounds(268, 14, 146, 24);
		panel.add(textField);
		textField.setColumns(10);
		
		final JTextArea textArea = new JTextArea();
		textArea.setFont(new Font("Monospaced", Font.BOLD, 18));
		textArea.setBounds(0, 42, 626, 245);
		textArea.setEditable(false);
		panel.add(textArea);
		
		JButton btnNewButton = new JButton("查询");
		btnNewButton.setFont(new Font("幼圆", Font.PLAIN, 17));
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		btnNewButton.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
//此处为核心代码:
QqOnlineWebServiceLocator qqOnlineWS=new QqOnlineWebServiceLocator();
	        	QqOnlineWebServiceSoap qqOnlineWSSoap;
	        	String results;
				try {
					qqOnlineWSSoap = qqOnlineWS.getqqOnlineWebServiceSoap();
					results =qqOnlineWSSoap.qqCheckOnline(textField.getText());
					// String,Y = 在线;N = 离线;E = QQ号码错误;A = 商业用户验证失败;V = 免费用户超过数量
	                   if ("Y".equalsIgnoreCase(results)) {
	                	   results = "在线";
	                   } else if ("N".equalsIgnoreCase(results)) {
	                	   results = "离线";
	                   } else if ("E".equalsIgnoreCase(results)) {
	                	   results = "QQ号码错误";
	                   } else if ("A".equalsIgnoreCase(results)) {
	                	   results = "商业用户验证失败";
	                   } else if ("V".equalsIgnoreCase(results)) {
	                	   results = "免费用户超过数量";
	                   }
	       			textArea.setText(results);
				} catch (ServiceException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				} catch (RemoteException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}			
			}
		});
		btnNewButton.setBounds(453, 13, 122, 27);
		panel.add(btnNewButton);
	}
}

The running result is shown in the figure:

 5. Mobile phone number attribution query

package com.devins.ws.phone;
import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
import cn.com.WebXml.MobileCodeWSLocator;

public class PhoneBelongQuery {
	public static void main(String[] args) throws ServiceException, RemoteException {
		MobileCodeWSLocator factory = new MobileCodeWSLocator();
cn.com.WebXml.MobileCodeWSSoap mobileCodeWSSoap = factory.getMobileCodeWSSoap();
		String result = mobileCodeWSSoap.getMobileCodeInfo("18202969482",null);
		System.out.println(result);
	}
}

The running result is shown in the figure:

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324481661&siteId=291194637