Android-remote server storage for data storage

Source: Internet
Author: User
<span id="Label3"></p><p><p>Cond....</p></p><p><p></p></p><p><p>public class Mainactivity extends Activity {</p></p><p><p>Private TextView tv;<br><br>@Override<br>protected void OnCreate (Bundle Savedinstancestate) {<br>Super.oncreate (savedinstancestate);<br>Setcontentview (r.layout.activity_main);<br>tv= (TextView) Findviewbyid (r.id.textview1);<br>}</p></p><p>public void Testget (View V) throws exception{<br><br>Final ProgressDialog dialog=progressdialog.show (this, null, "in Request ...");<br>New Thread () {<br>public void Run () {<br>String path= "http://192.168.56.1:8080/Web_Server/index.jsp?name=tom&age=23";<br>URL url;<br>try {<br>url = new URL (path);<br>HttpURLConnection huc= (httpurlconnection) url.openconnection ();<br>Huc.setrequestmethod ("GET");<br>Huc.setconnecttimeout (5000);<br>Huc.setreadtimeout (6000);<br>Huc.connect ();<br><br>Send Request<br>int Responsecode=huc.getresponsecode ();<br>If (responsecode==200) {<br>InputStream Is=huc.getinputstream ();<br>Bytearrayoutputstream baos=new Bytearrayoutputstream ();<br>byte[] buffer=new byte[1024];<br>int len=-1;<br>While ((len=is.read (buffer))!=-1) {<br>Baos.write (buffer, 0, len);<br>}<br>Final String reault=baos.tostring ();<br>Baos.close ();<br>Is.close ();<br><br>Updating the UI on the main thread<br>Runonuithread (new Runnable () {<br><br>@Override<br>public void Run () {<br>Tv.settext (reault);<br>Dialog.dismiss ();<br>}<br>});<br>Disconnect Connection<br>Huc.disconnect ();<br>}<br><br>} catch (Exception E) {<br>TODO auto-generated Catch block<br>E.printstacktrace ();<br>Dialog.dismiss ();<br>}<br><br><br>}<br>}.start ();<br><br><br><br>}<br><br>public void TestPost1 (View V) {<br>Final ProgressDialog dialog=progressdialog.show (this, null, "in Request ...");<br>New Thread () {<br>public void Run () {<br>String path= "http://192.168.56.1:8080/Web_Server/index.jsp";<br>URL url;<br>try {<br>url = new URL (path);<br>HttpURLConnection huc= (httpurlconnection) url.openconnection ();<br>Huc.setrequestmethod ("POST");<br>Huc.setconnecttimeout (5000);<br>Huc.setreadtimeout (6000);<br>Huc.connect ();<br>OutputStream Os=huc.getoutputstream ();<br>String data= "name=tom5&age=11";<br>Os.write (data.getbytes ("utf-8"));<br>Send Request<br>int Responsecode=huc.getresponsecode ();<br>If (responsecode==200) {<br>InputStream Is=huc.getinputstream ();<br>Bytearrayoutputstream baos=new Bytearrayoutputstream ();<br>byte[] buffer=new byte[1024];<br>int len=-1;<br>While ((len=is.read (buffer))!=-1) {<br>Baos.write (buffer, 0, len);<br>}<br>Final String reault=baos.tostring ();<br>Baos.close ();<br>Is.close ();<br><br>Updating the UI on the main thread<br>Runonuithread (new Runnable () {<br> <br>@Override<br>public void Run () {<br>Tv.settext (reault);<br>Dialog.dismiss ();<br>}<br>});<br>Disconnect Connection<br>Huc.disconnect ();<br>}<br><br>} catch (Exception E) {<br>TODO auto-generated Catch block<br>E.printstacktrace ();<br>Dialog.dismiss ();<br>}<br><br><br>}<br>}.start ();<br><br>}<br><br>public void TestGet2 (View V) throws exception{<br><br>Final ProgressDialog dialog=progressdialog.show (this, null, "in Request ...");<br>New Thread () {<br>public void Run () {<br>String path= "http://192.168.56.1:8080/Web_Server/index.jsp?name=tom&age=23";<br><br>try {<br>Creating a Client Object<br>HttpClient httpclient=new defaulthttpclient ();<br>Set timeout<br>Httpparams Params=httpclient.getparams ();<br>Httpconnectionparams.setconnectiontimeout (params, 5000);<br>Httpconnectionparams.setsotimeout (params, 5000);<br>Create Request Object<br>HttpGet request=new HttpGet (path);<br>Execute Request Object<br>HttpResponse Response=httpclient.execute (request);<br><br>int Statuscode=response.getstatusline (). Getstatuscode ();<br>If (statuscode==200) {<br>Get response text<br>Httpentity entity=response.getentity ();<br>Final String result=entityutils.tostring (entity);<br><br>Updating the UI on the main thread<br>Runonuithread (new Runnable () {<br><br>@Override<br>public void Run () {<br>Tv.settext (result);<br>Dialog.dismiss ();<br>}<br>});<br>}<br><br><br> <br>Disconnect Connection<br>Httpclient.getconnectionmanager (). shutdown ();<br>} catch (Exception E) {<br>TODO auto-generated Catch block<br>E.printstacktrace ();<br>Dialog.dismiss ();<br>}<br>}<br><br>}.start ();<br>}<br><br>public void TestPost2 (View V) {<br>Final ProgressDialog dialog=progressdialog.show (this, null, "in Request ...");<br>New Thread () {<br>public void Run () {<br>String path= "http://192.168.56.1:8080/Web_Server/index.jsp";<br><br>try {<br>Creating a Client Object<br>HttpClient httpclient=new defaulthttpclient ();<br>Set timeout<br>Httpparams Params=httpclient.getparams ();<br>Httpconnectionparams.setconnectiontimeout (params, 5000);<br>Httpconnectionparams.setsotimeout (params, 5000);<br>Create Request Object<br>HttpPost request=new HttpPost (path);<br>Set the request body<br>list<basicnamevaluepair> parameters=new arraylist<> ();<br>Parameters.Add (new Basicnamevaluepair ("name", "tom4"));<br>Parameters.Add (new Basicnamevaluepair ("age", "14"));<br>Httpentity entity=new urlencodedformentity (parameters);<br>Request.setentity (entity);<br><br>Execute Request Object<br>HttpResponse Response=httpclient.execute (request);<br><br>int Statuscode=response.getstatusline (). Getstatuscode ();<br>If (statuscode==200) {<br>Get response text<br>Httpentity entity2=response.getentity ();<br>Final String result=entityutils.tostring (entity2);<br><br>Updating the UI on the main thread<br>Runonuithread (new Runnable () {<br> <br>@Override<br>public void Run () {<br>Tv.settext (result);<br>Dialog.dismiss ();<br>}<br>});<br>}<br><br> <br><br>Disconnect Connection<br>Httpclient.getconnectionmanager (). shutdown ();<br>} catch (Exception E) {<br>TODO auto-generated Catch block<br>E.printstacktrace ();<br>Dialog.dismiss ();<br>}<br>}<br><br>}.start ();<br>}<br>}</p><p><p>Android-remote server storage for data storage</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.