简体   繁体   中英

Android connecting Asp.net web service parameters

I have created a web service using ASP.NET then I'm having a problem in connecting to it using android app. The method Hello world in my code work correctly because it has no arguments but the method add doesn't.

//ASP.NET code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;

/// <summary>
/// Summary description for WebService
/// </summary>

[WebService(Namespace = "http://ahmadezzat.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {
    SqlConnection connection = new SqlConnection();
    public WebService () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public int HelloWorld()
    {
        return 5;
    }

    [WebMethod]
    public int echo(int x) {
        return x;
    }

}

Android code

package com.me;

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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class SignUp extends Activity {
private static final String SOAP_ACTION = "http://ahmadezzat.com/echo";
private static final String METHOD_NAME = "echo"; 
private static final String NAMESPACE = "ServiceReference1"; 
private static final String urll = "http://10.0.2.2:51295/WebSite1/WebService.asmx";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sign_up);

Button button = (Button) findViewById(R.id.signUp);
final TextView tv = (TextView) findViewById(R.id.textView1);

button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String result = callWebService(22);
tv.setText("the result is " + result);
}
});
}

public String callWebService(int x) {
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

//request.addProperty("msg", "1");

PropertyInfo pi = new PropertyInfo();
        pi.setName("x");
        pi.setValue(x);
        pi.setType(int.class);
        request.addProperty(pi);

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);

envelope.dotNet = true; 
envelope.setOutputSoapObject(request);

HttpTransportSE androidHttpTransport = new HttpTransportSE(urll);
androidHttpTransport.debug = true;
//return androidHttpTransport.requestDump + " / " + androidHttpTransport.responseDump; 
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive res = (SoapPrimitive) envelope.getResponse();

String v = res.toString() + "\n";
v += androidHttpTransport.requestDump + "\n" + androidHttpTransport.responseDump;
return v;

} catch (Exception E) {
return E.toString();

}

}

}

After tracing the request it was like this

<v:Body>
<echo xmlns="ServiceReference1" id="o0" c:root="1">
<x i:type="d:int">22</x>
<echo>
<v:/Body>

and the response is

<Soap:Body>
<echoResponse xmlns="http://www.ahmadezzat.com"/>
<echoResult>0</echoResult>
<echoResponse>
<Soap:/Body>

Try something like this:

replace your below code:

PropertyInfo pi = new PropertyInfo();
        pi.setName("msg");
        pi.setValue(x);
        pi.setType(int.class);
        request.addProperty(pi);

with

PropertyInfo pi = new PropertyInfo();
pi.setName("x");
pi.setValue(x);
pi.setType(String.class);
request.addProperty(pi);

because "x" defines that you are sending value for parameter "x" in your webservice

I usually use it this way, all are tested and clean : If web result is an object CallServerObject.java like string or int:

    import java.util.List;
    import org.apache.http.NameValuePair;
    import org.ksoap2.SoapEnvelope;
    import org.ksoap2.serialization.SoapObject;
    import org.ksoap2.serialization.SoapSerializationEnvelope;
    import org.ksoap2.transport.HttpTransportSE;
    import com.ebrahimi.helper.Constants;
    public class CallServerObject {
        private static String PAGE_NAME;
        public static void setPAGE_NAME(String pageName) {
            PAGE_NAME = pageName;
        }

        private static String METHOD_NAME;
        public static void setMETHOD_NAME(String methodName) {
            METHOD_NAME = methodName;
        }

        private static List<NameValuePair> PARAMS;
        public static void setPARAMS(List<NameValuePair> params) {
            PARAMS = params;
        }

        public Object GetObject() {
            SoapObject request = new SoapObject(Constants.MAIN_URL, METHOD_NAME);
            for (NameValuePair prop : PARAMS) {
                request.addProperty(prop.getName(), prop.getValue());
            }

            SoapSerializationEnvelope envelope = getSoapSerializationEnvelope(request);
            envelope.setOutputSoapObject(request);
            HttpTransportSE androidHttpTransport = getHttpTransportSE(Constants.MAIN_URL+PAGE_NAME);
            Object javab = null;
            try {
                androidHttpTransport.call(Constants.MAIN_URL+METHOD_NAME, envelope);
                javab = (Object)envelope.getResponse();
            } catch (Exception e) {
                javab = e.getLocalizedMessage();
            }
            return javab;
        }

        private final static SoapSerializationEnvelope getSoapSerializationEnvelope(SoapObject request) {
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.implicitTypes = true;
            envelope.setAddAdornments(false);
            envelope.setOutputSoapObject(request);   
            return envelope;
        }

        private final static HttpTransportSE getHttpTransportSE(String MAIN_REQUEST_URL) {
            HttpTransportSE ht = new HttpTransportSE(MAIN_REQUEST_URL);
            ht.debug = true;
            ht.setXmlVersionTag("<?xml version=\"1.0\" encoding= \"UTF-8\"?>");    
            return ht;
        }

    }    

and if server return SoapObject CallServerSopaObject like a list I use this :

    import java.util.List;
    import org.apache.http.NameValuePair;
    import org.ksoap2.SoapEnvelope;
    import org.ksoap2.serialization.SoapObject;
    import org.ksoap2.serialization.SoapSerializationEnvelope;
    import org.ksoap2.transport.HttpTransportSE;

    import com.ebrahimi.helper.Constants;

    public class CallServerSopaObject {

        private static String PAGE_NAME;
        public static void setPAGE_NAME(String pageName) {
            PAGE_NAME = pageName;
        }

        private static String METHOD_NAME;
        public static void setMETHOD_NAME(String methodName) {
            METHOD_NAME = methodName;
        }

        private static List<NameValuePair> PARAMS;
        public static void setPARAMS(List<NameValuePair> params) {
            PARAMS = params;
        }

        public SoapObject GetObject() {
            SoapObject request = new SoapObject(Constants.MAIN_URL, METHOD_NAME);
            for (NameValuePair prop : PARAMS) {
                request.addProperty(prop.getName(), prop.getValue());
            }

            SoapSerializationEnvelope envelope = getSoapSerializationEnvelope(request);
            envelope.setOutputSoapObject(request);
            HttpTransportSE androidHttpTransport = getHttpTransportSE(Constants.MAIN_URL+PAGE_NAME);
            SoapObject javab = null;
            try {
                androidHttpTransport.call(Constants.MAIN_URL+METHOD_NAME, envelope);
                javab = (SoapObject)envelope.getResponse();
            } catch (Exception e) {
                e.getLocalizedMessage();
            }
            return javab;
        }

        private final static SoapSerializationEnvelope getSoapSerializationEnvelope(SoapObject request) {
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.implicitTypes = true;
            envelope.setAddAdornments(false);
            envelope.setOutputSoapObject(request);   
            return envelope;
        }

        private final static HttpTransportSE getHttpTransportSE(String MAIN_REQUEST_URL) {
            HttpTransportSE ht = new HttpTransportSE(MAIN_REQUEST_URL);
            ht.debug = true;
            ht.setXmlVersionTag("<?xml version=\"1.0\" encoding= \"UTF-8\"?>");    
            return ht;
        }
    }

Now when I need to connect to server, for each asp.net function CheckCode.java I have this code :

import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import com.ebrahimi.list.BasketListView;
import com.ebrahimi.server.CallServerObject;
import com.ebrahimi.server.Server;
import com.ebrahimi.server.Shoper;
import com.ebrahimi.shiriniyazd.R;
import android.app.ProgressDialog;
import android.os.AsyncTask;

public class CheckCode {

    private ProgressDialog pDialog;
    private Object javab = null;
    private String code;
    private String cell;

    public BasketListView context;

    public CheckCode (BasketListView act, 
                        String code, 
                        String cell) {
        this.context = act;
        this.code    = code;
        this.cell    = cell;
    }

    public void callCheckRegister(){
        new callDics().execute();
    }

    private class callDics extends AsyncTask<String, String, Object > {

        @Override
        protected Object doInBackground(String... params) {
            List<NameValuePair> params1 = new ArrayList<NameValuePair>();
            CallServerObject list = new CallServerObject();
            CallServerObject.setPAGE_NAME(  Server.PageCommons.PageNameShoper);
CallServerObject.setMETHOD_NAME(Shoper.Methods.MethodCheckUserByGUID);

            params1.add(new BasicNameValuePair("userName", cell));
            params1.add(new BasicNameValuePair("code"    , code));
            params1.add(new BasicNameValuePair("getImage", "true"));

            CallServerObject.setPARAMS(params1);

publishProgress(context.getString(R.string.msg_wait_connecting));
            javab = list.GetObject();
            return javab;
        }

        @Override
        protected void onProgressUpdate(String... progress) {
            super.onProgressUpdate(progress);
            if (pDialog != null) {
                pDialog.setMessage(progress[0]);
            }
        }

        @Override
        protected void onPreExecute() {
            pDialog = new ProgressDialog(context);
            pDialog.setMessage(context.getString(R.string.msg_wait));
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected void onPostExecute(Object javab) {
            pDialog.dismiss();
            if (isCancelled()) {
            }
        }
    }

}

Now I have collected all names and urls in one place and define them like this :

    public static String MAIN_URL = "http://ahmadezzat.com/";
    public static final String PageName = "myPaageName.asmx";
    public static String MethodCheckUserByGUID = "helloWorldMethod";

Check this way, if any things wrong let me know and hope it may help.

  • You just have one problem in the Code: User this namespace " http://ahmadezzat.com/ " instead of "ServiceReference1"

    private static final String NAMESPACE = " http://ahmadezzat.com/ ";

  • If you tried to test the android code over android version 4.* it will case exception "NetworkOnMainThreadException" and don't work well with you. So you need to user AsyncTask to avoid that.

developer.android.com/reference/android/os/AsyncTask.html

Android Code after editing:

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.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class SignUp extends Activity {
    private static final String SOAP_ACTION = "http://ahmadezzat.com/echo";
    private static final String METHOD_NAME = "echo";
    private static final String NAMESPACE = "http://ahmadezzat.com/";
    private static final String urll = "http://10.0.2.2:51295/WebSite1/WebService.asmx";

    private TextView tv;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sign_up);

        Button button = (Button) findViewById(R.id.signUp);
        tv = (TextView) findViewById(R.id.textView1);

        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                new CallWebService().execute(22);
            }
        });
    }

    class CallWebService extends AsyncTask<Integer, Void, String> {

        @Override
        protected String doInBackground(Integer... params) {
            try {
                SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

                PropertyInfo pi = new PropertyInfo();
                pi.setName("x");
                pi.setValue(params[0]);
                pi.setType(int.class);
                request.addProperty(pi);

                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                        SoapEnvelope.VER11);

                envelope.dotNet = true;
                envelope.setOutputSoapObject(request);

                HttpTransportSE androidHttpTransport = new HttpTransportSE(urll);
                androidHttpTransport.debug = true;
                androidHttpTransport.call(SOAP_ACTION, envelope);
                SoapPrimitive res = (SoapPrimitive) envelope.getResponse();

                String v = res.toString() + "\n";
                v += androidHttpTransport.requestDump + "\n"
                        + androidHttpTransport.responseDump;
                return v;

            } catch (Exception E) {
                return E.toString();

            }
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            tv.setText(result);
        }

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM