Universal Android calls WebService method-parameter types are unrestricted

Source: Internet
Author: User

Note: This is just an example. The scalability and reusability are poor. I just put forward an idea and the returned XML parsing code is also poorly written.
You can solve these problems when using code.
========================================================== ===
Key code:

Try {
// Post machine principle, simulating Browser
Final string server_url = "http: // 10.40.15.11/Android/WebService. asmx"; // define the source address of the content to be obtained

URL url = new URL (server_url );
Urlconnection con = URL. openconnection ();
Con. setdooutput (true );
Con. setrequestproperty ("Pragma:", "No-Cache ");
Con. setrequestproperty ("cache-control", "No-Cache ");
Con. setrequestproperty ("Content-Type", "text/XML ");

Outputstreamwriter out = new outputstreamwriter (con
. Getoutputstream ());
// Control Value
Edittext etextname = (edittext) findviewbyid (R. Id. tbx_name );
Edittext etextage = (edittext) findviewbyid (R. Id. tbx_age );
String xmlinfo = "<? XML version = \ "1.0 \" encoding = \ "UTF-8 \"?> <Soap12: envelope xmlns: xsi = \ "comment" xmlns: XSD = \ "http://www.w3.org/2001/xmlschema\" xmlns: soap12 = \ "comment"> <soap12: body> <showentity xmlns = \ "http://tempuri.org/\"> <person> <Name>"
+ Urlencoder. encode (etextname. gettext (). tostring ())
+ "</Name> <age>"
+ Etextage. gettext (). tostring ()
+ "</Age> </person> </showentity> </soap12: Body> </soap12: envelope> ";
// Send
Out. Write (new string (xmlinfo. getbytes ("UTF-8 ")));
Out. Flush ();
Out. Close ();
// Return Value
Bufferedreader BR = new bufferedreader (New inputstreamreader (con
. Getinputstream ()));
Stringbuilder sbuilder = new stringbuilder ();
String line = "";
For (line = Br. Readline (); line! = NULL; line = Br. Readline ()){
Sbuilder. append (line );
}
// Parse XML
Pattern patternname = pattern. Compile ("<Name> .*? </Name> ");
Matcher matchername = patternname. matcher (sbuilder. tostring ());
If (matchername. Find ()){
String name = matchername. Group ();
Textview lblname = (textview) findviewbyid (R. Id. lbl_name );
Lblname. settext (urldecoder. Decode (name. substring (name
. Indexof (">") + 1, name. lastindexof ("<"))));
}

Pattern patternage = pattern. Compile ("<age> .*? </Age> ");
Matcher matcherage = patternage. matcher (sbuilder. tostring ());
If (matcherage. Find ()){
String age = matcherage. Group ();
Textview lblage = (textview) findviewbyid (R. Id. lbl_age );
Lblage. settext (age. substring (age. indexof (">") + 1, age
. Lastindexof ("<")));
}

} Catch (exception e ){
String STR = E. getmessage ();
}

The xmlinfo variable is the place where the red lines are circled.

Server: C # code

[Webmethod]
Public Person showentity (person)
{
Return person;
}

Publicclass person
{
Publicstring name {Get; set ;}

Publicint age {Get; set ;}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.