温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

在Android中使用ksoap调用webservice实现图片上传功能

发布时间:2020-11-24 15:54:16 来源:亿速云 阅读:181 作者:Leah 栏目:移动开发

本篇文章给大家分享的是有关在Android中使用ksoap调用webservice实现图片上传功能,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

代码实现

private ExecutorService executorService;//定义一个线程池 

定义线程池的大小

executorService=Executors.newFixedThreadPool(5);//开启5个线程,其实根据你的情况,一般不会超过8个 

线程启动

executorService.execute(new Runnable() { 
    
   @Override 
   public void run() { 
    getImageromSdk(); 
   } 
  }); 

最后就是批量上传图片的方法了

public void getImageromSdk(){ 
  Log.i("进入获取图片方法", "进入获取图片方法"); 
  try{ 
   String srcUrl = "/sdcard/"; //路径 
   String fileName = "1.png"; //文件名 
   String filrName2="2.jpg";//文件名 
  List<String>imageList=new ArrayList<>();//定义一个list,里面装2个图片地址,模拟批量上传 
  imageList.add(fileName); 
  imageList.add(filrName2);   
  for (int i = 0; i < imageList.size(); i++) { 
   FileInputStream fis = new FileInputStream(srcUrl + imageList.get(i)); 
   ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
   byte[] buffer = new byte[4096]; 
   int count = 0; 
   while((count = fis.read(buffer)) >= 0){ 
    baos.write(buffer, 0, count); 
   } 
   String uploadBuffer = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT)); //进行Base64编码 
   String methodName = "uploadImage";   
    getImageFromAndroid(methodName,imageList.get(i), uploadBuffer); //调用webservice   
   Log.i("connectWebService", "start");  
   fis.close(); 
} 
  }catch(Exception e){ 
   e.printStackTrace(); 
  }   
 } 

最后就是提交soap方法了,这方法我都写了几百遍了

public String getImageFromAndroid(String arg0,String arg1, String arg2){ 
  Log.i("进入端口方法", "进入端口方法"); 
  final String methodName="getImageFromAndroid"; 
  final String soapAction=AgbcApi.NAMESPACE+methodName; 
  request = new SoapObject(AgbcApi.NAMESPACE, methodName); 
  request.addProperty("arg0",arg1);  
  request.addProperty("arg1",arg2); 
  SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);  
  (new MarshalBase64()).register(envelope);   
  envelope.bodyOut = request; 
  envelope.dotNet=false; 
  envelope.setOutputSoapObject(request);   
  HttpTransportSE ht = new HttpTransportSE(AgbcApi.TASKSERVICEURL); 
  ht.debug=true;  
  try { 
 
   ht.call(soapAction, envelope); 
   Log.i("请求", envelope.bodyIn.toString()); 
  } catch (IOException | XmlPullParserException e) { 
   e.printStackTrace(); 
  } 
  return arg1; 
 
 }; 

配置类

public class AgbcApi { 
 /** 
  * 服务器ip 
  */ 
 private static String IP="http://10.123.42.138:8080/fff"; 
 public static String TASKSERVICEURL=IP+"TaskService"; 
  
 public static String NAMESPACE="http://iservice.gbc.com/"; 
} 

以上就是在Android中使用ksoap调用webservice实现图片上传功能,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI