Android HttpClient POST JSON restful-web-services
Last Update:2015-08-14
Source: Internet
Author: User
<span id="Label3"></p><pre><span style="color: #000000;"><span style="color: #000000;">@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span><span style="color: #000000;"><span style="color: #000000;">string Doinbackground (string ... arg0) {gson Gson</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Gson (); String JSON</span></span>=<span style="color: #000000;"><span style="color: #000000;">Gson.tojson (map); HttpPost HttpPost</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">HttpPost (WR. url_yjfk); String result</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Try</span></span><span style="color: #000000;"><span style="color: #000000;">{stringentity Entity</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">stringentity (json, HTTP. utf_8); Entity.setcontenttype (</span></span>"application/json"<span style="color: #000000;"><span style="color: #000000;">); Httppost.setentity (entity); HttpClient Client</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">defaulthttpclient (); HttpResponse Response</span></span>=<span style="color: #000000;"><span style="color: #000000;">Client.execute (httppost); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(response.getstatusline (). getstatuscode () = =<span style="color: #000000;"><span style="color: #000000;">Httpstatus.sc_ok) {inputstream</span> in</span>=<span style="color: #000000;"><span style="color: #000000;">response.getentity (). getcontent (); Result</span></span>=<span style="color: #000000;"><span style="color: #000000;">strutil.readstring (in); } } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Catch</span></span><span style="color: #000000;"><span style="color: #000000;">(unsupportedencodingexception E) {e.printstacktrace (); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Catch</span></span><span style="color: #000000;"><span style="color: #000000;">(clientprotocolexception E) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Catch block</span></span><span style="color: #000000;"><span style="color: #000000;">E.printstacktrace (); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Catch</span></span><span style="color: #000000;"><span style="color: #000000;">(ioexception E) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Catch block</span></span><span style="color: #000000;"><span style="color: #000000;">E.printstacktrace (); } </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>Android HttpClient POST JSON restful-web-services</p></p></span>