1

我试图将一个 int 值传递给 Web 服务并期待回复。但我只在网络服务上获得价值。我的意思是它不接受我的输入值。安卓代码如下:

    package com.example.fp1_webservicedropdown;

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

import org.ksoap2.*;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.*;

public class MainActivity extends Activity {
    TextView result;

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

        result = (TextView) findViewById(R.id.textView2);

        final String NAMESPACE = "http://sample.com/";
        final String METHOD_NAME = "SayHello";
        final String SOAP_ACTION = "http://sample.com/SayHello";
        final String URL = "http://localip/HellowWorld/Service1.asmx";

        SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
        Request.addProperty("SayHello", "32");

        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();
            result.setText("The web service returned " + resultString);
            System.out.println(resultString);
        }

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

Web服务代码如下:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;

namespace HellowWorld
{
    [WebService(Namespace = "http://sample.com/")]
    public class Service1 : System.Web.Services.WebService
    {
        [WebMethod]
        public int SayHello(int a)
        {

            int t = 8 + a;
            return t;
        }
    }
}

它只是返回当我通过 android 运行时,Web 服务返回 8。Insted 给予 Web 服务返回 40。非常感谢任何帮助。

4

1 回答 1

1

Web 服务或 Web URL 是基于文本的格式。它不识别数据类型。它只接受文本..所以您必须作为字符串传递,并且必须从 Web 服务代码将其转换为 int。

在网络服务中使用它

public int SayHello(String abc)
        {
            // convert abc  to int
            int t = 8 + converted int;
            return t;
        }
于 2012-12-10T05:33:39.447 回答