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

私のWebサービスコードは次のとおりです。

 [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 に答える