现在的位置: 首页 > 综合 > 正文

WebService Soap协议详解

2014年02月06日 ⁄ 综合 ⁄ 共 5606字 ⁄ 字号 评论关闭

首先让我们打开http://www.webxml.com.cn/webservices/weatherwebservice.asmx,这是一个天气相关的服务,再打开getSupportCity方法。进入该页面之后,我们可以看到





分别是Soap1.1,soap1.2以及get和post请求。其中Soap1.1和Soap1.2都采用了post请求,只是采用了不同的头,重点不同点已划出。那我就以这四种方式分别请求,看是否可以得到一致的结果。(时间有限,没有做封装了的soap,如果需要可以参考http://www.cnblogs.com/ghj1976/archive/2011/04/26/2028904.html)

上代码

package com.example.webservicetest;


import java.net.URLEncoder;

import android.os.Bundle;
import android.app.Activity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener{
	private static String URL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx";
	private TextView result_txt;
	private Button get_btn,post_btn,saop11_btn,
				   saop12_btn,saop11_post_btn,saop12_post_btn;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		findViews();
		bindViews();
	}

	private void findViews(){
		result_txt = (TextView)findViewById(R.id.result_txt);
		get_btn = (Button)findViewById(R.id.get_btn);
		post_btn = (Button)findViewById(R.id.post_btn);
		saop11_btn = (Button)findViewById(R.id.saop11_btn);
		saop12_btn = (Button)findViewById(R.id.saop12_btn);
		saop11_post_btn = (Button)findViewById(R.id.saop11_post_btn);
		saop12_post_btn = (Button)findViewById(R.id.saop12_post_btn);
	}
	
	private void bindViews(){
		get_btn.setOnClickListener(this);
		post_btn.setOnClickListener(this);
		saop11_btn.setOnClickListener(this);
		saop12_btn.setOnClickListener(this);
		saop11_post_btn.setOnClickListener(this);
		saop12_post_btn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		HttpUtil httpUtil = new HttpUtil();
		String result = null;
		switch (v.getId()) {
		
		case R.id.get_btn:
			result = get(httpUtil);
			break;
		case R.id.post_btn:
			result = post(httpUtil);
			break;
		case R.id.saop11_btn:
			
			break;
		case R.id.saop12_btn:
			
			break;
		case R.id.saop11_post_btn:
			result = soap11Post(httpUtil);
			break;
		case R.id.saop12_post_btn:
			result = soap12Post(httpUtil);
			break;

		default:
			break;
		}
		Log.i("tag","error:"+ httpUtil.getError());
		if(TextUtils.isEmpty(httpUtil.getError())){
			result_txt.setText(result);
		}
	}
	private String get(HttpUtil httpUtil){
		String result = null;
		try {
			String url = URL+"/getSupportCity?byProvinceName=";
			//注意中文字码要编码,否则报400错误
			httpUtil.setGETConn(url+URLEncoder.encode("广东"));
			result = httpUtil.getString();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	private String post(HttpUtil httpUtil){
		String result = null;
		try {
			httpUtil.setPOSTConn(URL+"/getSupportCity");
			result = httpUtil.postString("byProvinceName=广东");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	private String soap11Post(HttpUtil httpUtil){
		String result = null;
		try {
			httpUtil.setSoap11Post(URL,"http://WebXml.com.cn/getSupportCity");
			String data = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" +
" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"+
  "<soap:Body>" +
  "<getSupportCity xmlns=\"http://WebXml.com.cn/\">"+
      "<byProvinceName>广东</byProvinceName>"+
    "</getSupportCity>"+
  "</soap:Body>"+
"</soap:Envelope>";
			Log.i("tag", data);
result = httpUtil.postString(data);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	private String soap12Post(HttpUtil httpUtil){
		String result = null;
		try {
			httpUtil.setSoap12Post(URL);
			String data = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
"<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" +
" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">"+
  "<soap12:Body>" +
  "<getSupportCity xmlns=\"http://WebXml.com.cn/\">"+
      "<byProvinceName>广东</byProvinceName>"+
    "</getSupportCity>"+
  "</soap12:Body>"+
"</soap12:Envelope>";
			Log.i("tag", data);
result = httpUtil.postString(data);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
}
布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <Button android:id="@+id/get_btn"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:text="get方式"
	   />
	<Button android:id="@+id/post_btn"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:text="post方式"
	    android:layout_toRightOf="@id/get_btn"
	    android:layout_marginLeft="5dp"/>
	<Button android:id="@+id/saop11_btn"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:text="soap1.1框架"
	    android:layout_below="@id/get_btn"
	    android:layout_marginTop="5dp"/>
	<Button android:id="@+id/saop12_btn"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:text="soap1.2框架"
	    android:layout_toRightOf="@id/saop11_btn"
	    android:layout_alignTop="@id/saop11_btn"
	    android:layout_marginLeft="5dp"/>
	<Button android:id="@+id/saop11_post_btn"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:text="soap1.1原生post"
	    android:layout_below="@id/saop11_btn"
	    android:layout_marginTop="5dp"/>
	<Button android:id="@+id/saop12_post_btn"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:text="soap1.2原生post"
	    android:layout_toRightOf="@id/saop11_post_btn"
	     android:layout_alignTop="@id/saop11_post_btn"
	    android:layout_marginLeft="5dp"/>
    <TextView  android:id="@+id/result_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" 
        android:layout_below="@id/saop11_post_btn"
        android:layout_marginTop="5dp"/>

</RelativeLayout>

附上源码:http://download.csdn.net/detail/wxp1980/6006599


抱歉!评论已关闭.