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;
  • }
  • }

写的不好之处希望大神指点下,赶鸡不进,嘿嘿,希望对大家有用!

web service上传参数代码实例的更多相关文章

  1. java web service 上传下载文件

    1.新建动态web工程youmeFileServer,新建包com,里面新建类FileProgress package com; import java.io.FileInputStream; imp ...

  2. web service 上传file说明

    之前做过一个接口,PI发布WEB SERVICE给对方调,传附件到SAP... 接口中包含文件名称,文件类型,文件流... 1.SOAPUI新建项目: 文件内容的地方会自动带上一个ID,这个ID对应文 ...

  3. JSCH实现文件上传的代码实例

    package com.vcredit.ddcash.monitor.sendmail; import java.io.File;import java.io.FileInputStream;impo ...

  4. JavaWeb实现文件上传下载功能实例解析

    转:http://www.cnblogs.com/xdp-gacl/p/4200090.html JavaWeb实现文件上传下载功能实例解析 在Web应用系统开发中,文件上传和下载功能是非常常用的功能 ...

  5. web 文件上传的几种方式

    问题 文件上传在WEB开发中应用很广泛. 文件上传是指将本地图片.视频.音频等文件上传到服务器上,可以供其他用户浏览或下载的过程. 以下总结了常见的文件(图片)上传的方式和要点处理. 表单上传 这是传 ...

  6. Web文件上传方法总结大全

    1. 表单上传 这是传统的form表单上传,使用form表单的input[type=”file”]控件,可以打开系统的文件选择对话框,从而达到选择文件并上传的目的,它的好处是多浏览器兼容,它是web开 ...

  7. web 图片上传实现本地预览

    在说上传之前先说说如何替换or美化浏览器自带的简陋上传按钮(自定义自己的上传按钮 如:img): 1.将自定义上传按钮上方添加 input file 框,实现input实现透明处理. 2.对自定义上传 ...

  8. JavaWeb实现文件上传下载功能实例解析 (好用)

    转: JavaWeb实现文件上传下载功能实例解析 转:http://www.cnblogs.com/xdp-gacl/p/4200090.html JavaWeb实现文件上传下载功能实例解析 在Web ...

  9. 从WEB SERVICE 上返回大数据量的DATASET

    前段时间在做一个项目的时候,遇到了要通过WEB SERVICE从服务器上返回数据量比较大的DATASET,当然,除了显示在页面上以外,有可能还要用这些数据在客户端进行其它操作.查遍了网站的文章,问了一 ...

随机推荐

  1. JS魔法堂:函数重载 之 获取变量的数据类型

    Brief 有时我们需要根据入参的数据类型来决定调用哪个函数实现,就是说所谓的函数重载(function overloading).因为JS没有内置函数重载的特性,正好给机会我们思考和实现一套这样的机 ...

  2. ElasticSearch 配置详解

    配置文件位于es根目录的config目录下面,有elasticsearch.yml和logging.yml两个配置,主配置文件是elasticsearch.yml,日志配置文件是logging.yml ...

  3. Java Lambda表达式初探

    Java Lambda表达式初探 前言 本文受启发于Trisha Gee在JavaOne 2016的主题演讲Refactoring to Java 8. Java 8已经发行两年多,但很多人仍然在使用 ...

  4. Winform如何实现ComboBox模糊查询

    最近朋友问了一个关于Winform实现ComboBox模糊查询的知识点,自己好久没有搞Winform了,就上手练了一下,废话不多说,进入正题. 前台设计: 前台就是一个简单的Form窗体+一个Comb ...

  5. Handler "BlockViewHandler" has a bad module "ManagedPipelineHandler" in its module list

    当你的ASP.NET MVC项跑在IIS时,出现如标题Handler "BlockViewHandler" has a bad module "ManagedPipeli ...

  6. PHP面试题汇总

    1.用PHP打印出前一天的时间格式是2014-01-13 12:10:21(2分) 2.echo(),print(),print_r()的区别(3分) 3.能够使HTML和PHP分离开使用的模板(1分 ...

  7. jquery.tagthis和jquery.autocomplete一起实现标签

    目的 jquery.tagthis:http://www.dangribbin.co/jquery-tag-this/demo/ 使用tagthis控件实现标签的输入提醒功能,每个标签具有id和tex ...

  8. 点击div折叠

    <!doctype html> <html> <head> <meta charset="utf-8"> <meta cont ...

  9. 实现在ios开发中的App滑动封面 UIScrollView

    - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. _scrol ...

  10. Xcode push带有cocoapods类库的项目到git仓库

    关于git之一点不熟悉,以前公司的项目搭建,版本控制这块,都是有专门的人在做,当然那时候也是用的git项目中也包含了cocoapods类库,当前公司比较闲, 所以研究了下git,用的是git.osch ...