Android连接Asp.net Web服务参数

时间:2015-04-24 09:24:03

标签: android asp.net web-services ksoap2

我使用ASP.NET创建了一个Web服务,然后我在使用Android应用程序连接到它时遇到了问题。 我的代码中的Hello world方法正常工作,因为它没有参数,但方法add不是。

// ASP.NET代码

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代码

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

}

}

}

跟踪请求之后就像这样

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

,回复是

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

2 个答案:

答案 0 :(得分:0)

尝试这样的事情:

替换以下代码:

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

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

因为“x”定义您在web服务中为参数“x”发送值

答案 1 :(得分:0)

我通常以这种方式使用它,所有都经过测试和清洁: 如果Web结果是一个对象 CallServerObject.java ,如string或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;
        }

    }    

如果服务器像列表一样返回SoapObject CallServerSopaObject ,我会使用它:

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

现在当我需要连接到服务器时,对于每个asp.net函数 CheckCode.java 我有这个代码:

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

}

现在我已经在一个地方收集了所有名称和网址,并按照以下方式定义:

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

检查这种方式,如果有任何错误让我知道并希望它可能会有所帮助。