Sending images using ksoap2 android

限于喜欢 提交于 2019-12-08 11:44:34

问题


I'm trying to send an .jpeg image from Android using Ksoap2 however everything that I've tried I get a error saying "Cannot Serialize" Here's what I'm doing:

    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();
    }

I've tried sending out, raw and encodedImage and all come back as some error. The out is actually what I believe it should look like and what I think my .Net webservice is expecting. The webservice is expecting:

      <myImage>base64Binary</myImage>

Any ideas? Thanks.


回答1:


Never mind I got it. This is the code I used:

    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();
    }

Then I had a procedure called:

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();
}

Code came from:http://lakshman39.wordpress.com/2012/09/08/sending-byte-to-a-webservice-using-ksoap2-in-android/

On the web server to turn it back into an image do this:

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


来源:https://stackoverflow.com/questions/15049631/sending-images-using-ksoap2-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!