通过HttpClient请求webService

通过HttpClient请求webService 

由于服务端是用webService开发的,android要调用webService服务获取数据,这里采用的是通过HttpClient发送post请求,获取webService数据。
 
服务端使用的webService框架是axis2,请求数据之前,要封装一个xml格式,再通过post请求,获取服务端数据。

请求的xml格式如下所示: 


<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"xmlns:sam="http://user.service.xxx.com">
	<soap:Header/>
	<soap:Body>
		<sam:getUserInfo>
			<sam:userName>sunlightcs</sam:userName>
		</sam:getUserInfo>
	</soap:Body>
</soap:Envelope>




其中:getUserInfo是方法名,userName是参数名,当然,还可以加多个参数。

如果需要SOAP Header需要添加审核信息,如用户名密码验证,可在<soap:Header/>节点添加数据:

<soap:Header>
	<username>user</username>
	<password>pwd</password>
</soap:Header>



下面的代码是向webService发送请求,获取数据,返回的数据是xml形式的,android只要解析xml数据,就可以获得想要的数据了。  

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
 
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentProducer;
import org.apache.http.entity.EntityTemplate;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
 
 
public class ClientTest {
	 
	public static void main(String[] args) {
		ClientTest.httpClientPost();
	}
	 
	private static void httpClientPost() {
		HttpClient client = new DefaultHttpClient();
		HttpPost post = newHttpPost("http://localhost:8080/xxx/services/userService");

		try {
			ContentProducer cp = new ContentProducer() {
				public void writeTo(OutputStream outstream) throwsIOException {
					Writer writer = new OutputStreamWriter(outstream,"UTF-8");
					 
					/**
					 * 获取请求的xml格式数据
					 */
					String requestXml = getRequestXml();
					writer.write(requestXml);
					writer.flush();
				}
			};
 
			post.setEntity(new EntityTemplate(cp));
			HttpResponse response = client.execute(post);
			 
		//打印返回的xml数据
			System.out.println(EntityUtils.toString(response.getEntity()));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	 
	 
	private static String getRequestXml(){
		StringBuilder sb = new StringBuilder("<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:sam=\"http://user.service.xxx.com\">");
		sb.append("<soap:Header/>");
		sb.append("<soap:Body>");
		sb.append("<sam:getUserInfo>");
		sb.append("<sam:userName>sunlightcs</sam:userName>");
		sb.append("</sam:getUserInfo>");
		sb.append("</soap:Body>");
		sb.append("</soap:Envelope>");
		 
		return sb.toString();
	}
	 
}

返回的数据格式如下:  

<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
    <soapenv:Body>
        <ns:getUserInfoResponse xmlns:ns="http://user.service.xxx.com">
            <ns:return>xxx</ns:return>
        </ns:getUserInfoResponse>
    </soapenv:Body>
</soapenv:Envelope>



其中,<ns:return>内的"xxx"可以是json数据,android只需解析标签<ns:return>里的json数据即可。 
转载 http://www.juziku.com/wiki/3919.htm


你可能感兴趣的:(通过HttpClient请求webService)