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. php对xml文件进行CURD操作

    XML是一种数据存储.交换.表达的标准: - 存储:优势在于半结构化,可以自定义schema,相比关系型二维表,不用遵循第一范式(可以有嵌套关系): - 交换:可以通过schema实现异构数据集成: ...

  2. Visual Studio Code编写HTML

    第一步双击打开Visual Studio Code,我们发现什么也没有,但是有一个默认打开的编辑页面.我们先点击File->OpenFoldor 为什么先这么做呢,有两个原因,第一个原因假如你有 ...

  3. 【转】ASP.NET"正在中止线程"错误原因

    最近做的系统中老出现的一些问题不太明白,在使用 Response.End.Response.Redirect 或 Server.Transfer 时出现 ThreadAbortException , ...

  4. 动态dynamically变更母版_Layout页body标签css的class

    这个功能演示是Insus.NET最近想实现的一个功能,就是动态dynamically变更母版_Layout页body标签的样式css的class. 很多视图共同一个母版_Layout页,但是某一个视图 ...

  5. 使用jquery的append(content)方法的注意事项

    append(content)函数:向每个匹配的元素内部追加内容. 如以下示例: 向所有段落中追加一些HTML标记. HTML 代码: <p>I would like to say: &l ...

  6. WCF Throttling 限流的三道闸口

    WCF Throttling 限流的三道闸口 一.WCF Throttling  流量限制简介 我们期望WCF服务端能够处理尽可能多的并发请求,但是资源是有限的,服务不可能同时处理无限多的并发请求,如 ...

  7. Redis系列四之复制

    一.复制基本配置与演示 为了避免单点故障,Redis提供了复制功能,可以实现自动同步的过程. 1.配置 同步后的数据分为两类:一类是主数据库(master),一类是从数据库(slave).主数据库可以 ...

  8. Repository 设计模式介绍

    在DDD设计中大家都会使用Repository pattern来获取domain model所需要的数据. 1.什么是Repository? "A Repository mediates b ...

  9. [moka同学笔记]YII2.0 判断签约状态,sql的两种查询方法

    方法一: //判断签约状态 $signed = 0; $sql="SELECT * from usho_community_sign_record WHERE com_id=$r->i ...

  10. ViewPager的刷新、限制预加载、缓存所有

    [框架]: 公共部分:左侧菜单.TitleBar.RadioGroup(3个RadioButton:X.Y.Z) 选择X页面:指示器+ViewPager [要达成的效果]: (1)左侧选择A,进入X页 ...