摘要:本文将带你了解Android应用开发之Android开发Web Service通信,希望本文对大家学Android有所帮助
本文将带你了解Android应用开发之Android开发Web Service通信,希望本文对大家学Android有所帮助
与HTTP通信方式相比,HTTP不能实现远程方法的调用,Web Service可以实现。Web Service也有服务器端和客户端,一般采用XFire来搭建服务器。客户端Android系统中并没有提供直接与Web Service互相调用的操作类库,一般是采用第三方提供的类库才可以完成,比较常用的是Ksoap2类库。
一个完整的Web Service通信应该有服务器的建设和客户端的建设两个部分,服务器端的建设比较繁琐,可以借助内置Tomcat的MyEclipse8.5来实现。
其实网络上有很多已经建立好的服务器端,在服务器端提供开发好的方法供外界使用,例如:在//www.webxml.com.cn服务器上对外公开了很多方法,比如天气情况、航班信息、手机归属地等,客户端只需要调用这些方法和相关参数,就可以编程调用这些方法,而不需要自己再去建立服务器。
下面编写了一个实例:
新建主布局文件:
Xml代码 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="//schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/et" android:hint="请输入城市" android:selectAllOnFocus="true"/> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/search_but" android:text="获取天气信息"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/text" android:textSize="24sp"/> </LinearLayout> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="//schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/et"
android:hint="请输入城市"
android:selectAllOnFocus="true"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/search_but"
android:text="获取天气信息"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text"
android:textSize="24sp"/>
</LinearLayout> 下面是主程序类:
Java代码 package xiao.fuyan.testapp; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE; /** * Created by xiao on 2017/2/3. */ public class WebServiceActivity extends Activity { private Button search_but; private TextView textView; private EditText et; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.webservice_xml); et = (EditText) findViewById(R.id.et); search_but = (Button) findViewById(R.id.search_but); search_but.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String city = et.getText().toString(); getWeather(city, " "); } }); } //定义命名空间 private static final String NAMESPACE = "//WebXml.com.cn/"; //定义请求WSDL文档的URL private static final String URL = "//www.webxml.com.cn/WebServices/WeatherWS.asmx"; //定义调用方法 private static final String METHOD_NAME = "getWeather"; //定义命名空间+调用方法名 private static final String SOAP_ACTION = "//WebXml.com.cn/getWeather"; private SoapObject detail; public void getWeather(String city, String id){ try { textView = (TextView) findViewById(R.id.text); //实例化SoapObject对象 SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME); //添加参数 rpc.addProperty("thecityname", city); rpc.addProperty("id", id); //实例化SoapSerializationEnvelope对象 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); //封装输入的SoapObject对象rpc envelope.bodyOut = rpc; //要访问的服务器是.net服务器,否则设置为false envelope.dotNet = true; //设置要输出的SoapObject对象 envelope.setOutputSoapObject(rpc); //指定WSDL地址 HttpTransportSE ht = new HttpTransportSE(URL); //使用调试 ht.debug = true; //调用web service ht.call(SOAP_ACTION, envelope); //获取返回信息 detail = (SoapObject) envelope.getResponse(); textView.setText(detail.toString()); return; }catch (Exception e){ e.printStackTrace(); } } } package xiao.fuyan.testapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
/**
* Created by xiao on 2017/2/3.
*/
public class WebServiceActivity extends Activity {
private Button search_but;
private TextView textView;
private EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webservice_xml);
et = (EditText) findViewById(R.id.et);
search_but = (Button) findViewById(R.id.search_but);
search_but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String city = et.getText().toString();
getWeather(city, " ");
}
});
}
//定义命名空间
private static final String NAMESPACE = "//WebXml.com.cn/";
//定义请求WSDL文档的URL
private static final String URL = "//www.webxml.com.cn/WebServices/WeatherWS.asmx";
//定义调用方法
private static final String METHOD_NAME = "getWeather";
//定义命名空间+调用方法名
private static final String SOAP_ACTION = "//WebXml.com.cn/getWeather";
private SoapObject detail;
public void getWeather(String city, String id){
try {
textView = (TextView) findViewById(R.id.text);
//实例化SoapObject对象
SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);
//添加参数
rpc.addProperty("thecityname", city);
rpc.addProperty("id", id);
//实例化SoapSerializationEnvelope对象
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
//封装输入的SoapObject对象rpc
envelope.bodyOut = rpc;
//要访问的服务器是.net服务器,否则设置为false
envelope.dotNet = true;
//设置要输出的SoapObject对象
envelope.setOutputSoapObject(rpc);
//指定WSDL地址
HttpTransportSE ht = new HttpTransportSE(URL);
//使用调试
ht.debug = true;
//调用web service
ht.call(SOAP_ACTION, envelope);
//获取返回信息
detail = (SoapObject) envelope.getResponse();
textView.setText(detail.toString());
return;
}catch (Exception e){
e.printStackTrace();
}
}
}
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之Android频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号