如何从Android设备调用Web服务中的List <string>值

时间:2016-01-25 22:41:08

标签: java android ksoap2 glassfish-4 webservices-client

我在glassfish服务器上配置了一个简单的Web服务。它是localhost:4848。 Web服务配置完美,我可以通过addProduct()服务器的测试人员成功调用getProduct()glassfish方法。

现在我开发了一个Android设备应用程序,用于从我的Android设备或AVD调用我的Websevice。当我在AVD运行应用程序时,我无法得到任何东西或者无法向Webservice添加任何内容。 Manifest.xml INTERNET权限也已完成。

我需要在java的Webservice调用部分中提供有关如何进行调用的帮助。这是我的代码,请参阅并建议添加更多内容。谢谢

package com.example.bookwservice;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

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

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
private static final String         
SOAP_ACTION="http://javabrains.ali.org/addProduct";
private static final String METHOD_NAME="addProduct";
private static final String NAMESPACE="http://javabrains.ali.org/";
private static final String   
URL="http://ali:8080/TestWeb/ProductCatalogService";
TextView tv1, tv2, tv3;
EditText et1, et2,et3;
Button bt1, bt2, bt3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv1=(TextView)findViewById(R.id.textView1);
    tv2=(TextView)findViewById(R.id.textView2);
    tv3=(TextView)findViewById(R.id.textView3);

    et1=(EditText)findViewById(R.id.editText1);
    et2=(EditText)findViewById(R.id.editText2);
    et3=(EditText)findViewById(R.id.editText3);

    bt1=(Button)findViewById(R.id.button1);
    bt2=(Button)findViewById(R.id.button2);
    bt3=(Button)findViewById(R.id.button3);

    bt1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

    SoapObject request=new SoapObject(NAMESPACE, METHOD_NAME);
    request.addProperty("arg0",et1.getText().toString());


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

    AndroidHttpTransport aht=new AndroidHttpTransport(URL);
    try {
        aht.call(SOAP_ACTION, soapEnvelope);
        SoapPrimitive resultString=(SoapPrimitive)soapEnvelope.getResponse();

       tv1.setText("Status: " + resultString );
       }

    catch (Exception e){

        e.printStackTrace();
    }
        }});

    }
}

请查看测试结果。它在这里工作正常。我唯一想知道的,如何将List Values&#34;如下所示在SOAP响应中#34;到我的Android设备。

getProducts Method invocation


Method parameter(s)

Type   Value
java.lang.String   
books

Method returned

java.util.List : "[swatnama, yadona, ka ta raghali za ba gul sham, abc,  
newbook, newbook]"

SOAP Request

<?xml version="1.0" encoding="UTF-8"?><S:Envelope  
xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP- 
ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<S:Body>
<ns2:getProducts xmlns:ns2="http://javabrains.ali.org/">
    <arg0>books</arg0>
</ns2:getProducts>
</S:Body>
</S:Envelope>

SOAP Response

<?xml version="1.0" encoding="UTF-8"?><S:Envelope  
xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-
ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<S:Body>
<ns2:getProductsResponse xmlns:ns2="http://javabrains.ali.org/">
    <return>swatnama</return>
    <return>yadona</return>
    <return>ka ta raghali za ba gul sham</return>
    <return>abc</return>
    <return>newbook</return>
    <return>newbook</return>
</ns2:getProductsResponse>
</S:Body>
</S:Envelope>

0 个答案:

没有答案