android - 使用 ksoap2 从 Android 发送图像到网络服务

标签 android apache-axis 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 创建了一个网络服务,这是一个返回随机数的简单方法:

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

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

最佳答案

您可以继续编写代码,而不是编码:

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

在你的网络方法中解码字符串如下:

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

关于android - 使用 ksoap2 从 Android 发送图像到网络服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6280862/

相关文章:

java - 轴2 : Avoid "Unexpected subelement" error when non-required property added to WSDL

java - Windows 与 Tomcat 的集成安全性

tomcat - 如何使用 Nginx 进行负载均衡?

android - 动态设置微调器的选择,不触发 OnItemSelectedListener

java - 为什么在从字符串创建 JSONObject 时得到一个空对象?

javascript - Phonegap 构建 Android : ReferenceError

java - 用于实现 Android 延迟加载的 cglib 替代方案

java - Axis2 Web 服务和 Eclipse WSDL 创作 : Adding an axis fault to wsdl causes exception when running wsdl2java

java - 从简单的 java 客户端调用 Web 服务

java - Tomcat 6 中的 Quartz 调度程序,线程不会停止