欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android ksoap調(diào)用webservice批量上傳多張圖片詳解

 更新時(shí)間:2017年02月24日 11:49:45   作者:厲害了我的江  
這篇文章主要介紹了Android ksoap調(diào)用webservice批量上傳多張圖片詳解的相關(guān)資料,需要的朋友可以參考下

Android ksoap調(diào)用webservice批量上傳多張圖片詳解

這幾天一直在開發(fā)app,哎呀,什么都是第一接觸,想想自己自學(xué)Java,然后自學(xué)Android,一直沒有放棄,曾想放棄的,但是想到爸媽供我上學(xué),不能在宿舍里面玩游戲,加入學(xué)校實(shí)驗(yàn)室,一天沒課就來(lái)著里學(xué)習(xí),當(dāng)然這里也有志同道合的人,一起努力一起進(jìn)步!雖然大學(xué)這幾年都在努力的學(xué)習(xí)技術(shù),也沒有參加什么活動(dòng)的,更別說(shuō)找個(gè)女伴了!還是老老實(shí)實(shí)的敲代碼,成功給我?guī)?lái)巨大的潛能,新技術(shù)總是吸引著我。自己做項(xiàng)目,哎呀!好像說(shuō)偏題了,言歸正傳吧!前幾天我們說(shuō)了Android怎么通過(guò)webservice上傳圖片到指定服務(wù)器上面,然而這遠(yuǎn)遠(yuǎn)不能滿足需求,于是我就像怎么上傳多張圖片呀,想來(lái)想去,我就上網(wǎng)看了看,哎,都沒有什么具體的代碼,都是你抄我的,我抄你的,也許這就是中國(guó)人的習(xí)慣,自己不去驗(yàn)證代碼的正確性就拿過(guò)來(lái),真是誤人子弟呀。

于是我一定要寫著一片博文來(lái)彌補(bǔ)這個(gè)悲傷的空缺,讓Android開發(fā)者不在感到苦惱,讓小伙伴們都找到信心。

下面就看我一步一步的寫法吧!大家認(rèn)真看,可能方法不是完美,金無(wú)足赤嘛,但是肯定是我自己一步一步的寫出來(lái)的。

首先,小伙伴們一定要去了解Java的線程池管理類,這個(gè)類比較重要。ExecutorService    點(diǎn)擊打開

這個(gè)老哥講的不錯(cuò),了解ExecutorService之后,你看代碼就比較輕松一點(diǎn)了

下面就是我的代碼實(shí)現(xiàn)了

private ExecutorService executorService;//定義一個(gè)線程池 

定義線程池的大小

executorService=Executors.newFixedThreadPool(5);//開啟5個(gè)線程,其實(shí)根據(jù)你的情況,一般不會(huì)超過(guò)8個(gè) 

線程啟動(dòng)

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

最后就是批量上傳圖片的方法了

public void getImageromSdk(){ 
  Log.i("進(jìn)入獲取圖片方法", "進(jìn)入獲取圖片方法"); 
  try{ 
   String srcUrl = "/sdcard/"; //路徑 
   String fileName = "1.png"; //文件名 
   String filrName2="2.jpg";//文件名 
  List<String>imageList=new ArrayList<>();//定義一個(gè)list,里面裝2個(gè)圖片地址,模擬批量上傳 
  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)); //進(jìn)行Base64編碼 
   String methodName = "uploadImage";   
    getImageFromAndroid(methodName,imageList.get(i), uploadBuffer); //調(diào)用webservice   
   Log.i("connectWebService", "start");  
   fis.close(); 
} 
  }catch(Exception e){ 
   e.printStackTrace(); 
  }   
 } 

最后就是提交soap方法了,這方法我都寫了幾百遍了

public String getImageFromAndroid(String arg0,String arg1, String arg2){ 
  Log.i("進(jìn)入端口方法", "進(jìn)入端口方法"); 
  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("請(qǐng)求", envelope.bodyIn.toString()); 
  } catch (IOException | XmlPullParserException e) { 
   e.printStackTrace(); 
  } 
  return arg1; 
 
 }; 

配置類

public class AgbcApi { 
 /** 
  * 服務(wù)器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/"; 
} 

其實(shí)Android開發(fā)還是很簡(jiǎn)單的只要你刻苦的去敲,多想想開發(fā)思路,多了解開發(fā)api有一天你會(huì)發(fā)現(xiàn)自己很牛逼,哈哈

相關(guān)文章

最新評(píng)論