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

本文详细介绍了如何在Android中利用ksoap库调用webservice实现图片的批量上传。首先创建一个固定大小的线程池,然后通过循环将图片读取为字节数组并进行Base64编码,再通过调用webservice的方法进行上传。关键步骤包括文件读取、Base64编码、soap请求的构造和发送。
摘要由CSDN通过智能技术生成

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

发布时间:2020-11-24 15:54:16

来源:亿速云

阅读:104

作者: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";//文件名

ListimageList=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实现图片上传功能,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值