使用kso​​ap2 android发送图像

时间:2013-02-24 07:27:52

标签: android web-services android-ksoap2

我正在尝试使用Ksoap2从Android发送.jpeg图像但是我尝试的所有内容都会出现错误,说“无法序列化”这就是我正在做的事情:

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.JPEG, 100, out);
    byte[] raw = out.toByteArray();
    String encodedImage = Base64.encodeToString(raw, Base64.DEFAULT);

    SoapObject request = new SoapObject("http://tempuri.org/", "sendImage");
    request.addProperty("image", out);       
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);     
    envelope.setOutputSoapObject(request);
    envelope.dotNet = true;

    try {
        Toast.makeText(getApplicationContext(), "Sending Pic", Toast.LENGTH_LONG).show();
        HttpTransportSE androidHttpTransport = new HttpTransportSE("http://www.letstrend.com/spursService.asmx?WSDL");
        androidHttpTransport.call("http://tempuri.org/sendImage", envelope);
        SoapObject result = (SoapObject)envelope.bodyIn;
    } catch (Exception e) {
        e.printStackTrace();            
        Toast.makeText(getApplicationContext(), "in catch e=" + e.getMessage(), Toast.LENGTH_LONG).show();
    }

我已经尝试过发送,raw和encodedImage,所有这些都会出现一些错误。 out实际上是我认为它应该是什么样子以及我认为我的.Net webservice期待的东西。网络服务期待:

      <myImage>base64Binary</myImage>

有什么想法吗?感谢。

1 个答案:

答案 0 :(得分:3)

没关系,我明白了。这是我使用的代码:

    byte[] bytearray = null;
    try
    {
        is = new FileInputStream(_path);
        if(_path != null)
            try{
                bytearray = streamToBytes(is);                  
            }finally{
                is.close();
            }
    }catch (Exception e)
    {}

    SoapObject request = new SoapObject("http://tempuri.org/", "sendImage");        
    request.addProperty("myImage", bytearray);

    SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
    new MarshalBase64().register(envelope);
    envelope.dotNet = true; 
    envelope.setOutputSoapObject(request);

    try {
        Toast.makeText(getApplicationContext(), "Sending Pic", Toast.LENGTH_LONG).show();
        Toast.makeText(getApplicationContext(), "array length=" + bytearray.length, Toast.LENGTH_LONG).show();
        HttpTransportSE androidHttpTransport = new HttpTransportSE("http://www.letstrend.com/spursService.asmx?WSDL");
        androidHttpTransport.call("http://tempuri.org/sendImage", envelope);
        SoapObject result = (SoapObject)envelope.bodyIn;
    } catch (Exception e) {
        e.printStackTrace();            
        Toast.makeText(getApplicationContext(), "in catch e=" + e.getMessage(), Toast.LENGTH_LONG).show();
        Toast.makeText(getApplicationContext(), "fault=" + ((SoapFault) envelope.bodyIn).faultstring, Toast.LENGTH_LONG).show();
    }

然后我有一个叫做的程序:

public static byte[] streamToBytes(InputStream is) {
    ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
    byte[] buffer = new byte[1024];
    int len;
    try {
        while ((len = is.read(buffer)) >= 0) {
            os.write(buffer, 0, len);
        }
    } catch (java.io.IOException e) {
    }
    return os.toByteArray();
}

代码来自:http://lakshman39.wordpress.com/2012/09/08/sending-byte-to-a-webservice-using-ksoap2-in-android/

在Web服务器上将其重新转换为图像,执行以下操作:

Dim FilePath As String = Server.MapPath("~/folder/")
System.IO.File.WriteAllBytes(FilePath & "filename.jpg", myImage)