web service上传参数代码实例
这次做的项目用到webservice比较多,最开始在网上看的参考dome,发现都不行,后来发现安卓4.0以后有很大的不同,在做传参时,有些东西需要注意:
第一,命名空间:与服务器一致,命名空间后缀千万不要加“/”;
第二,方法名:与服务器一致;
第三,url:就是服务器地址不加后面的?=。。。;
代码如下:
- package com.example.web;
- import java.util.ArrayList;
- import org.ksoap2.SoapEnvelope;
- import org.ksoap2.serialization.SoapObject;
- import org.ksoap2.serialization.SoapSerializationEnvelope;
- import org.ksoap2.transport.HttpTransportSE;
- import android.annotation.SuppressLint;
- import android.annotation.TargetApi;
- import android.app.Activity;
- import android.os.Build;
- import android.os.Bundle;
- import android.os.StrictMode;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
- //@SuppressLint("NewApi")
- @TargetApi(Build.VERSION_CODES.GINGERBREAD) @SuppressLint("NewApi") public class MainActivity extends Activity {
- public static final String TAG ="webService_pj";
- Button button;
- TextView resultView;
- String result ;
- String str_userno = "144";
- // @TargetApi(Build.VERSION_CODES.GINGERBREAD) @SuppressLint("NewApi")
- @TargetApi(Build.VERSION_CODES.GINGERBREAD) @SuppressLint("NewApi") @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- // 强制在UI线程中操作
- StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
- .detectDiskReads()
- .detectDiskWrites()
- .detectNetwork()
- .penaltyLog()
- .build());
- StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
- .detectLeakedSqlLiteObjects()
- .detectLeakedClosableObjects()
- .penaltyLog()
- .penaltyDeath()
- .build());
- resultView = (TextView)findViewById(R.id.TextView);
- button = (Button) findViewById(R.id.butt);
- button.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- // TODO Auto-generated method stub
- String nameSpace = "";
- // 调用的方法名称
- String methodName = "";
- String url = "http://i.cnblogs.com/EditPosts.aspx";
- String soapAction = "nameSpace+methodName";
- ArrayList<String> params= new ArrayList<String>();
- params.add("str_userno");
- params.add("str_sex");
- params.add("str_telephone");
- params.add("str_pwd");
- ArrayList<String> vals= new ArrayList<String>();
- vals.add("33");
- vals.add("1");
- vals.add("23442");
- vals.add("1443");
- new MyThread(nameSpace,methodName,url,soapAction, params,vals).start();
- //将WebService返回的结果显示在TextView中
- resultView.setText("dfsd"+getResult());
- }
- });
- }
- public String getResult(){
- return result;
- }
- private class MyThread extends Thread
- {
- private String nameSpace;
- private String methodName;
- private String soapAction;
- private String url;
- private ArrayList<String> params;
- private ArrayList<String> vals;
- public MyThread(String nameSpace, String methodName,
- String url, String soapAction,ArrayList<String> params,ArrayList<String> vals){
- this.nameSpace = nameSpace;
- this.methodName = methodName;
- this.url = url;
- this.soapAction = soapAction;
- this.params = params;
- this.vals = vals;
- }
- @Override
- public void run()
- {
- result= getRemoteInfo(nameSpace, methodName, url,
- soapAction,params,vals);
- }
- }
- public String getRemoteInfo(String nameSpace, String methodName,
- String url, String soapAction,ArrayList<String> params,ArrayList<String> vals) {
- // 指定WebService的命名空间和调用的方法名
- SoapObject rpc = new SoapObject(nameSpace, methodName);
- //设置需调用WebService接口需要传入的两个参数mobileCode、userId
- //
- //for (int i = 0; i < params.size();i++) {
- rpc.addProperty(params.get(0),vals.get(0));
- rpc.addProperty(params.get(1),vals.get(1));
- rpc.addProperty(params.get(2),vals.get(2));
- rpc.addProperty(params.get(3),vals.get(3));
- System.out.println(rpc);
- // rpc.addProperty("Content-Type", "text/xml; charset=utf-8");
- // rpc.addProperty("Content-Length", "length");
- //}
- //生成调用WebService方法的SOAP请求信息,并指定SOAP的版本
- SoapSerializationEnvelope envelope =new SoapSerializationEnvelope(SoapEnvelope.VER10);
- envelope.bodyOut = rpc;
- envelope.setOutputSoapObject(rpc);
- // envelope.bodyIn = rpc;
- envelope.dotNet = true;
- // HttpTransportSE transport = new HttpTransportSE(url);
- HttpTransportSE transport=new HttpTransportSE(url);//20秒限时
- try {
- // 调用WebService
- transport.call(soapAction,envelope);
- System.out.println("haha");
- if(envelope.getResponse()!=null){
- SoapObject object = (SoapObject)envelope.bodyIn;
- if (object != null) {
- // 获取返回的结果
- result =object.getProperty(0).toString();
- System.out.println("faf"+result);
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- System.out.println("yghu"+result);
- }
- // 获取返回的数据
- return result;
- }
- }
写的不好之处希望大神指点下,赶鸡不进,嘿嘿,希望对大家有用!