版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010046908/article/details/50503428
首先,WebService进年来的使用人数很多了,它可以是一套接口多端调用,返回的格式是JSON和XML。在项目中使用的,可以分为如下的四个步骤:
1.下载ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar文件
2.开始创建HttpTransportSE 对象
3.使用Soap1.1的协议创建SoapSerializationEnvelope
4.创建SoapObject对象
5.设置与.NET提供的Web Service 保存更好的兼容
6.调用Web Service
7.处理返回结果,解析数据。
下面是我的源码:
(1)WebServiceUtil.java
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
/**
* @className WebServiceUtil 的工具类
* Created by lidong on 2016/1/11.
*/
public class WebServiceUtil {
public static SoapObject getData(String service_url, final String service_ns, final String methodName){
/**
* 创建HttpTransportSE 对象
*/
final HttpTransportSE ht = new HttpTransportSE(service_url);
ht.debug = true;
/**
* 使用Soap1.1的协议创建EEnvelope
*/
final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
/**
*创建SoapObject对象
*/
SoapObject soapObject = new SoapObject(service_ns,methodName);
envelope.bodyOut =soapObject;
/***
* 设置与.NET提供的Web Service 保存更好的兼容
*/
envelope.dotNet = true;
FutureTask<SoapObject> task = new FutureTask<SoapObject>(new Callable<SoapObject>() {
@Override
public SoapObject call() throws Exception {
//调用Web Service
ht.call(service_ns+methodName,envelope);
if (envelope.getResponse()!=null){
//获取服务器返回的结果
SoapObject result = (SoapObject) envelope.bodyIn;
return result;
}
return null;
}
});
new Thread(task).start();
try {
return task.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
return null;
}
}
(2).布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_web_service_demo"
tools:context="com.chni.lidong.androidtestdemo.webService.WebServiceDemo">
<android.support.v7.widget.RecyclerView
android:layout_margin="10dp"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@id/recyclerView"/>
</RelativeLayout>
3.WebServiceDemo
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import com.chni.lidong.androidtestdemo.R;
import com.chni.lidong.androidtestdemo.webService.adapter.MyAdapter;
import com.chni.lidong.androidtestdemo.webService.model.CarType;
import com.google.gson.Gson;
import org.ksoap2.serialization.SoapObject;
/**
* 访问WebService的demo
*/
public class WebServiceDemo extends AppCompatActivity {
//WSDL文档中的命名空间
private static final String nameSpace = "http://www.ichehang.com/";
//WSDL文档中的URL
private static final String endPoint = "http://app.ichehang.com/AppService.asmx";
//调用的方法
private static final String methodName = "GetBrandNew";
private RecyclerView recyclerView;
private CarType carType;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_service_demo);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
SoapObject data = WebServiceUtil.getData(endPoint, nameSpace, methodName);
Log.d("WebServiceDemo",data.getPropertyCount()+" "+data.getProperty(0) + "");
parseData( data);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
// 创建一个线性布局管理器
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
// 设置布局管理器
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(new MyAdapter(carType));
}
private void parseData(SoapObject data) {
carType = new Gson().fromJson(data.getProperty(0).toString(), CarType.class);
}
}
4.MyAdapter.java
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.chni.lidong.androidtestdemo.R;
import com.chni.lidong.androidtestdemo.webService.model.CarType;
/**
* Created by Administrator on 2016/1/11.
*/
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private CarType mDataset;
public MyAdapter(CarType mDataset) {
this.mDataset = mDataset;
}
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = View.inflate(parent.getContext(), R.layout.my_adapter_detail, null);
// 创建一个ViewHolder
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(MyAdapter.ViewHolder holder, int position) {
holder.mTextView.setText(mDataset.getTable().get(position).getName());
holder.mTextViewName.setText(mDataset.getTable().get(position).getImgname());
}
@Override
public int getItemCount() {
return mDataset.getTable().size();
}
public static class ViewHolder extends RecyclerView.ViewHolder
{
public TextView mTextView;
public TextView mTextViewName;
public ViewHolder(View itemView)
{
super(itemView);
mTextView = (TextView) itemView.findViewById(R.id.textView6);
mTextViewName = (TextView) itemView.findViewById(R.id.textView7);
}
}
}
(6)CarType.java
import java.util.List;
/**
* Created by lidong on 2016/1/11.
*/
public class CarType {
/**
* recordcount : 22
* table : [{"no":"1","id":"9","name":"奔驰","imgname":"ppxl"},{"no":"2","id":"11","name":"宝马","imgname":"baoma"},{"no":"3","id":"12","name":"保时捷","imgname":"baoshijie"},{"no":"4","id":"13","name":"奥迪","imgname":"aodi"},{"no":"5","id":"14","name":"路虎","imgname":"luhu"},{"no":"6","id":"15","name":"尼桑","imgname":"nisang"},{"no":"7","id":"16","name":"福特","imgname":"fute"},{"no":"8","id":"17","name":"丰田","imgname":"fengtian"},{"no":"9","id":"39","name":"玛莎拉蒂","imgname":"msld"},{"no":"10","id":"40","name":"克尔维特","imgname":"kewt"},{"no":"11","id":"51","name":"JEEP","imgname":"jeep"},{"no":"12","id":"53","name":"林肯","imgname":"linken"},{"no":"13","id":"73","name":"雷克萨斯","imgname":"lkss"},{"no":"14","id":"76","name":"道奇","imgname":"daoqi"},{"no":"15","id":"82","name":"凯迪拉克","imgname":"kaidilake"},{"no":"16","id":"93","name":"沃尔沃","imgname":"16kuanwoerwo"},{"no":"17","id":"95","name":"GMC","imgname":"GMC"},{"no":"18","id":"97","name":"宾利","imgname":"binli"},{"no":"19","id":"101","name":"法拉利","imgname":"falali"},{"no":"20","id":"113","name":"斯达泰克","imgname":"sidataike"},{"no":"21","id":"114","name":"大众","imgname":"dayin"},{"no":"22","id":"120","name":"乔治巴顿","imgname":"qiaozhibadu"}]
*/
private String recordcount;
/**
* no : 1
* id : 9
* name : 奔驰
* imgname : ppxl
*/
private List<TableEntity> table;
public void setRecordcount(String recordcount) {
this.recordcount = recordcount;
}
public void setTable(List<TableEntity> table) {
this.table = table;
}
public String getRecordcount() {
return recordcount;
}
public List<TableEntity> getTable() {
return table;
}
public static class TableEntity {
private String no;
private String id;
private String name;
private String imgname;
public void setNo(String no) {
this.no = no;
}
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setImgname(String imgname) {
this.imgname = imgname;
}
public String getNo() {
return no;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getImgname() {
return imgname;
}
}
}
最后展示一下结果吧: