安卓学习-ksoap2_WebService客户端调用

image

image

image

image

 

image

image

image

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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<EditText
android:id="@+id/editTextNum"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="32dp"
android:hint="请输入你的手机号码"
android:ems="10" >

<requestFocus />
</EditText>

<Button
android:id="@+id/btnSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/editTextNum"
android:layout_marginTop="42dp"
android:text="查询" />

<TextView
android:id="@+id/textViewResult"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btnSearch"
android:layout_centerVertical="true"
android:text=""
/>

</RelativeLayout>

MainActivity.java

package com.example.administrator.webservice;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

public class MainActivity extends AppCompatActivity {

private EditText editText;
private Button button;
private TextView textView;
public MyThread thread;
public Handler handler;
public String telephone;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editTextNum);

button = (Button) findViewById(R.id.btnSearch);
thread = new MyThread();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
telephone = editText.getText().toString();
if ("".equals(telephone) || telephone.length() < 7) {
Toast.makeText(getApplicationContext(), "输入手机号不合规范!",
Toast.LENGTH_LONG).show();
editText.requestFocus();
return;
}
else
{
new Thread(thread).start();
}
}
});
handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 0x01:
Bundle bundle = msg.getData();
String p=bundle.getString("result").trim();
System.out.println(p);
Toast.makeText(MainActivity.this,bundle.getString("result"), Toast.LENGTH_SHORT).show();

/* TextView tv= (TextView) findViewById(R.id.textViewResult);
tv.setText(p);*/
break;
}
}
};

}
//创建线程
public class MyThread implements Runnable
{
public void run()
{
Looper.prepare();//创建本线程的消息队列并初始化
getPhone(telephone);
Looper.loop();//开始运行消息队列
}
}

public void getPhone(String phoneSec) {

//命名空间
String nameSpace = "http://WebXml.com.cn/";
//调用的方法名称
String methodName = "getMobileCodeInfo";
//EndPoint
String endPoint = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl";
//SOAP Action
String soapAction = "http://WebXml.com.cn/getMobileCodeInfo";


//指定WebService的命名空间和调用的方法名
SoapObject rpc = new SoapObject(nameSpace, methodName);
//设置需调用WebService接口需要传入的两个擦数mobileCode.userId

rpc.addProperty("mobileCode",phoneSec);
System.out.println(phoneSec);
rpc.addProperty("userId", "");
//生成diar调用WebService方法的SOAP请求信息,并指定SOAP的版本
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = rpc;
envelope.dotNet = true;//设置是否调用的是dotNet开发的WebService
HttpTransportSE transport = new HttpTransportSE(endPoint);
try {
transport.call(soapAction, envelope);//调用WebService
} catch (Exception e) {
e.printStackTrace();
}
//获取返回的数据
SoapObject object = (SoapObject) envelope.bodyIn;
//获取返回的结果
String result = object.getProperty("getMobileCodeInfoResult")
.toString();
Message msg=new Message();
Bundle bundle = new Bundle();
bundle.putString("result", result);
msg.setData(bundle);
msg.what = 0x01;
handler.handleMessage(msg);
// System.out.println(result);

}
}

转载于:https://my.oschina.net/xiaofeiandroid/blog/655450

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值