在Android中使用Java Web服务 [英] Access java web service in Android

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

问题描述

我有一个问题,而使用kso​​ap2从Android应用程序中调用Java Web服务。我的web服务类有一个私有变量,我用的getter和放大器; setter方法​​来更新该变量。我想通过使用get方法来获取价值的Andr​​oid应用程序。我该怎么办呢?请帮帮我!我是新来编程。

我的Web服务类:

 公共类客户{
    私人字符串客户名称;    公共字符串getCustomerName(){
        返回客户名称;
    }    公共无效setCustomerName(字符串客户名称){
        this.customerName =客户名称;
    }

我用了一个演示类设置的客户名称值。但是使用仿真器上运行应用程序时,它并没有给这应该是价值。它显示开场仅默认消息

 包com.testversiontwo.ws;进口org.ksoap2.SoapEnvelope;
进口org.ksoap2.serialization.PropertyInfo;
进口org.ksoap2.serialization.SoapObject;
进口org.ksoap2.serialization.SoapPrimitive;
进口org.ksoap2.serialization.SoapSerializationEnvelope;
进口org.ksoap2.transport.HttpTransportSE;进口android.app.Activity;
进口android.os.Bundle;
进口android.util.Log;
进口android.widget.TextView;进口android.app.Activity;
进口android.os.Bundle;公共类TestVTwoClientActivity延伸活动{
    私有静态最后弦乐SOAP_ACTION =htt​​p://ws.customer.com;
    私有静态最后弦乐METHOD_NAME =getCustomerName;
    私有静态最后弦乐NAMESPACE =htt​​p://ws.customer.com/getCustomerName/;
    私有静态最终字符串URL =htt​​p://175.157.141.120:8085/TestVTwo/services/Customer?wsdl;    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);        SoapObject要求=新SoapObject空间(namespace,METHOD_NAME);        SoapSerializationEnvelope信封=新SoapSerializationEnvelope(SoapEnvelope.VER11);        envelope.setOutputSoapObject(请求);        HttpTransportSE HT =新HttpTransportSE(URL);
        尝试{
            ht.call(SOAP_ACTION,信封);
            SoapPrimitive响应=(SoapPrimitive)envelope.getResponse();
            Log.i(对myApp,response.toString());            TextView的电视=新的TextView(本);
            tv.setText(消息:+响应);
            的setContentView(电视);        }赶上(例外五){
            e.printStackTrace();
        }
    }
}


解决方案

OK!我已经解决了通过使用MySQL数据库和放这个问题;创建Android客户端来访问MySQL数据库!

I have a problem while calling a Java web service from an Android application using ksoap2. My web service class has a private variable, and I used getters & setters to update that variable. I want to get the value to the Android application through using the get method. How can I do that? Please help me! I'm new to programming.

My web service class:

public class Customer {
    private String customerName;

    public String getCustomerName() {
        return customerName;
    }

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

I used a demo class to set the customer name value. but when running the app using an emulator, it doesn't give the value which should be. It shows only the opening default message

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

解决方案

OK!! I have solved that problem by using a MySQL database & creating an Android client to access that MySQL database!!!

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

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