0

我正在尝试使用 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();
    }

我已经尝试发送原始和编码图像,但都以错误的形式返回。out 实际上是我认为它应该看起来的样子,也是我认为我的 .Net web 服务所期望的。网络服务期望:

      <myImage>base64Binary</myImage>

有任何想法吗?谢谢。

4

1 回答 1

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)
于 2013-02-25T00:28:24.860 回答