REST和SOAP webservice在android中
发布时间:2020-12-16 21:37:51 所属栏目:安全 来源:网络整理
导读:我发现教程使用kSOAP api来使用SOAP webservice.任何人都可以提供我在 Android中获取REST webservice和SOAP webservice的示例程序(教程).我已经google了很多,但没有找到这样的教程. 解决方法 肥皂 优点: Langauge,platform,and transport agnostic Designed
我发现教程使用kSOAP api来使用SOAP webservice.任何人都可以提供我在
Android中获取REST webservice和SOAP webservice的示例程序(教程).我已经google了很多,但没有找到这样的教程.
解决方法
肥皂
优点:
缺点:
休息 优点:
缺点:
REST的示例 使用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示例 您可以自己使用kso??ap或创建soap xml并发送到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 + "n"); } } catch (IOException e) { e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 为什么shell base64编码结果与其他结果不同?
- supervisorctl unix ///tmp/supervisor.sock refused conne
- performance – Scalable包含针对SQL后端的LINQ方法
- Linux权限控制的基本原理
- angularjs – angular-ui:使用popover-template
- angularjs – Angular-XEditable e-onChange
- 一键设置windows java环境变量
- angularjs中的编译和链接函数之间的区别是什么
- angular6 flex-layout
- MVC开发模式以及Smarty模板引擎的使用