android 中的 REST 和 SOAP 网络服务 [英] REST and SOAP webservice in android
本文介绍了android 中的 REST 和 SOAP 网络服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我找到了使用 kSOAP api 使用 SOAP 网络服务的教程.谁能为我提供有关在 android 中获取 REST webservice 和 SOAP webservice 的示例程序(教程).我用谷歌搜索了很多,但没有找到这种类型的教程.
I found tutorial to use kSOAP api to use SOAP webservice. Can anyone provide me sample programs (tutorial) on getting REST webservice and SOAP webservice in android. I have googled lot but didn't find such type of tutorial.
推荐答案
SOAP
优点:
- 与语言、平台和运输无关
- 专为处理分布式计算环境而设计
- 是 Web 服务的流行标准,因此得到其他标准(WSDL、WS-*)和供应商工具的更好支持
- 内置错误处理(故障)
- 可扩展性
缺点:
- 概念上比 REST 更难,更重量级"
- 更详细
- 开发难度大,需要工具
休息
优点:
- 与语言和平台无关
- 开发比 SOAP 简单得多
- 学习曲线小,对工具的依赖更少
- 简洁,无需额外的消息传递层
- 在设计和理念上更接近网络
缺点:
- 假设一种点对点通信模型——不适用于消息可能通过一个或多个的分布式计算环境更多中介
- 缺乏对安全、策略、可靠消息传递等的标准支持,因此具有更复杂要求的服务更加困难开发(自己动手")
- 绑定到 HTTP 传输模型
REST 示例
使用apache http jar
Example of REST
Use apache http jar
public void callRestWebService(){
System.out.println(".....REST..........");
HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet(wsdlURL);
request.addHeader("company name", "abc");
request.addHeader("getAccessNumbers","http://service.xyz.com/");
ResponseHandler<String> handler = new BasicResponseHandler();
try {
result = httpclient.execute(request, handler);
System.out.println("..result..."+result);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
httpclient.getConnectionManager().shutdown();
} // end callWebService()
}
SOAP 示例
你可以使用ksoap或者自己创建soap xml并发送到url
Example of SOAP
You can use either ksoap or create soap xml by yourself and send to url
private boolean callSOAPWebService() {
OutputStream out = null;
int respCode = -1;
boolean isSuccess = false;
URL url = null;
HttpsURLConnection httpURLConnection = null;
try {
url = new URL(wsdlURL);
httpURLConnection = (HttpsURLConnection) url.openConnection();
do {
// httpURLConnection.setHostnameVerifier(DO_NOT_VERIFY);
httpURLConnection.setRequestMethod("POST");
httpURLConnection
.setRequestProperty("Connection", "keep-alive");
httpURLConnection
.setRequestProperty("Content-Type", "text/xml");
httpURLConnection.setRequestProperty("SendChunked", "True");
httpURLConnection.setRequestProperty("UseCookieContainer",
"True");
HttpURLConnection.setFollowRedirects(false);
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setUseCaches(true);
httpURLConnection.setRequestProperty("Content-length",
getReqData().length + "");
httpURLConnection.setReadTimeout(10 * 1000);
// httpURLConnection.setConnectTimeout(10 * 1000);
httpURLConnection.connect();
out = httpURLConnection.getOutputStream();
if (out != null) {
out.write(getReqData());
out.flush();
}
if (httpURLConnection != null) {
respCode = httpURLConnection.getResponseCode();
Log.e("respCode", ":" + respCode);
}
} while (respCode == -1);
// If it works fine
if (respCode == 200) {
try {
InputStream responce = httpURLConnection.getInputStream();
String str = convertStreamToString(responce);
System.out.println(".....data....." + new String(str));
// String str
// =Environment.getExternalStorageDirectory().getAbsolutePath()+"/sunilwebservice.txt";
// File f = new File(str);
//
// try{
// f.createNewFile();
// FileOutputStream fo = new FileOutputStream(f);
// fo.write(b);
// fo.close();
// }catch(Exception ex){
// ex.printStackTrace();
// }
} catch (Exception e1) {
e1.printStackTrace();
}
} else {
isSuccess = false;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
out = null;
}
if (httpURLConnection != null) {
httpURLConnection.disconnect();
httpURLConnection = null;
}
}
return isSuccess;
}
public static String createSoapHeader() {
String soapHeader = null;
soapHeader = "<?xml version="1.0" encoding="utf-8"?>"
+ "<soap:Envelope "
+ "xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/""
+ " xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance""
+ " xmlns:xsd="http://www.w3.org/2001/XMLSchema"" + ">";
return soapHeader;
}
public static byte[] getReqData() {
StringBuilder requestData = new StringBuilder();
requestData.append(createSoapHeader());
requestData
.append("<soap:Body>"
+ "<getAccessNumbers"
+ " xmlns="http://service.xyz.com.com/""
+ "</getAccessNumbers> </soap:Body> </soap:Envelope>");
return requestData.toString().trim().getBytes();
}
private static String convertStreamToString(InputStream is)
throws UnsupportedEncodingException {
BufferedReader reader = new BufferedReader(new InputStreamReader(is,
"UTF-8"));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "
");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
这篇关于android 中的 REST 和 SOAP 网络服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文