Android调用webservice(线程实现)
2016-05-10 11:17
549 查看
本文是在Android环境下调用c#写的webservice,由于楼主也是入门初学者,所以过程一波三折 ,由于android4.0后不允许在主线连接网络,直接在主线程中写运行会出错,主线程不能直接访问WebService,需要新建一个线程访问WebService,然后把消息传回更改TextView。所以楼主写一下自己学习他人代码,然后利用线程实现的过程
第一步:新建一个安卓项目,导入webservice所需要的jar包ksoap2-android-assembly-2.4-jar-with-dependencies.jar
第二步:编写布局文件activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > <TextView android:id="@+id/result" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>第三步:编写WebServiceUtils
public class WebServiceUtils { public static final String WEB_SERVER_URL = "http://192.168.1.100:80/WebService1.asmx"; // 含有3个线程的线程池 private static final ExecutorService executorService = Executors .newFixedThreadPool(1); // 命名空间 private static final String NAMESPACE = "http://tempuri.org/"; /** * * @param url * WebService服务器地址 * @param methodName * WebService的调用方法名 * @param properties * WebService的参数 * @param webServiceCallBack * 回调接口 */ public static void callWebService(String url, final String methodName, final WebServiceCallBack webServiceCallBack) { // 创建HttpTransportSE对象,传递WebService服务器地址 final HttpTransportSE httpTransportSE = new HttpTransportSE(url); // 创建SoapObject对象 SoapObject soapObject = new SoapObject(NAMESPACE, methodName); // 实例化SoapSerializationEnvelope,传入WebService的SOAP协议的版本号 final SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope( SoapEnvelope.VER12); // 设置是否调用的是.Net开发的WebService soapEnvelope.setOutputSoapObject(soapObject); soapEnvelope.dotNet = true; httpTransportSE.debug = true; // 用于子线程与主线程通信的Handler final Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); // 将返回值回调到callBack的参数中 webServiceCallBack.callBack((SoapObject) msg.obj); } }; // 开启线程去访问WebService executorService.submit(new Runnable() { @Override public void run() { SoapObject resultSoapObject = null; try { httpTransportSE.call(NAMESPACE + methodName, soapEnvelope); System.out.println("soap envelop"+soapEnvelope.getResponse()); if (soapEnvelope.getResponse() != null) { // 获取服务器响应返回的SoapObject resultSoapObject = (SoapObject) soapEnvelope.bodyIn; } } catch (HttpResponseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (XmlPullParserException e) { e.printStackTrace(); } finally { // 将获取的消息利用Handler发送到主线程 mHandler.sendMessage(mHandler.obtainMessage(0, resultSoapObject)); } } }); } public interface WebServiceCallBack { public void callBack(SoapObject result); } }第四步:编写ProgressDialogUtils
public class ProgressDialogUtils { private static ProgressDialog mProgressDialog; /** * 显示ProgressDialog * @param context * @param message */ public static void showProgressDialog(Context context, CharSequence message){ if(mProgressDialog == null){ mProgressDialog = ProgressDialog.show(context, "", message); }else{ mProgressDialog.show(); } } /** * 关闭ProgressDialog */ public static void dismissProgressDialog(){ if(mProgressDialog != null){ mProgressDialog.dismiss(); mProgressDialog = null; } } }第五步:在配置文件AndroidManifest.xml中加入 <uses-permission android:name="android.permission.INTERNET"/>入网许可 最后运行结果
相关文章推荐
- 使用Android应用调用WebService实现天气预报
- android之调用webservice 实现图片上传下载
- android之调用webservice 实现图片上传下载
- android之调用webservice 实现图片上传
- android之调用webservice 实现图片上传
- android 调用webservice实现手机号码归属地查询
- Android基于SOAP标准调用Webservice实现数据交互
- android之调用webservice 实现图片上传
- android之调用webservice 实现图片上传
- Android调用Webservice实现手机号码归属地查询
- 简单的 Android 调用WebService 实现号码归属地查询
- Android调用WebService之服务端实现(一)
- Android通过调用Webservice实现天气预报
- android调用webservice实现图片上传
- android之调用webservice 实现图片上传下载
- android之调用webservice 实现图片上传下载
- android之调用webservice 实现图片上传下载
- Android调用webservice实现 手机号码归属地查询
- android使用ksoap2调用webservice接口实现电话号码查询功能
- android之调用webservice 实现图片上传