There are many ways in which the Android client interacts with background data. Here is a note today, interacting with WebService data. Create a new simple webservice by creating the following method:
The creation of a good project is like this.
Let's write a few simple code in it, as follows:
In this way, our webservice is written, and then we publish. Right-click the project and select Publish.
Select File System, here I publish directly to the local IIS inside.
So the release is over, and then we open the local IIS directly.
Right-click Login.asmx. Click Browse to open it in the default browser.
The address here is more important.
So our webservice is over. Next is the Android client. Here we first need a jar package. Here I use ksoap2:http://www.oschina.net/p/ksoap2+android download, we put the jar package into the Libs, then right-click, in the Pop-up menu click on the Add as Library.
Then pop the window and click OK.
Now let's start writing code. A simple layout page was written first.
The code is as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:paddingbottom= "@dimen/activity_vertical_margin"
android:paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_margin"
Tools:context= ". Mainactivity ">
<edittext
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:inputtype= "Textpersonname"
Android:hint= "Account"
Android:ems= "10"
Android:id= "@+id/accountext"
android:layout_margintop= "34DP"
Android:layout_alignparenttop= "true"
android:layout_alignstart= "@+id/pwdtext"/>
<edittext
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:inputtype= "Textpassword"
Android:hint= "Password"
Android:ems= "10"
Android:id= "@+id/pwdtext"
android:layout_margintop= "24DP"
android:layout_below= "@+id/accountext"
Android:layout_centerhorizontal= "true"/>
<button
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Login"
Android:id= "@+id/button"
android:layout_below= "@+id/pwdtext"
Android:layout_centerhorizontal= "true"
android:layout_margintop= "36DP"/>
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Content"
Android:id= "@+id/textview"
android:layout_below= "@+id/button"
android:layout_margintop= "24DP"
Android:layout_alignparentstart= "true"
Android:layout_alignparentend= "true"
Android:layout_alignparentbottom= "true"/>
</RelativeLayout>
Then the main activity code is as follows:
There are several points of note in the following code NAMESPACE, Myurl, METHODNAME. Where namespace corresponds to the above we WebService namespace, here I am the default namespace.
The address of the myurl corresponds not only to the current WebService address, but to the specific address of the reference method as stated above. METHODNAME is the method name that is called
Package com.example.administrator.login1;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.os.Message;
Import Android.util.Log;
Import Android.view.View;
Import Android.widget.Button;
Import Android.widget.EditText;
Import Android.widget.TextView;
Import Org.ksoap2.SoapEnvelope;
Import Org.ksoap2.serialization.SoapObject;
Import Org.ksoap2.serialization.SoapSerializationEnvelope;
Import Org.ksoap2.transport.HttpTransportSE;
public class Mainactivity extends Activity {
Private EditText Accountet,pwdet;
Private Button loginbtn;
Private TextView TextView;
Private MyHandler MyHandler =new MyHandler ();
Private final String namespace= "http://tempuri.org/"; WebService Server Namespace
Private final String myurl= "Http://172.18.216.7/login/login.asmx?op=login1"; Detailed address of the method
Private final String methodname= "Login1"; The specific method name
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Initview ();
Login button
Loginbtn.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (View v) {
New Thread (New Runnable () {
@Override
public void Run () {
try {
Soapobject request = new Soapobject (namespace,methodname);
Request.addproperty ("Name", Accountet.gettext (). toString ());
Request.addproperty ("pwd", Pwdet.gettext (). toString ());
Soapserializationenvelope envelope = new Soapserializationenvelope (SOAPENVELOPE.VER11);
Envelope.bodyout = Request;
Envelope.dotnet = true;
Envelope.setoutputsoapobject (Request);
Httptransportse ht = new Httptransportse (Myurl);
Ht.call (namespace+methodname, envelope);
Return object is expected to be objects otherwise a conversion exception error occurs
Object soapobject = (object) envelope.getresponse ();
Send Message Update UI
Message MSG2 = new Message ();
Bundle bundle = new bundle ();
Bundle.putstring ("Result", soapobject.tostring ());
Msg2.what = 1;
Msg2.setdata (bundle);
Myhandler.sendmessage (MSG2);
}catch (Exception e) {
LOG.E ("Lyf", e.tostring ());
E.printstacktrace ();
}
}
}). Start ();
}
});
}
private void Initview ()
{
accountet= (EditText) This.findviewbyid (R.id.accountext);
pwdet= (EditText) This.findviewbyid (R.id.pwdtext);
LOGINBTN = (Button) This.findviewbyid (R.id.button);
textview= (TextView) This.findviewbyid (R.id.textview);
}
Class MyHandler extends Handler {
@Override
public void Handlemessage (Message msg) {
Switch (msg.what) {
Case 1:
Update TextView Show return results
Textview.settext (Msg.getdata (). getString ("result"));
Break
}
Super.handlemessage (msg);
}
}
}
Android uses ksoap2-android to access WebService (C #)