. NET Programmer Android Learning path 3:post data to the Web API

Source: Internet
Author: User
<span id="Label3"></p>Original:. NET Programmer Android Learning path 3:post data to the Web API<p><p></p></p><p><p>In this example, we implement a real network interaction, post the data to the api, then receive the Server's return value for processing, and introduce custom types and legends of Ali's Fastjson.</p></p><p><p>Implementation ideas such as:</p></p><p><p>1. The data of receiving customer post on the API is restored to object, and the output is appended with a suffix for each attribute;</p></p><p><p>2. Enter the user name and password on the client to compare with the Server-side return;</p></p><p><p>We post to the server is name=mady&pwd=123, the server was added with the suffix Name=madya</p></p><p><p>&pwd=1231 so our client needs to enter Madya and 1231 to verify Success.</p></p><p><p>The specific operation shows as Follows:</p></p><p><p></p></p><p><p></p></p>first, Prepare the Api:<p><p>The current write API uses the asp. net WEB API2. Create an API project in VS2013 and configure him to support JSON first:</p></p><p><p>Open the Webapiconfig file in your project and include a configuration entry in the Register method:</p></p><pre><pre>Config. FORMATTERS.JSONFORMATTER.SUPPORTEDMEDIATYPES.ADD (<span style="color: #0000ff;">new</span> mediatypeheadervalue (<span style="color: #800000;">"</span><span style="color: #800000;">text/html</span> <span style="color: #800000;">"</span>));</pre></pre><p><p>Create entity classes such as:</p></p><pre><pre> <span style="color: #0000ff;"></span> public <span style="color: #0000ff;">class</span> <span style="color: #000000;">UserInfo { </span>public<span style="color: #0000ff;"></span><span style="color: #0000ff;">string</span><span style="color: #0000ff;">get</span><span style="color: #0000ff;">set</span><span style="color: #000000;">;} </span> <span style="color: #0000ff;"></span> public <span style="color: #0000ff;">string</span> <span style="color: #0000ff;">Get</span> <span style="color: #0000ff;">Set</span> <span style="color: #000000;">; } }</span></pre></pre><p><p>Open the Valuescontroller modify code as Follows:</p></p><pre><pre> <span style="color: #008000;">//</span> <span style="color: #008000;">POST api/values</span> <span style="color: #0000ff;"></span> public <span style="color: #000000;">UserInfo Post ([frombody]userinfo Value) { </span><span style="color: #0000ff;">return</span><span style="color: #0000ff;">new</span><span style="color: #800000;">"</span><span style="color: #800000;">a</span><span style="color: #800000;">"</span>, pwd = value.pwd+<span style="color: #800000;">"</span><span style="color: #800000;">1 </span><span style="color: #800000;">"</span><span style="color: #000000;"> }; }</span></pre></pre><p><p>You can open Fidder debugging until it succeeds:</p></p><p><p></p></p><p><p></p></p>second, the introduction of Ali's Fastjson Package<p><p>Just right-click to paste In:</p></p><p><p></p></p><p><p>This bag really works:</p></p><pre><pre>String json=json.tojsonstring (user); <span style="color: #008000;">//</span><span style="color: #008000;"></span>UserInfo userinfo=json.parseobject (json,userinfo. <span class="keyword"> class);</span> <span style="color: #008000;">//</span> <span style="color: #008000;">deserialization</span></pre></pre><p><p>The 2 sentences are all Done.</p></p><p><p>Let's go ahead and create a new Java entity class:</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">java.io.Serializable;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>UserInfo<span style="color: #0000ff;"><span style="color: #0000ff;">Implements</span></span><span style="color: #000000;"><span style="color: #000000;">serializable{</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">String name;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">String pwd;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">String getpwd () {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">pwd;}</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">setpwd (String Pwd) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>. PWD =<span style="color: #000000;"><span style="color: #000000;">pwd;}</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span><span style="color: #000000;"><span style="color: #000000;">String getName () {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">name;}</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">setName (String Name) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>. Name =<span style="color: #000000;"><span style="color: #000000;">name;}}</span></span></pre><p><p>Then modify the network access class, the last section is too shabby (see attached).</p></p><p><p>Then modify the async part Code:</p></p><pre><pre> <span style="color: #0000ff;">protected</span> <span style="color: #000000;">string Doinbackground (string ... params) {</span><span style="color: #008000;">//</span><span style="color: #008000;"> </span> UserInfo user=<span style="color: #0000ff;">new</span> <span style="color: #000000;"> UserInfo (); User.setname (</span>"mady"<span style="color: #000000;">); User.setpwd (</span>"123"<span style="color: #000000;">); String Str_json</span>=<span style="color: #000000;">json.tojsonstring (user); </span> <span style="color: #0000ff;">return</span> Httputils.sendpostmessage (params[0<span style="color: #000000;">],str_json); }</span></pre></pre><p><p>and the completion code:</p></p><pre> <span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onpostexecute (String Result) {TextView lblinfo</span></span>=<span style="color: #000000;"><span style="color: #000000;">(TextView) Findviewbyid (r.id.form_title); EditText Txt_login_name</span></span>=<span style="color: #000000;"><span style="color: #000000;">(EditText) Findviewbyid (r.id.txt_login_name); EditText Txt_login_pass</span></span>=<span style="color: #000000;"><span style="color: #000000;">(EditText) Findviewbyid (r.id.txt_login_pwd); String LoginName</span></span>=<span style="color: #000000;"><span style="color: #000000;">txt_login_name.gettext (). toString (). trim (); String Loginpass</span></span>=<span style="color: #000000;"><span style="color: #000000;">txt_login_pass.gettext (). toString (). trim (); UserInfo UserInfo</span></span>=json.parseobject (result,userinfo.<span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(loginpass.equals (userinfo.getpwd ()) &&<span style="color: #000000;"><span style="color: #000000;">loginname.equals (userinfo.getname ())) {lblinfo.settext (</span></span>"login successful!" "<span style="color: #000000;"><span style="color: #000000;">); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;">{lblinfo.settext (</span></span>"login failed!" "<span style="color: #000000;"><span style="color: #000000;">); } }</span></span></pre><p><p>By this completion, there is no change in the Unchanged.</p></p><p><p>Attach the new network access Class:</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span><span style="color: #000000;"><span style="color: #000000;">httputils{</span></span><span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;"> * </span></span><span style="color: #808080;"><span style="color: #808080;">@param</span></span><span style="color: #008000;"><span style="color: #008000;">Server URL address for path request *</span></span><span style="color: #808080;"><span style="color: #808080;">@param</span></span><span style="color: #008000;"><span style="color: #008000;">encode encoded format *</span></span><span style="color: #808080;"><span style="color: #808080;">@return</span></span><span style="color: #008000;"><span style="color: #008000;">converts the data returned from the server side into a string</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span><span style="color: #000000;"><span style="color: #000000;">string sendpostmessage (string path,string jsonstr) {string result</span></span>= ""<span style="color: #000000;"><span style="color: #000000;">; HttpClient HttpClient</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">defaulthttpclient (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Try</span></span><span style="color: #000000;"><span style="color: #000000;">{httppost HttpPost</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">HttpPost (path); Httppost.addheader (</span></span>"content-type", "text/json"<span style="color: #000000;"><span style="color: #000000;">); Httppost.setentity (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">stringentity (jsonstr)); HttpResponse HttpResponse</span></span>=<span style="color: #000000;"><span style="color: #000000;">Httpclient.execute (httppost); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(httpresponse.getstatusline (). getstatuscode () = =<span style="color: #000000;"><span style="color: #000000;">Httpstatus.sc_ok) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">LOG.E ("json+++++2:", jsonstr); Log info for debug</span></span>Httpentity httpentity =<span style="color: #000000;"><span style="color: #000000;">httpresponse.getentity (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(httpentity! =<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">) {result</span></span>= Entityutils.tostring (httpentity, "UTF-8"<span style="color: #000000;"><span style="color: #000000;">); } } } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Catch</span></span><span style="color: #000000;"><span style="color: #000000;">(Exception E) {e.printstacktrace (); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">finally</span></span><span style="color: #000000;"><span style="color: #000000;">{httpclient.getconnectionmanager (). shutdown (); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">result; }}</span></span></pre><p><p></p></p><p><p>. NET Programmer Android Learning path 3:post data to the Web API</p></p></span>

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.