`

根据List生成XML利用HttpClient发送给MPC

阅读更多

此程序是根据一个List,然后生成一个XML的文件,然后再使用HttpClient发送给相应的URL进行处理(这里的XML的文件可以保存在自己的项目的一个文件中)
@Override
	public void locationEvent(LocationEvent locationEvent) {
		// TODO Auto-generated method stub
		List list = ls.getListLocation();
		// 在这里就可以调用一个函数去生成一个XML的文件,然后再发送一个HttpClient的请求给MPC
		this.creatXML(list);
		//生成XML之后就发送一个Http请求,通过HttpClient
		this.sendPostMethod("http://192.168.0.89:2000/LcsInterface");
	}

	private void creatXML(List list) {
		Element root, e1, e11, e12, e2, e21, e22, e3, e31, e32, e33, e331, e332, e333, e34, e35, e351, e352, e353, e36, e361, e362, e363, e364;
		Document Doc;
		root = new Element("REQ"); // 首先建立根元素
		DocType type = new DocType("REQ", "LOCREQ.DTD"); // 文档类型
		Doc = new Document(root, type); // 然后用root创建XML文档对象

		e1 = new Element("CLIENT"); // 创建元素e1,设置内容,设置
		e11 = new Element("LCSCLIENTID");   // 定义子元素
		e11.setText("333");
		e12 = new Element("PASSWORD");      // 定义子元素
		e12.setText("0555");
		e1.addContent(e11); // 把子元素添加到元素e1中
		e1.addContent(e12);
		root.addContent(e1);
		
		e2 = new Element("ORIGINATOR");
		e21 = new Element("ORID");
		String mobile = (String)list.get(0);
		e21.setText(mobile);
		e22 = new Element("ORID_TYPE");
		e22.setText("0");
		e2.addContent(e21);
		e2.addContent(e22);
		root.addContent(e2);
		
		e3 = new Element("LIR");
		e31 = new Element("ORIGUSER_ACCESSTYPE");
		e31.setText("1");
		e3.addContent(e31);
		e32 = new Element("FINDME_INDIC");
		e32.setText("1");
		e3.addContent(e32);
		e33 = new Element("MSIDS");
		e331 = new Element("MSID");
		e331.setText(mobile);
		e33.addContent(e331);
		e332 = new Element("MSID_TYPE");
		e332.setText("0");
		e33.addContent(e332);
		e333 = new Element("QUERYPASSWORD");
		e333.setText("888888");
		e33.addContent(e333);
		e3.addContent(e33);
		e34 = new Element("POSREQTYPE");
		e34.setText("1");
		e3.addContent(e34);
		
		e35 = new Element("GEO_INFO");
		e351 = new Element("COORD_SYS");
		e351.setText("LL");
		e35.addContent(e351);
		e352 = new Element("DATUM");
		e352.setText("WGS-84");
		e35.addContent(e352);
		e353 = new Element("LL_FORMAT");
		e353.setText("DMS3");
		e35.addContent(e353);
		e3.addContent(e35);
		
		e36 = new Element("PQOS");
		e361 = new Element("RESP_REQ");
		e361.setText("0");
		e36.addContent(e361);
		e362 = new Element("RESP_TIMER");
		e362.setText("0010");
		e36.addContent(e362);
		e363 = new Element("HOR_ACC");
		e363.setText("200");
		e36.addContent(e363);
		e364 = new Element("ALT_ACC");
		e364.setText("200");
		e36.addContent(e364);
		e3.addContent(e36);
		
		root.addContent(e3);

		XMLOutputter XMLOut = new XMLOutputter();
		Format format = Format.getCompactFormat(); // 得到一个定义xml格式的format
		format.setIndent("  "); // 在元素后换行,每一层元素缩排两格
		XMLOut.setFormat(format); // 设定格式
		
		try {
			XMLOut.output(Doc, new FileOutputStream("c:/test3.xml"));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} // 输出
	}
	
	/**
	 * 生成XML之后就通过HttpClient发送一个请求到MPC
	 */
	private void sendPostMethod(String url){
		HttpClient client = new HttpClient();
		HttpMethod method = getPostMethod(url);
		try {
			client.executeMethod(method);
			//打印服务器返回的状态   
			System.out.println(method.getStatusLine());
			//打印结果页面   
	        //String response = new String(method.getResponseBodyAsString().getBytes("8859_1"));   
	       //打印返回的信息   
	        //System.out.println(response);   
		} catch (HttpException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			method.releaseConnection();
		}
	}
	
	/**
	 * 使用post方式提交数据
	 */
	private static HttpMethod getPostMethod(String url){
		File input = new File("c:/test3.xml");
		System.out.println(url);
		PostMethod post = new PostMethod(url);
		//NameValuePair mobile = new NameValuePair("mobile", "13728852648");
		//post.setRequestBody(new NameValuePair[]{mobile});
		try {
			post.setRequestBody(new FileInputStream(input));
			 post.setRequestHeader("Content-type", "text/xml; charset=UTF-8");   
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return post;
	}
 上面的那个方法locationEvent是一个自定义监听器的一个实现方法,当list一变化的时候就会自动调用locationEvent的方法

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics