使用kso​​ap2从Android发送图像到Web服务

时间:2011-06-08 14:57:27

标签: android axis2 tomcat6 ksoap2

我想使用相机拍摄的ksoap2库图像发送图像:

public class testwebservice extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    private static final String SOAP_ACTION="http://axis2.soap.webservice.vogella.de/getNumberResponse";
    private static final String METHOD_NAME="getNumberResponse";
    private static final String NAMESPACE="http://axis2.soap.webservice.vogella.de/";
    private static final String URL="http://192.168.0.178:8080/Randomnumber/services/RandomNumber.RandomNumberHttpEndpoint/";

    ImageView imv;
    TextView tv;
    Bitmap bmp;
    int i;
    final static int CAMERA_RESULT = 0;
    Button upload;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView)findViewById(R.id.text1);

        upload = (Button)findViewById(R.id.upload);

        upload.setOnClickListener(this);
        Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

        startActivityForResult(i,  CAMERA_RESULT);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if (resultCode == RESULT_OK)
        {
            Bundle extras = intent.getExtras();
            bmp = (Bitmap) extras.get("data");
            imv = (ImageView) findViewById(R.id.ReturnedImageView);
            imv.setImageBitmap(bmp);
        }
    }
}

然后我有AlertDialog询问我是否要发送图像,然后我调用使用Marshallbase64转换图像的实际函数:

public void testWebService() {
    MarshalBase64 b = new MarshalBase64();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    bmp.compress(CompressFormat.PNG, 100, out);
    byte[] imagebyte = out.toByteArray();

    SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
    Request.addProperty("image",imagebyte);

    SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    soapEnvelope.dotNet=true;
    soapEnvelope.setOutputSoapObject(Request);

    b.register(soapEnvelope);
    HttpTransportSE aht=new HttpTransportSE(URL);

    try
    {
        aht.call(SOAP_ACTION, soapEnvelope);
        SoapPrimitive response = (SoapPrimitive)soapEnvelope.getResponse();

        tv.setText("REZULTAT:"+response);       
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}

我使用Eclipse WTP和Axis2创建了一个Web服务,它是一个返回随机数的简单方法:

package de.vogella.webservice.soap.axis2;

import java.util.Random;

public class RandomNumber {
    public float[] getNumber(byte[] image){
        float u[] = new float[8];
        for(int i = 0; i < 8; i++) {
            u[i] = new Float(i);
            Random random = new Random();
            u[i]= random.nextFloat();
        }
        return u;
    }
}

我需要做的就是将这三个链接起来,我的工作就完成了。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:2)

您可以继续使用代码,而不是编组:

String strBase64 = Base64.encode(imagebyte);
Request.addProperty("image", strBase64); 

在您的webmethod解码字符串如下:

byte[] data=Base64.decode(image);

答案 1 :(得分:0)

您的问题中的确切地说您正在存储图像,这一点并不清楚。您显示的Web服务代码只返回一个数字,不支持HTTP POST请求。在我看来,您需要调整Web服务以接受此类请求并将编组后的字节数组存储在数据库中。因此,下一个逻辑步骤是创建用于存储这些图像的数据库,然后更新您的Web服务以连接到此DB并插入图像数据。最后,您的Android应用程序将调用图像编组代码,然后使用生成的字节数组进行webservice调用。