当前位置: 移动技术网 > 移动技术>移动开发>Android > Android ksoap调用webservice批量上传多张图片详解

Android ksoap调用webservice批量上传多张图片详解

2019年07月24日  | 移动技术网移动技术  | 我要评论

android ksoap调用webservice批量上传多张图片详解

这几天一直在开发app,哎呀,什么都是第一接触,想想自己自学java,然后自学android,一直没有放弃,曾想放弃的,但是想到爸妈供我上学,不能在宿舍里面玩游戏,加入学校实验室,一天没课就来着里学习,当然这里也有志同道合的人,一起努力一起进步!虽然大学这几年都在努力的学习技术,也没有参加什么活动的,更别说找个女伴了!还是老老实实的敲代码,成功给我带来巨大的潜能,新技术总是吸引着我。自己做项目,哎呀!好像说偏题了,言归正传吧!前几天我们说了android怎么通过webservice上传图片到指定服务器上面,然而这远远不能满足需求,于是我就像怎么上传多张图片呀,想来想去,我就上网看了看,哎,都没有什么具体的代码,都是你抄我的,我抄你的,也许这就是中国人的习惯,自己不去验证代码的正确性就拿过来,真是误人子弟呀。

于是我一定要写着一片博文来弥补这个悲伤的空缺,让android开发者不在感到苦恼,让小伙伴们都找到信心。

下面就看我一步一步的写法吧!大家认真看,可能方法不是完美,金无足赤嘛,但是肯定是我自己一步一步的写出来的。

首先,小伙伴们一定要去了解java的线程池管理类,这个类比较重要。executorservice    

这个老哥讲的不错,了解executorservice之后,你看代码就比较轻松一点了

下面就是我的代码实现了

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开发还是很简单的只要你刻苦的去敲,多想想开发思路,多了解开发api有一天你会发现自己很牛逼,哈哈

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网