在Android中访问java Web服务

时间:2012-02-13 14:29:20

标签: java android ksoap2

使用kso​​ap2从Android应用程序调用Java Web服务时遇到问题。我的Web服务类有一个私有变量,我使用了getters& setters更新该变量。我想通过使用get方法获得Android应用程序的价值。我怎样才能做到这一点?请帮我!我是编程新手。

我的网络服务类:

public class Customer {
    private String customerName;

    public String getCustomerName() {
        return customerName;
    }

    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }

我使用了一个演示类来设置客户名称值。但是当使用模拟器运行应用程序时,它不会给出应该的值。它仅显示打开的默认消息

package com.testversiontwo.ws;

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

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import android.app.Activity;
import android.os.Bundle;

public class TestVTwoClientActivity extends Activity {
    private static final String SOAP_ACTION = "http://ws.customer.com";
    private static final String METHOD_NAME = "getCustomerName";
    private static final String NAMESPACE = "http://ws.customer.com/getCustomerName/";
    private static final String URL = "http://175.157.141.120:8085/TestVTwo/services/Customer?wsdl";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);          

        SoapSerializationEnvelope envelope = new     SoapSerializationEnvelope(SoapEnvelope.VER11);

        envelope.setOutputSoapObject(request);

        HttpTransportSE ht = new HttpTransportSE(URL);
        try {
            ht.call(SOAP_ACTION, envelope);
            SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
            Log.i("myApp", response.toString());

            TextView tv = new TextView(this);
            tv.setText("Message :"+response);
            setContentView(tv);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
} 

3 个答案:

答案 0 :(得分:0)

我认为您的网址必须是:

private static final String URL = "http://175.157.141.120:8085/TestVTwo/services/Customer";

答案 1 :(得分:0)

好的,我已经意识到getCustomerName会返回一个字符串,所以你必须这样做:

SoapObject response = (SoapObject)envelope.getResponse();
TextView tv = new TextView(this);
tv.setText("Message :" + response.toString());
setContentView(tv);

如果仍有错误,请发布异常堆栈跟踪。

答案 2 :(得分:0)

行!!我已经通过使用MySQL数据库解决了这个问题。创建一个Android客户端来访问MySQL数据库!!!