Yoa*_*oav 5 upload android soap
如何使用SOAP将图像文件上传到Web服务?
我需要它与SoapObejct一起使用,因此webservice可以在其上下文中处理输入文件并为其提供新的文件名.
怎么样?任何代码示例?
约阿夫
将您的图像文件转换为Base64字符串,并将您的字符串及其名称轻松发送到Web服务.你还需要代码样本吗?
编辑
public static String fileToBase64(String path) throws IOException {
byte[] bytes = Utilities.fileToByteArray(path);
return Base64.encodeBytes(bytes);
}
public static byte[] fileToByteArray(String path) throws IOException {
File imagefile = new File(path);
byte[] data = new byte[(int) imagefile.length()];
FileInputStream fis = new FileInputStream(imagefile);
fis.read(data);
fis.close();
return data;
}
public class MyImage {
public String name;
public String content;
}
Run Code Online (Sandbox Code Playgroud)
将您的对象作为JSON字符串发送到Web服务:
在你的活动中:
MyClass myClass = new MyClass();
myClass.name = "a.jpg";
myClass.content = fileToBase64("../../image.jpg");
sendMyObject(myClass);
private void sendMyObject(
MyImage myImage ) throws Exception {
// create json string from your object
Gson gson = new Gson();
String strJson = gson.toJson(myImage);
//send your json string here
//...
}
Run Code Online (Sandbox Code Playgroud)
在您的web服务中,将您的json字符串转换为真实对象,该对象是MyClass的副本.
编辑:
您也可以忽略Json并使用带有2个参数的webserivce方法:MyWebserviceMethod(string filename, string content);
将Base64 string作为第二个参数传递.
| 归档时间: |
|
| 查看次数: |
8090 次 |
| 最近记录: |