时间:2021-05-21
Android 调用WCF实例
1. 构建服务端程序
5. 在Eclipse中新建一个Java项目,测试你的服务
新建一个接口, 用于专门读取WCF返回的SoapObject对象
ISoapService
package junit.soap.wcf;import org.ksoap2.serialization.SoapObject;public interface ISoapService { SoapObject LoadResult();}HelloService
package junit.soap.wcf;import java.io.IOException;import org.ksoap2.SoapEnvelope;import org.ksoap2.serialization.SoapObject;import org.ksoap2.serialization.SoapSerializationEnvelope;import org.ksoap2.transport.HttpTransportSE;import org.xmlpull.v1.XmlPullParserException;public class HelloService implements ISoapService { private static final String NameSpace = "http:// = true; envelope.setOutputSoapObject(soapObject); HttpTransportSE trans = new HttpTransportSE(URL); trans.debug = true; // 使用调试功能 try { trans.call(SOAP_ACTION, envelope); System.out.println("Call Successful!"); } catch (IOException e) { System.out.println("IOException"); e.printStackTrace(); } catch (XmlPullParserException e) { System.out.println("XmlPullParserException"); e.printStackTrace(); } SoapObject result = (SoapObject) envelope.bodyIn; return result; }}测试程序
package junit.soap.wcf;import org.ksoap2.serialization.SoapObject;public class HelloWcfTest { public static void main(String[] args) { HelloService service = new HelloService("Master HaKu"); SoapObject result = service.LoadResult(); System.out.println("WCF返回的数据是:" + result.getProperty(0)); }}经过测试成功
运行结果:
Hello Master HaKu
6. Android客户端测试
package david.android.wcf;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;import org.ksoap2.serialization.SoapObject;public class AndroidWcfDemoActivity extends Activity { private Button mButton1; private TextView text; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mButton1 = (Button) findViewById(R.id.myButton1); text = (TextView) this.findViewById(R.id.show); mButton1.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { HelloService service = new HelloService("Master HaKu"); SoapObject result = service.LoadResult(); text.setText("WCF返回的数据是:" + result.getProperty(0)); } }); }}7. 最后运行结果
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了jQuery实现ajax调用WCF服务的方法。分享给大家供大家参考,具体如下:关于AJAX调用WCF服务分为跨域和不跨域两种方式,今天咱们先介绍下
Android中ActivityLifecycleCallbacks的实例详解以上就是使用ActivityLifecycleCallbacks的实例,代码注释写
AndroidNotification使用详解Notification核心代码(链式调用):适用于Android4.0以上(不兼容低版本)Notificatio
Androidxmlns的作用及其自定义实例详解xmlns:Android="http://schemas.android.com/apk/res/androi
Android自定义阴影效果详解及实例Android5.X中,Google为其增加了两个属性android:elevation=””与android:trans