0

我正在尝试将值从 android 传递到 aspx Web 服务。但是 Web 服务不采用以下代码中传递的 32。

Request.addProperty("SayHello", "32");

任何帮助将不胜感激。

我完整的android代码如下

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://myLoclalIP/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);
        }

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

我的网络服务代码如下:

 [WebService(Namespace = "http://sample.com/")]
    public class Service1 : System.Web.Services.WebService
    {
        [WebMethod]
        public  String SayHello(int a)
        {
            int passingValue = 8 + a;
            String aTemp=passingValue.ToString();
            return aTemp;
        }


    }
4

2 回答 2

0

Request 类的文档中没有任何方法作为 addProperty 。尽管您可以将 URI 构造为字符串并通过构造函数或使用setDestinationUri(uri)方法传递它。

例如,

uri = "http://www.mydomain.com/index?SayHello=32"
my_request.setDestinationUri(uri)

为了获得更多控制,还有一种方法可以自己设置 HTTP 标头。

于 2012-12-10T07:12:50.397 回答
0

这是示例...

package com.mehul.Web_Service;

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

import android.R.string;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class Web_ServiceActivity extends Activity 
{
    //SoapObject request; 
    Button ok,btnimage;
    EditText eprice;
    TextView tprice;


    private final String NAMESPACE = "http://www.webserviceX.NET/";
    private final String URL = "http://www.webservicex.net/stockquote.asmx";
    private final String SOAP_ACTION = "http://www.webserviceX.NET/GetQuote";
    private final String METHOD_NAME = "GetQuote";

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

        ok = (Button)findViewById(R.id.btnok);
        eprice = (EditText)findViewById(R.id.edtprice);
        tprice = (TextView)findViewById(R.id.txtprice);
        Typeface typface=Typeface.createFromAsset(getAssets(),"fonts/Gretoon.ttf");
        tprice.setTypeface(typface);

        ok.setOnClickListener(new OnClickListener() 
        {

            public void onClick(View v) 
            {
                    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
                    PropertyInfo weightProp =new PropertyInfo();
                    weightProp.setName("symbol");

                    weightProp.setValue(eprice.getText().toString());

                    weightProp.setType(string.class);
                    request.addProperty(weightProp);


                    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                    envelope.dotNet = true;
                    envelope.setOutputSoapObject(request);
                    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

                    try 
                    {
                        androidHttpTransport.call(SOAP_ACTION, envelope);
                        //SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
                        SoapObject obj1 = (SoapObject)envelope.bodyIn;
                        //Log.i("myApp", response.toString());


                        //tv.setText(weight+" "+fromUnit+" equal "+response.toString()+ " "+toUnit);

                        String str = obj1.toString();
                        Log.d("My Price", ""+str);
                        int start = str.indexOf("<Last>")+("<Last>").length();
                        int end = str.indexOf("</Last>");

                        tprice.setText(""+str.substring(start,end)+" Price");
                        Toast.makeText(getApplicationContext(), "Set Price Successfully .. "+str.substring(start,end), Toast.LENGTH_SHORT).show();


                       request = null;

                    } 
                    catch (Exception e) 
                    {
                        //e.printStackTrace();
                        Log.d("Exception Generated", ""+e.getMessage());
                    }

            }
        });

        //String weight = "ITC";
        //String fromUnit = "Grams";
        //String toUnit = "Kilograms";



        /*PropertyInfo fromProp =new PropertyInfo();
        fromProp.setName("FromUnit");
        fromProp.setValue(fromUnit);
        fromProp.setType(String.class);
        request.addProperty(fromProp);

        PropertyInfo toProp =new PropertyInfo();
        toProp.setName("ToUnit");
        toProp.setValue(toUnit);
        toProp.setType(String.class);
        request.addProperty(toProp);
        */ 



    }


}
于 2012-12-10T09:30:16.447 回答