最近开发当中使用了webServices作为接口,然后就看了一下,其实分析一下你就会发现其实就是拼一段长的http请求(哈哈,说的有点蛋)。你们看代码就明白了,直接上代码。
注意:由于是公司接口做了处理,大家可以拿很多天气接口进行测试,我做了简单的封装,你传入参数就行。
build
compile files('libs/ksoap2-android-assembly-3.6.2-jar-with-dependenci.jar')
compile 'com.google.code.gson:gson:2.2.4'
compile 'io.reactivex:rxjava:1.0.14'
compile 'io.reactivex:rxandroid:1.0.1'
业务bean Heads
package a79az.com.test;
import java.util.List;
/**
* Created by Administrator on 2017/3/21.
*/
public class Heads {
public Heads(List<HeadBean> head) {
Head = head;
}
private List<HeadBean> Head;
public List<HeadBean> getHead() {
return Head;
}
public void setHead(List<HeadBean> Head) {
this.Head = Head;
}
public static class HeadBean {
public HeadBean(String functionid, String orgcode, String interfacesusername, String interfacesuserpass, String operusername, String operuserpass, String opertime, int dbtype, String interfaces) {
this.functionid = functionid;
this.orgcode = orgcode;
this.interfacesusername = interfacesusername;
this.interfacesuserpass = interfacesuserpass;
this.operusername = operusername;
this.operuserpass = operuserpass;
this.opertime = opertime;
this.dbtype = dbtype;
this.interfaces = interfaces;
}
/**
* functionid : 1001
* orgcode : 001
* interfacesusername : test
* interfacesuserpass : test
* operusername : admin
* operuserpass : 1
* opertime : 20170304101522
* dbtype : 0
* interfaces : ******
*/
private String functionid;
private String orgcode;
private String interfacesusername;
private String interfacesuserpass;
private String operusername;
private String operuserpass;
private String opertime;
private int dbtype;
private String interfaces;
public String getFunctionid() {
return functionid;
}
public void setFunctionid(String functionid) {
this.functionid = functionid;
}
public String getOrgcode() {
return orgcode;
}
public void setOrgcode(String orgcode) {
this.orgcode = orgcode;
}
public String getInterfacesusername() {
return interfacesusername;
}
public void setInterfacesusername(String interfacesusername) {
this.interfacesusername = interfacesusername;
}
public String getInterfacesuserpass() {
return interfacesuserpass;
}
public void setInterfacesuserpass(String interfacesuserpass) {
this.interfacesuserpass = interfacesuserpass;
}
public String getOperusername() {
return operusername;
}
public void setOperusername(String operusername) {
this.operusername = operusername;
}
public String getOperuserpass() {
return operuserpass;
}
public void setOperuserpass(String operuserpass) {
this.operuserpass = operuserpass;
}
public String getOpertime() {
return opertime;
}
public void setOpertime(String opertime) {
this.opertime = opertime;
}
public int getDbtype() {
return dbtype;
}
public void setDbtype(int dbtype) {
this.dbtype = dbtype;
}
public String getInterfaces() {
return interfaces;
}
public void setInterfaces(String interfaces) {
this.interfaces = interfaces;
}
}
}
网络请求主逻辑 net
public class Net {
private static final String TAG = "Net";
String SERVICE_NAMESPACE = "mia.hn.cn";
//大家不要试公司接口我做了假处理
String SERVICE_URL = "http://114.55.63.146/MiaInterface.asmx";
String methodName = "DmsInterfaces";
public static Net newInstance() {
Net fragment = new Net();
return fragment;
}
public Observable<String> getSupportCity(final Map<String,String> paras){
Observable<String> observable = Observable.create(new Observable.OnSubscribe<String>() {
@Override
public void call(final Subscriber<? super String> subscriber) {
SoapObject request = new SoapObject(SERVICE_NAMESPACE, methodName);
Iterator it = paras.keySet().iterator();
while (it.hasNext()) {
String key = it.next().toString();
request.addProperty(key, paras.get(key));
}
final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
//envelope.bodyOut = request;
// 创建HttpTransportSE传输对象
final HttpTransportSE ht = new HttpTransportSE(SERVICE_URL);
envelope.setOutputSoapObject(request);
new Thread(new Runnable() {
@Override
public void run() {
// 调用webservice
try {
ht.call(SERVICE_NAMESPACE + "/" + methodName, envelope);
// 获取服务器响应返回的SOAP消息
SoapObject responseSoap = (SoapObject) envelope.bodyIn;
Log.d(TAG, "run: "+responseSoap.getProperty(0).toString());
String getS = responseSoap.getProperty(1).toString();
subscriber.onNext(getS);
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
}
}).start();
}
}).observeOn(AndroidSchedulers.mainThread());
return observable;
}
}
测试
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Heads.HeadBean bean = new Heads.HeadBean("1001", "001", "test", "test",
"admin", "1", "20170304101522", 0, "MIADMS");
List<Heads.HeadBean> beans = new ArrayList<>();
beans.add(bean);
Heads heads = new Heads(beans);
Gson gson = new Gson();
String s = gson.toJson(heads);
Map<String,String> map = new HashMap<>();
map.put("ReqHeadJson",s);
map.put("ReqBodyJson",null);
Log.d(TAG, "onCreate: "+s);
Net.newInstance().getSupportCity(map).subscribe(new Observer<String>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
Log.d(TAG, "onError: "+e.getMessage());
}
@Override
public void onNext(String s) {
Log.d(TAG, "onNext: "+s);
}
});
}
}
log返回数据:
好了 这个webServices就算入门了
代码:这里写链接内容