android使用ksoap2调用webservice接口实现电话号码查询功能
2013-01-07 16:04
786 查看
最近在研究android调用webservice接口的问题,在网上查找了很多资料,现在把自己的整理出来分享一下,感谢其他一些开源的朋友,提供了很多的帮助
首先来看一下活动类的布局文件main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:paddingTop="5dip" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="手机号码(段):" />
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="这里显示查询的结果"
android:inputType="textPhonetic"
android:singleLine="true" />
<EditText
android:id="@+id/editText2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="在这里输入要查询的手机号码"
android:inputType="textPhonetic"
android:singleLine="true" />
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="退出" />
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:onClick="onClick"
android:text="查询" />
</LinearLayout>
在这里将调用webservice接口的方法写到一个独立的类里面,然后再主活动里面调用这个方法
package com.example.webservice;
import java.io.IOException;
import java.util.Map;
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 android.content.Context;
/**
* 这是一个用于请求webservice的工具类
*
* @author xxs
*
*/
public class WebRequest {
/**
* @param namespace
* 命名空间
* @param methodname
* 要调用的方法
* @param url
* 要访问的url
* @param soapaction
* 接口调用地址
* @param paras
* 要传入的参数
* @return 返回的SoapObject对象
*/
public static String GetRequest(String namespace, String methodname,
String url, String soapaction, Map<String, String> paras) {
String s = null;
// 创建SoapObject对象,并指定WebService的命名空间和调用的方法名
SoapObject rpc = new SoapObject(namespace, methodname);
// 设置WebService方法的参数
for (Map.Entry<String, String> entry : paras.entrySet()) {
rpc.addProperty(entry.getKey(), entry.getValue());
}
// 创建SoapSerializationEnvelope对象,并指定WebService的版本
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
// 设置bodyout属性
envelope.bodyOut = rpc;
envelope.dotNet = true;
envelope.setOutputSoapObject(rpc);
// 创建HttpTransportSE对象,并指定WSDL文档的URL
HttpTransportSE ht = new HttpTransportSE(url);
try {// 调用WebService
ht.call(soapaction, envelope);
SoapObject object = (SoapObject) envelope.bodyIn;//取得返回值
if (object != null) {
s = object.getProperty(0).toString();//将返回的值转化为String
}else{
s = "没有查询到相关信息";
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return s;
}
}
接下来就是在活动类调用上面工具类中GetRequest方法就是了
package com.example.webservice;
import java.util.HashMap;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
/**
* 访问webservice
* @author xxs
*
*/
public class WebServiceActivity extends Activity {
private EditText txt1, txt2;//定义输入框
private String nameSpace = "http://WebXml.com.cn/";//命名空间
private String methodName = "getMobileCodeInfo";//要使用的方法
private String endPoint = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";//要访问的wsdl的url地址
private String soapAction = "http://WebXml.com.cn/getMobileCodeInfo";//接口地址
private HashMap<String, String>paras = new HashMap<String, String>();//传入参数
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);//填充xml布局文件
FindWidget();//调用查找控件的方法
}
/**
* 此方法用于查找xml文件中的控件
*/
public void FindWidget(){
txt1 = (EditText) findViewById(R.id.editText1);
txt2 = (EditText) findViewById(R.id.editText2);
}
/**
* Button触发事件监听
* @param v
*/
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
//传入参数
paras.put("mobileCode", txt2.getText().toString().trim());
paras.put("userID", null);
handler.sendEmptyMessage(1);
break;
case R.id.button2:
finish();
break;
default:
break;
}
}
Handler handler = new Handler() {
String result = null;
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 1:
new Thread(new Runnable() {
@Override
public void run() {
//调用请求webservice的方法
result = WebRequest.GetRequest(nameSpace, methodName,endPoint,soapAction, paras);
handler.sendEmptyMessage(2);
}
}).start();
break;
case 2:
txt1.setText(result);//将处理结果显
default:
break;
}
}
};
}
运行截图:
首先来看一下活动类的布局文件main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:paddingTop="5dip" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="手机号码(段):" />
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="这里显示查询的结果"
android:inputType="textPhonetic"
android:singleLine="true" />
<EditText
android:id="@+id/editText2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="在这里输入要查询的手机号码"
android:inputType="textPhonetic"
android:singleLine="true" />
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="退出" />
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:onClick="onClick"
android:text="查询" />
</LinearLayout>
在这里将调用webservice接口的方法写到一个独立的类里面,然后再主活动里面调用这个方法
package com.example.webservice;
import java.io.IOException;
import java.util.Map;
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 android.content.Context;
/**
* 这是一个用于请求webservice的工具类
*
* @author xxs
*
*/
public class WebRequest {
/**
* @param namespace
* 命名空间
* @param methodname
* 要调用的方法
* @param url
* 要访问的url
* @param soapaction
* 接口调用地址
* @param paras
* 要传入的参数
* @return 返回的SoapObject对象
*/
public static String GetRequest(String namespace, String methodname,
String url, String soapaction, Map<String, String> paras) {
String s = null;
// 创建SoapObject对象,并指定WebService的命名空间和调用的方法名
SoapObject rpc = new SoapObject(namespace, methodname);
// 设置WebService方法的参数
for (Map.Entry<String, String> entry : paras.entrySet()) {
rpc.addProperty(entry.getKey(), entry.getValue());
}
// 创建SoapSerializationEnvelope对象,并指定WebService的版本
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
// 设置bodyout属性
envelope.bodyOut = rpc;
envelope.dotNet = true;
envelope.setOutputSoapObject(rpc);
// 创建HttpTransportSE对象,并指定WSDL文档的URL
HttpTransportSE ht = new HttpTransportSE(url);
try {// 调用WebService
ht.call(soapaction, envelope);
SoapObject object = (SoapObject) envelope.bodyIn;//取得返回值
if (object != null) {
s = object.getProperty(0).toString();//将返回的值转化为String
}else{
s = "没有查询到相关信息";
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return s;
}
}
接下来就是在活动类调用上面工具类中GetRequest方法就是了
package com.example.webservice;
import java.util.HashMap;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
/**
* 访问webservice
* @author xxs
*
*/
public class WebServiceActivity extends Activity {
private EditText txt1, txt2;//定义输入框
private String nameSpace = "http://WebXml.com.cn/";//命名空间
private String methodName = "getMobileCodeInfo";//要使用的方法
private String endPoint = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";//要访问的wsdl的url地址
private String soapAction = "http://WebXml.com.cn/getMobileCodeInfo";//接口地址
private HashMap<String, String>paras = new HashMap<String, String>();//传入参数
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);//填充xml布局文件
FindWidget();//调用查找控件的方法
}
/**
* 此方法用于查找xml文件中的控件
*/
public void FindWidget(){
txt1 = (EditText) findViewById(R.id.editText1);
txt2 = (EditText) findViewById(R.id.editText2);
}
/**
* Button触发事件监听
* @param v
*/
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
//传入参数
paras.put("mobileCode", txt2.getText().toString().trim());
paras.put("userID", null);
handler.sendEmptyMessage(1);
break;
case R.id.button2:
finish();
break;
default:
break;
}
}
Handler handler = new Handler() {
String result = null;
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 1:
new Thread(new Runnable() {
@Override
public void run() {
//调用请求webservice的方法
result = WebRequest.GetRequest(nameSpace, methodName,endPoint,soapAction, paras);
handler.sendEmptyMessage(2);
}
}).start();
break;
case 2:
txt1.setText(result);//将处理结果显
default:
break;
}
}
};
}
运行截图:
相关文章推荐
- 在android中使用xml调用webservice,实现自己的单词查询
- android使用ksoap2调用webservice接口返回错误HTTP status: 500
- android中webService接口调用示例:查询电话号码所在地
- Android使用ksoap2调用C#中的webservice实现图像上传
- Android使用ksoap2调用C#中的webservice实现图像上传
- Android使用ksoap2调用C#中的webservice函数方法
- 实现功能要求统计图书的总价,编写方法查询出在50元以上的所有图书,使用排序接口图书按照价格排序(升序排序)
- Android使用ksoap2-android调用WebService学习
- 在Android中使用Android Ksoap2调用WebService
- Java调用WebService接口实现发送手机短信验证码功能
- 在Android 中使用KSOAP2调用WebService
- 在Android 中使用KSOAP2调用WebService
- 在Android 中使用KSOAP2调用WebService
- Java调用WebService接口实现发送手机短信验证码功能,java 手机验证码,WebService接口调用
- Android使用KSOAP2调用WebService及正确导入jar包的问题
- Java调用WebService接口实现发送手机短信验证码功能,java 手机验证码,WebService接口调用
- Android使用ksoap2-android调用WebService学习
- 在Android 中使用KSOAP2调用WebService
- Java调用WebService接口实现发送手机短信验证码功能
- android调用webservice接口 手机号所在地查询 新手总结