Android tool connected .net WebService class code

Andrews Code: 

Import android.os.Handler;
Import android.os.Message;


Import org.ksoap2.SoapEnvelope;
Import org.ksoap2.SoapFault;
Import org.ksoap2.serialization.SoapObject;
Import org.ksoap2.serialization.SoapSerializationEnvelope;
Import org.ksoap2.transport.HttpTransportSE;

public class WebServiceUtils {
public interface the CallBack {
void Result (String Result);
}
// parameters, the serviceUrl is a webservice .net address, is generally achieved http: // IP address: port number / XXX.asmx
// methodName is the name of the interface on the WebService
// okparams is passed as a parameter passed as a string, pass Json can first converted to a string, the server then parsed
// callBack callback, obtain the value returned
    static void the getResult public (the serviceUrl Final String, String methodName Final, Final String okparams, the CallBack Final callBack) { 
Handler // child thread for communicating with the main thread
Final Handler Handler mHandler new new = () {
@Override
public void the handleMessage (the Message MSG) {
super.handleMessage (MSG);
// the return value of the callback to the parameter callBack
callBack.result ((String) msg.obj);
}
};
new new the Thread (the Runnable new new () {
@Override
public void RUN ( ) {

the try {
namespace on the method name //.net + webwervice
The soapAction = String "http://tempuri.org/" methodName +;
// parameter name space, the method name
SoapObject request = new SoapObject ( "http://tempuri.org/ ", methodName);
parameters passed // attention key parameter names to be consistent with the method of .net
request.addProperty ( "okparams", okparams);
SoapSerializationEnvelope new new Envelope = SoapSerializationEnvelope (SoapEnvelope.VER11);
envelope.bodyOut = Request;
HttpTransportSE HT = new new HttpTransportSE (the serviceUrl);
HT .call (the soapAction, Envelope);
Object Object;
IF (! envelope.getResponse () = null) {
= Object (Object) envelope.getResponse ();
// SoapObject soapObject = (SoapObject) envelope.getResponse ();
// use the result to the main thread Handler
mHandler.sendMessage (mHandler.obtainMessage (1, object.toString ( ) ));

}
IF (envelope.bodyIn the instanceof the SoapFault) {
the SoapFault error = (the SoapFault) envelope.bodyIn;
// use Handler exception message is sent to the main thread
mHandler.sendMessage (mHandler.obtainMessage (0, error.toString ( ) ));
}

} the catch (Exception EX)
{
mHandler.sendMessage(mHandler.obtainMessage(-1, ex.getMessage()));
}

}
}).start();
}
}



//使用实例

WebServiceUtils.getResult (parameter serviceUrl, methodName, passing, new new WebServiceUtils.CallBack () {
@Override
public void Result (String Result) {
// Return Value
String RES = Result;

}
});


//.Net part of the code

/// <Summary>
/// Test Description Summary
/// </ Summary>
[the WebService (the Namespace = "http://tempuri.org/")]
[the WebServiceBinding (conformsTo = WsiProfiles.BasicProfile1_1)]
[the System. ComponentModel.ToolboxItem (false)]
// To call this allows the use of ASP.NET AJAX Web service from a script, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Test: the System.Web.Services.WebService
{

     [WebMethod]

    public string HelloWorld(string okparams)
   {

        return "Hello World";
  }

  

 

}







Guess you like

Origin www.cnblogs.com/dosoftwarey/p/11328833.html