基本信息
源码名称:android调用 webservice的两种方法 示例源码下载
源码大小:0.42M
文件格式:.rar
开发语言:Java
更新时间:2013-10-23
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
package com.jj.logic; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.CoreConnectionPNames; import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE; import org.xmlpull.v1.XmlPullParserException; import com.jj.weather.R.string; import android.net.Uri; import android.util.Log; public class WebserviceHelper2 { private static final String tag = "jj"; // webservice 命名空间 private static final String NAMESPACE = "http://WebXml.com.cn/"; // WebService地址 private static String URL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx"; // webservice 调用方法 private static final String METHOD_NAME = "getWeatherbyCityName"; private static final int TimeoutBySecond = 1; // webservice 的SOAP_ACTION private static String SOAP_ACTION = "http://WebXml.com.cn/getWeatherbyCityName"; public static SoapObject soapObject; /*** * 拼接请求webservice * * @return */ private static StringEntity getEntity(String thecity) { StringEntity entity = null; String str = String .format("<?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>" "<getWeatherbyCityName xmlns=\"http://WebXml.com.cn/\">" "<theCityName>%1$s</theCityName>" "</getWeatherbyCityName>" "</soap:Body>" "</soap:Envelope>", thecity); try { entity = new StringEntity(str); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return entity; } /*** * webservice 请求 */ public static HttpPost httpPost(StringEntity entity, String SOAP_ACTION) { HttpPost request = new HttpPost(URL); request.addHeader("SOAPAction", SOAP_ACTION); request.addHeader("Host","www.webxml.com.cn"); request.addHeader("Content-Type", "text/xml; charset=utf-8"); request.setEntity(entity); return request; } /*** * 获取响应webservice HttpResponse */ public static HttpResponse getHttpResponse(HttpPost httpPost) { HttpClient client = new DefaultHttpClient(); client.getParams() .setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, TimeoutBySecond * 1000); HttpResponse httpResponse = null; try { httpResponse = client.execute(httpPost); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return httpResponse; } /*** * 获取内容 解析httpresponse */ public static String getContent(HttpResponse httpResponse) { InputStream inputStream; BufferedReader in = null; StringBuffer sb = new StringBuffer(); try { inputStream = httpResponse.getEntity().getContent(); in = new BufferedReader(new InputStreamReader(inputStream, "utf-8")); String inputLine; while ((inputLine = in.readLine()) != null) { sb.append(inputLine); sb.append("\n");// 换行 } in.close(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return sb.toString(); } public static String getWeather(String cityName) { StringEntity entity = getEntity(cityName); HttpPost httpPost = httpPost(entity, SOAP_ACTION); HttpResponse httpResponse = getHttpResponse(httpPost); String weather = getContent(httpResponse); Log.v(tag, weather); return weather; } }