实现Android客户端接收Web Services服务端传输的多张图片并显示的功能。
Web Services 服务端:
@WebService
public interface ITransPic {
@WebResult(name = "getPictures")
public FileDto transPic(@WebParam(name="folderName") String folderName);
}
注明:因为是测试代码,入参folderName在后面并未使用。
public class TransPicImpl implements ITransPic {
ArrayList
FileInputStream fis = null; //文件流对象
PicFile picFile = null; //文件图片对象
ByteArrayOutputStream bos = null;
File file = null;
byte[] buffer = null;
FileDto fileDto = new FileDto();
String filePath = "F:\\Desktop\\pictures";
@Override
public FileDto transPic(String folderName) {
// TODO Auto-generated method stub
fileList = new ArrayList
try {
file = new File(filePath);
File[] array = file.listFiles();
System.out.println("共有"+array.length+"张图片");
//读取每张图片
for (int i = 0; i < array.length; i++){
picFile = new PicFile();
fis = new FileInputStream(filePath+"\\"+(i+1)+".jpg");
bos = new ByteArrayOutputStream();
buffer = new byte[8092];
int count = 0;
while((count = fis.read(buffer)) > 0){
bos.write(buffer, 0, count);
}
String downloadBuffer = new String(Base64.encode(bos.toByteArray()));
picFile.setFileName((i+1)+".jpg");
picFile.setDownloadBuffer(downloadBuffer);
fileList.add(picFile);
}
fileDto.setFileLenth(array.length);
fileDto.setFileList(fileList);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return fileDto;
}
}
其中FileDto的代码为:
public class FileDto {
private ArrayList
private int fileLenth;
@XmlElementWrapper(name = "files")
@XmlElements(@XmlElement(name = "file", type = PicFile.class))
public ArrayList
return fileList;
}
public void setFileList(ArrayList
this.fileList = fileList;
}
public int getFileLenth() {
return fileLenth;
}
public void setFileLenth(int fileLenth) {
this.fileLenth = fileLenth;
}
}
PicFile包括fileName(文件名)与downloadBuffer两个属性。
服务发布后的wsdl为:
在浏览器中输入调用方法transPic的URL:http://localhost:8080/WSPic/TransPicService/transPic?folderName=1,显示:
可以看到files就是@XmlElementWrapper的名字,其下有10个以“file”命名的PicFile对象。利用@XmlElementWrapper、@XmlElements等注释,可以帮助Android客户端解析xml格式的数据。
其中,每个file的xml数据为:
说明:由于Java WebService传入参数的特殊性(虽然定义的参数是Java支持的参数,但是通过链接传入的参数的类型都为String),所以图片需要通过Base64编码,转化为String类型。
Android客户端:
需要的jar包:commons-codec-1.7.jar以及Ksoap的jar包。
public class WebService {
private final String NAME_SPACE = "http://service.yan.cn/";
private final String URL = "http://192.168.10.200:8080/WSPic/TransPicService";
private final String GET_METHOD_NAME = "transPic";
private final String GET_RESPONSE_OPERATION="http://192.168.10.200:8080/WSPic/TransPicService/transPic";
private ArrayList
private PicFile picFile = null;
private int fileNumber = 0;
public ArrayList
SoapObject rpc = new SoapObject(NAME_SPACE,GET_METHOD_NAME);
rpc.addProperty("folderName","12");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = rpc;
envelope.setOutputSoapObject(rpc);
AndroidHttpTransport ht = new AndroidHttpTransport(URL);//用http传输,wsdl地址
ht.debug = true;//允许调试
try {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("调用call之前时间:"+df.format(new Date()));
ht.call(GET_RESPONSE_OPERATION, envelope);
System.out.println("调用call之后时间:"+df.format(new Date()));
System.out.println("envelop=="+envelope);
SoapObject detail =(SoapObject)envelope.getResponse();
SoapObject result = (SoapObject)detail.getProperty("files");
System.out.println("fileNumber初始值=="+fileNumber);
fileNumber = result.getPropertyCount();
System.out.println("fileNumber=="+fileNumber);
//一次取出fileNumber张图片
for (int i = 0; i < fileNumber; i ++){
SoapObject tempObject = (SoapObject)result.getProperty(i);
picFile = new PicFile();
picFile.setDownloadBuffer(tempObject.getProperty("downloadBuffer").toString());
picFile.setFileName(tempObject.getProperty("fileName").toString());
picFileList.add(picFile);
}
} catch (Exception e) {
e.printStackTrace();
}
return picFileList;
}
}
至此,picFileList已包含所有图片的信息。若要显示图片,用Base64解码即可:
for (int i = 0; i < filePicList.size(); i++){
buffer = new Base64().decode(filePicList.get(i).getDownloadBuffer());
imgeViewList.get(i).setImageBitmap(BitmapFactory.decodeByteArray(buffer, 0, buffer.length));
}