在Android的web服务的SOAP [英] Webservice SOAP in Android

查看:130
本文介绍了在Android的web服务的SOAP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用Web服务在Android和我跟着这个教程:的创建Web服务在Java中使用Apache Axis2和Eclipse的来创建和使用web服务。本教程介绍了客户端应用程序在Java中,但与其他一些教程,我能够消耗WS Android中是这样的:

I'm starting to use webservices in Android and I followed this tutorial: Create Web Service in Java Using Apache Axis2 and Eclipse to create and consume the webservice. This tutorial explain the client side application in Java but with some others tutorials I was able to consume the WS in Android like this:

package com.android.webservices;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AddNumbersWSActivity extends Activity {
/** Called when the activity is first created. */

private String METHOD_NAME = "addTwoNumbers"; // our webservice method name
private String NAMESPACE = "http://sencide.com"; // Here package name in webservice with reverse order.
private String SOAP_ACTION = "http://sencide.com/addTwoNumbers"; // NAMESPACE + method name
private static final String URL = "http://192.168.1.214:8080/axis2/services/FirstWebService?wsdl"; // you must use ipaddress here, don’t use Hostname or localhost

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button addButton = (Button) findViewById(R.id.button1);

    addButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            try
            {
                SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
                EditText num1 = (EditText) findViewById(R.id.editText1);
                EditText num2 = (EditText) findViewById(R.id.editText2);
                request.addProperty("firstNumber", Integer.valueOf(num1.getText().toString()));
                request.addProperty("secondNumber", Integer.valueOf(num2.getText().toString()));
                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
//                  envelope.dotNet = true;
                    envelope.setOutputSoapObject(request);
                    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
                    androidHttpTransport.call(SOAP_ACTION,envelope);
                    Object result = envelope.getResponse();
                    System.out.println("Result : " + result.toString());
                    ((TextView) findViewById (R.id.textView4)).setText(result.toString());
                } catch (Exception E) {
                    E.printStackTrace();
                }
            }
        });

    }
}

但现在我有关于Android的消费WS一些问题:

But now I have some questions about consuming WS in Android:

  1. 在我所看到的<一个href="https://$c$c.google.com/p/ksoap2-android/downloads/list?can=1&q=&colspec=Filename%20Summary%20Uploaded%20ReleaseDate%20Size%20DownloadCount"相对=nofollow>此处,该kSOAP2库德precated ......我应该继续使用这个库或者我需要另一种工具?如果是的话,哪一个?
  2. 是否有可能知道名称空间 METHOD_NAME SOAP_ACTION 使用该服务的只有WSDL?
  3. 可以产生一些code自动使用WSDL(如所使用的Java应用程序的WSDL2Java工具)?
  4. 方法addProperty(字符串名称,对象的值)的方法,我可以用任意的名称还是应该遵循一个规则?
  1. As I can see here, the kSOAP2 library is deprecated... Should I continue to use this library or I need another tool? If yes, which one?
  2. Is it possible to know the NAMESPACE, METHOD_NAME, SOAP_ACTION using only the WSDL of the service?
  3. Can I generate some code automatically using WSDL (like WSDL2Java tool that is used for Java applications)?
  4. At the addProperty(String name, Object value) method, can I used arbitrary name or it should follow a rule?

在此先感谢!

推荐答案

Apache Axis是用于Java的,不以Android平台工作。 Ksoap2图书馆是一个替代,这和大多使用的工具在Android的消费web服务。对于您的问题:

Apache Axis is for Java and do not work in Android platform. Ksoap2 library is an alternate for this and mostly used tool to consuming webservices in Android. For your questions:

1 - 今天[2012年5月15日]我可以看到的这里

1 - Today [15.5.2012] i can see the last commit only 4 days ago in here

2 - 是的,它是可能的。命名空间描述类似如下:

2 - Yes it is possible. The namespace describes like as follow:

<wsdl:definitions targetNamespace="http://tempuri.org/">

和方法(S)姓名(或名称)描述了类似如下:

And method(s) name(s) describes like as follow:

<s:element name="YourMethodName">

和SOAP动作genarally由NAMESPACE + METHOD_NAME;

And SOAP action is genarally consist NAMESPACE + METHOD_NAME;

3 - 据我所知,没有写得很好的项目,现在

3 - As I know, there is no well written project for now.

4 - 它应该遵循的规则必须是马赫到WSDL的属性名称和大小写

4 - It should follow a rule that must be mach to WSDL's attributes name and case sensitivity

这篇关于在Android的web服务的SOAP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆