Android关于web services的使用

一、什么是web services

WebService是一个SOA(面向服务的编程)的架构,它是不依赖于语言,不依赖于平台,可以实现不同的语言间的相互调用,通过Internet进行基于Http协议的网络应用间的交互。 WebService实现不同语言间的调用,是依托于一个标准,webservice是需要遵守WSDL(web服务定义语言)/SOAP(简单请求协议)规范的。 WebService=WSDL+SOAP+UDDI(webservice的注册) Soap是由Soap的part和0个或多个附件组成,一般只有part,在part中有Envelope和Body。 Web Service是通过提供标准的协议和接口,可以让不同的程序集成的一种SOA架构。
Web Service的优点
(1) 可以让异构的程序相互访问(跨平台)
(2) 松耦合 (3) 基于标准协议(通用语言,允许其他程序访问) Web Service的基本原理 (1) Service Provider采用WSDL描述服务
(3) Service Provider 采用UDDI将服务的描述文件发布到UDDI服务器(Register server)
(4) Service Requestor在UDDI服务器上查询并 获取WSDL文件
(5) Service requestor将请求绑定到SOAP,并访问相应的服务。

以上是比较正式的说法,看不懂的没关系,我再说下我的个人理解
webservices是一种跨平台技术,比如我用android写客户端,用idea去写服务器端,然后通过webservices让两者连接起来,具体做法如下:在idea中创建一个web services程序,然后在这个新创建的界面写程序,之后生成一个wsdl文件,这个文件是一个类似于XML的东西,在客户端android中其实我们首先打开的是这个wsdl文件,然后去查找我们要调用的是服务器中的哪个方法,之后可以在客户端获取这个方法执行过后返回的值。

二、怎么使用

1 在Idea中创建web services
在这里插入图片描述
2 当java文件写完之后需要自动生成wsdL文件
在这里插入图片描述
再附上webservices创建成功的示例:启动服务,输入网址如果出现这个界面就说明创建成功了
在这里插入图片描述

再附上web services的全部代码,功能是实现文件的上传和下载

package example;

import org.apache.axis.encoding.Base64;

import java.io.*;

public class HelloWorld {

  public String uploadFile(String filename, String filestring) {
    FileOutputStream fos = null;
    try{
      String toDir = "D:\\work\\image";   //存储路径
      byte[] buffer = Base64.decode(filestring);   //对android传过来的图片字符串进行解码
      File destDir = new File(toDir);
      if(!destDir.exists()) {
        destDir.mkdirs();
      }
      if(filename.equals("face.txt"))
      {
        //FileInputStream fis = new FileInputStream(new File(destDir,filename));
        fos = new FileOutputStream(new File(destDir,filename),true);
      }
      else
      {
        fos = new FileOutputStream(new File(destDir,filename));   //保存图片
      }
      fos.write(buffer);
      fos.flush();
      fos.close();
      return "上传图片成功!" + "图片路径为:" + toDir;
    }catch (Exception e){
      e.printStackTrace();
    }
    return "上传图片失败!";
  }

  public String downloadFileOne(String path) {
    try {
      FileInputStream fis = new FileInputStream(path);
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      byte[] buffer = new byte[1024];
      int count = 0;
      //先生成字节流,然后再用Base64编码
      while ((count = fis.read(buffer)) >= 0) {
        baos.write(buffer, 0, count);
      }
      String uploadBuffer = new String(Base64.encode(baos.toByteArray()));  //进行Base64编码
      return uploadBuffer;
    }catch (IOException e) {
      e.printStackTrace();
    }
    return null;
  }

  public String downloadFile() {
    String toDir = "D:\\work\\image";   //存储路径
    File file=new File(toDir);
    File[] files=file.listFiles();
    String downBuffer = downloadFileOne(files[0].getAbsolutePath()) + "sparklujing" + files[0].getName() + "mingzispark";
    //List<String> s = new ArrayList<>();
    for(int i =1;i<files.length;i++){
//            Log.e("path:",files[i].getAbsolutePath().toString());
//            Log.e("path:",files[i].getName().toString());
      downBuffer = downBuffer + downloadFileOne(files[i].getAbsolutePath()) + "sparklujing" + files[i].getName() + "mingzispark";
    }
    return downBuffer;
  }
}

3 客户端如何调用
这时先做准备工作,要导入一个jar包
ksoap2-android-assembly-3.1.0-jar-with-dependencies
版本可以不同,清读者到网上自行下载

导入成功之后粘一下我调用的代码

String fileName = Environment.getExternalStorageDirectory().getPath()+"/Android/data/com.arcsoft.sdk_demo/cache/"; //图片保存sd地址
String namespace = "http://example";  // 命名空间
String url = "http://192.168.137.1:8080//services//HelloWorld?WSDL";  //对应的wsdl
String  methodName = "uploadFile";                  //调用的webservices的方法
            SoapObject soapObject = new SoapObject(namespace, methodName);
            soapObject.addProperty("filename", name);
            soapObject.addProperty("filestring", fileBuffer);
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = false;
            envelope.setOutputSoapObject(soapObject);
            HttpTransportSE httpTranstation = new HttpTransportSE(url);
            try {
                httpTranstation.call(namespace, envelope);
                Object result = (Object) envelope.getResponse();
                Message msg = Message.obtain();
                msg.arg1 = 2;
                Bundle data = new Bundle();
                data.putString("hh", (String) result.toString());
                msg.setData(data);
                handler.sendMessage(msg);
                Log.i("connectWebService", result.toString());
            } catch (Exception e) {
                Log.e("error", e.toString());
            }

在此特别需要强调一下,调用Ksoap的时候必须开起线程要不然会报错

发布了34 篇原创文章 · 获赞 2 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_38420342/article/details/86361308