本文主要是介绍安卓开发 调用 jax-ws 格式的webservices,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
通过 安卓调用 java 和 .net开发的webservices
1 JAVA版 服务器端 使用myeclipse 10+jax-ws API 很容易做一个服务端
1 新建一个 webservices project 工程 (导入 myeclipse library jax-ws 2.1 包 和 GSON2.4包, 这里用gson返回字符串
2 建立一个 bean 主要用来存储数据, 不用也可以
只有2个字段 private string name ; private int age; 配置 get set
3 建立一个 对外服务类, 写几个函数
package zms.zte.wbs;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import zms.zte.wbs.bean.userinfo;
public class SayHello {
/* public static void main(String[] args) {
SayHello sayHello=new SayHello();
System.out.println(sayHello.getusers());
}*/
public String getuser(String name,int age)
{
userinfo u=new userinfo(name, age);
Gson sGson=new Gson();
return sGson.toJson(u);
}
public String getusers()
{
List<userinfo> ls=new ArrayList<userinfo>();
for (int i=0; i<10; i++)
{
userinfo userinfo=new userinfo("name"+i, i);
ls.add(userinfo);
}
Gson sGson=new Gson();
return sGson.toJson(ls);
}
}
4 然后 把这个类作为webservice发布
new--webserices--选择 从 bean 生成 , 不是从wsdl生成
5 访问 http://localhost:8080/wbs/sayhelloport?wsdl
6 先用 java 写个测试类
新建一个java工程, 然后新建webservices client 找到 http://localhost:8080/wbs/SayHelloPort?wsdl
会自动生成代码
7 测试成功后 开始用安卓 访问了
8 代码部分
package com.example.androidwbs;
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.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
//java webserice , 参考assets/sayhelloport.wsdl
public static final String NAME_SPACE = "http://wbs.zte.zms/";
public static String URL = "http://192.168.1.103:8080/wbs/SayHelloPort";
public static String Method2="getuser";
public final String Action2=NAME_SPACE+Method2;
//.net webservice
//定义控件
private TextView tx;
private Button btn;
private TextView tx2;
private TextView tx3;
private Button btn2;
private EditText editText;
private String telString;
private Handler handler1 = new Handler() {
@Override
public void handleMessage(Message msg) {
if(msg.what==1)
{
tx.setText(String.valueOf(msg.obj));
}
else if (msg.what==2) {
tx3.setText(String.valueOf(msg.obj));
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tx=(TextView)findViewById(R.id.textView1);
tx2=(TextView)findViewById(R.id.textView2);
tx3=(TextView)findViewById(R.id.textView3);
editText=(EditText)findViewById(R.id.editText1);
btn=(Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Threads t1=new Threads();
t1.start();
}
});
btn2=(Button)findViewById(R.id.button2);
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 手机号码(段)
String phoneSec = editText.getText().toString().trim();
// 简单判断用户输入的手机号码(段)是否合法
if ("".equals(phoneSec) || phoneSec.length() < 7) {
// 给出错误提示
editText.setError("您输入的手机号码(段)有误!");
editText.requestFocus();
// 将显示查询结果的TextView清空
tx2.setText("");
return;
} else {
telString=editText.getText().toString().trim();
Threads2 threads2=new Threads2();
threads2.start();
}
}
});
}
class Threads extends Thread{
@Override
public void run() {
//tomcat 7.055 for 64 jdk 1.6 for 64 web3.0 这里写 ver11 ,如果是ver12会报 http 415错误
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
SoapObject request = new SoapObject(NAME_SPACE,Method2);
//传递参数必须是arg开头,这里搞死我了 搞了我一上午,网上说,按顺序即可,毛哦, 实践是检验真理的唯一标准!
request.addProperty("arg0", "张木生" );
request.addProperty("arg1", 443 );
//写真实的变量名也没有用,必须是 arg0 ,arg1 一直拍下去,好像只支持10个参数.
/* request.addProperty("name", "网名" );
request.addProperty("age", "16" );*/
envelope.setOutputSoapObject(request);
envelope.bodyOut = request;
envelope.dotNet = false;
HttpTransportSE ht = new HttpTransportSE(URL);
try{
ht.call(Action2,envelope);
//貌似 ht.call(null,envelope); 也可以
if(envelope.getResponse()!=null){
Log.v("zms", "返回的信息:"+envelope.getResponse().toString());
Message msg = handler1.obtainMessage();
msg.what=1;
msg.obj =envelope.getResponse().toString();
handler1.sendMessage(msg);
}
else
Log.v("zms", "没取到");
} catch(Exception e){
Log.v("zms",e.toString());
e.printStackTrace();
}
}
}
class Threads2 extends Thread{
@Override
public void run() {
Log.v("zms","第2个线程启动");
//命名空间
String nameSpace="http://WebXml.com.cn/";
//方法
String methodName="getMobileCodeInfo";
// EndPoint
String endPoint = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";
// SOAP Action
String soapAction = "http://WebXml.com.cn/getMobileCodeInfo";
// 指定WebService的命名空间和调用的方法名
SoapObject rpc = new SoapObject(nameSpace, methodName);
// 设置需调用WebService接口需要传入的两个参数mobileCode、userId
rpc.addProperty("mobileCode", telString);
rpc.addProperty("userId", "");
// 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
envelope.bodyOut = rpc;
// 等价于envelope.bodyOut = rpc;
// envelope.setOutputSoapObject(rpc);
// 设置是否调用的是dotNet开发的WebService
envelope.dotNet = true;
HttpTransportSE transport = new HttpTransportSE(endPoint);
try {
// 调用WebService
transport.call(soapAction, envelope);
/* // 获取返回的数据
SoapObject object = (SoapObject) envelope.bodyIn;
// 获取返回的结果
String result = object.getProperty(0).toString();
Log.v("zms",result);*/
if(envelope.getResponse()!=null){
Log.v("zms", "返回的信息:"+envelope.getResponse().toString());
Message msg = handler1.obtainMessage();
msg.what=2;
msg.obj =envelope.getResponse().toString();
handler1.sendMessage(msg);
}
else
{
Log.v("zms", "没取到");
Message msg = handler1.obtainMessage();
msg.what=2;
msg.obj ="没有查询到";
handler1.sendMessage(msg);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
9 效果
这篇关于安卓开发 调用 jax-ws 格式的webservices的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!