Web Service upload Parameter code instance
This time to do the project with more webservice, the beginning of the online look at the reference dome, found that no, and later found that Android 4.0 has a very big difference, in doing the communication, some things need to note:
First, the namespace: Consistent with the server, the namespace suffix must not add "/";
Second, the method name: consistent with the server;
Third, URL: is the server address does not add back? = ... ;
The code is as follows:
- Package com.example.web;
- Import java.util.ArrayList;
- Import Org.ksoap2.SoapEnvelope;
- Import Org.ksoap2.serialization.SoapObject;
- Import Org.ksoap2.serialization.SoapSerializationEnvelope;
- Import Org.ksoap2.transport.HttpTransportSE;
- Import Android.annotation.SuppressLint;
- Import Android.annotation.TargetApi;
- Import android.app.Activity;
- Import Android.os.Build;
- Import Android.os.Bundle;
- Import Android.os.StrictMode;
- Import Android.view.View;
- Import Android.view.View.OnClickListener;
- Import Android.widget.Button;
- Import Android.widget.TextView;
- @SuppressLint ("Newapi")
- @TargetApi (build.version_codes. Gingerbread) @SuppressLint ("Newapi") public class Mainactivity extends Activity {
- public static final String TAG = "WEBSERVICE_PJ";
- Button button;
- TextView Resultview;
- String result;
- String Str_userno = "144";
- @TargetApi (build.version_codes. Gingerbread) @SuppressLint ("Newapi")
- @TargetApi (build.version_codes. Gingerbread) @SuppressLint ("Newapi") @Override
- protected void OnCreate (Bundle savedinstancestate) {
- Super.oncreate (savedinstancestate);
- Setcontentview (R.layout.activity_main);
- Forcing actions in the UI thread
- Strictmode.setthreadpolicy (New StrictMode.ThreadPolicy.Builder ()
- . Detectdiskreads ()
- . Detectdiskwrites ()
- . Detectnetwork ()
- . Penaltylog ()
- . build ());
- Strictmode.setvmpolicy (New StrictMode.VmPolicy.Builder ()
- . Detectleakedsqlliteobjects ()
- . Detectleakedclosableobjects ()
- . Penaltylog ()
- . Penaltydeath ()
- . build ());
- Resultview = (TextView) Findviewbyid (R.id.textview);
- Button = (button) Findviewbyid (R.id.butt);
- Button.setonclicklistener (New Onclicklistener () {
- @Override
- public void OnClick (View arg0) {
- TODO auto-generated Method Stub
- String nameSpace = "";
- Called Method name
- String methodName = "";
- String url = "Http://i.cnblogs.com/EditPosts.aspx";
- String soapaction = "nameSpace+methodName";
- arraylist<string> params= new arraylist<string> ();
- Params.add ("Str_userno");
- Params.add ("Str_sex");
- Params.add ("Str_telephone");
- Params.add ("Str_pwd");
- arraylist<string> vals= new arraylist<string> ();
- Vals.add ("33");
- Vals.add ("1");
- Vals.add ("23442");
- Vals.add ("1443");
- New MyThread (Namespace,methodname,url,soapaction, Params,vals). Start ();
- Displays the results returned by WebService in TextView
- Resultview.settext ("DFSD" +getresult ());
- }
- });
- }
- Public String GetResult () {
- return result;
- }
- Private class MyThread extends Thread
- {
- Private String NameSpace;
- Private String MethodName;
- Private String SOAPAction;
- Private String URL;
- Private arraylist<string> params;
- Private arraylist<string> Vals;
- Public MyThread (String nameSpace, String methodName,
- String URL, string soapaction,arraylist<string> params,arraylist<string> vals) {
- This.namespace = NameSpace;
- This.methodname = MethodName;
- This.url = URL;
- This.soapaction = SOAPAction;
- This.params = params;
- This.vals = Vals;
- }
- @Override
- public void Run ()
- {
- result= Getremoteinfo (nameSpace, methodName, URL,
- Soapaction,params,vals);
- }
- }
- public string Getremoteinfo (string nameSpace, String methodName,
- String URL, string soapaction,arraylist<string> params,arraylist<string> vals) {
- Specifies the namespace of the WebService and the method name of the call
- Soapobject RPC = new Soapobject (NameSpace, methodName);
- Set the two parameters that need to be passed in to call the WebService interface Mobilecode, UserId
- //
- for (int i = 0; i < params.size (); i++) {
- Rpc.addproperty (params.get (0), Vals.get (0));
- Rpc.addproperty (Params.get (1), Vals.get (1));
- Rpc.addproperty (Params.get (2), Vals.get (2));
- Rpc.addproperty (Params.get (3), Vals.get (3));
- SYSTEM.OUT.PRINTLN (RPC);
- Rpc.addproperty ("Content-type", "text/xml; Charset=utf-8 ");
- Rpc.addproperty ("Content-length", "Length");
- //}
- Generates SOAP request information that calls the WebService method and specifies the version of Soap
- Soapserializationenvelope envelope =new soapserializationenvelope (SOAPENVELOPE.VER10);
- Envelope.bodyout = RPC;
- Envelope.setoutputsoapobject (RPC);
- Envelope.bodyin = RPC;
- Envelope.dotnet = true;
- Httptransportse transport = new Httptransportse (URL);
- Httptransportse transport=new httptransportse (URL);//20-second limit
- try {
- Call WebService
- Transport.call (Soapaction,envelope);
- System.out.println ("haha");
- if (Envelope.getresponse ()!=null) {
- Soapobject object = (soapobject) envelope.bodyin;
-
- if (Object! = null) {
- Gets the returned result
- result =object.getproperty (0). toString ();
- System.out.println ("FAF" +result);
- }
- }
- } catch (Exception e) {
- E.printstacktrace ();
- System.out.println ("Yghu" +result);
- }
- Get the returned data
- return result;
- }
- }
Write bad place hope the big God guidance, catch chicken not into, hey, hope to everyone useful!
Web Service upload parameter code instance