Android vs. server Get or post interaction data
Last Update:2015-05-20
Source: Internet
Author: User
<span id="Label3"></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> package</span><span style="color: #000000;"><span style="color: #000000;">mydemo.mycom.demo2.service;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">java.io.InputStream;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">java.io.OutputStream;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">java.net.HttpURLConnection;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">java.net.URL;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">mydemo.mycom.demo2.utils.StreamTools;</span></span><span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* Created by Administrator on 2015/5/20.</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;">class</span></span><span style="color: #000000;"><span style="color: #000000;">Netservice {</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 Loginbyget (string username,string Password) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Try</span></span><span style="color: #000000;"><span style="color: #000000;">{String Path</span></span>= "http://192.168.1.110:1010/UserInfo/Login?username=" +username+ "&password=" +<span style="color: #000000;"><span style="color: #000000;">password; URL URL</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">URL (path); HttpURLConnection Conn</span></span>=<span style="color: #000000;"><span style="color: #000000;">(httpurlconnection) url.openconnection (); Conn.setconnecttimeout (</span></span>5000<span style="color: #000000;"><span style="color: #000000;">); Conn.setrequestmethod (</span></span>"GET"<span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Code =<span style="color: #000000;"><span style="color: #000000;">Conn.getresponsecode (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(code==200<span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Request succeeded</span></span>InputStream is =<span style="color: #000000;"><span style="color: #000000;">Conn.getinputstream (); String result</span></span>=<span style="color: #000000;"><span style="color: #000000;">Streamtools.readinputstream (is); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">result; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;"> { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">request failed</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">return</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;">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;">return</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;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span><span style="color: #000000;"><span style="color: #000000;">string Loginbypost (string username,string Password) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Try</span></span><span style="color: #000000;"><span style="color: #000000;">{String Path</span></span>= "http://192.168.1.110:1010/UserInfo/Login"<span style="color: #000000;"><span style="color: #000000;">; URL URL</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">URL (path); HttpURLConnection Conn</span></span>=<span style="color: #000000;"><span style="color: #000000;">(httpurlconnection) url.openconnection (); Conn.setconnecttimeout (</span></span>5000<span style="color: #000000;"><span style="color: #000000;">); Conn.setrequestmethod (</span></span>"POST"<span style="color: #000000;"><span style="color: #000000;">); String Data</span></span>= "username=" +username+ "&password=" +<span style="color: #000000;"><span style="color: #000000;">password; Conn.setrequestproperty (</span></span>"content-type", "application/x-www-form-urlencoded"<span style="color: #000000;"><span style="color: #000000;">); Conn.setrequestproperty (</span></span>"content-length", data.length () + ""<span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">post is actually the way the browser writes the data to the server</span></span>Conn.setdoinput (<span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span><span style="color: #000000;"><span style="color: #000000;">); OutputStream OS</span></span>=<span style="color: #000000;"><span style="color: #000000;">Conn.getoutputstream (); Os.write (data.getbytes ()); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Code =<span style="color: #000000;"><span style="color: #000000;">Conn.getresponsecode (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(code==200<span style="color: #000000;"><span style="color: #000000;">) { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Request succeeded</span></span>InputStream is =<span style="color: #000000;"><span style="color: #000000;">Conn.getinputstream (); String result</span></span>=<span style="color: #000000;"><span style="color: #000000;">Streamtools.readinputstream (is); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">result; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;"> { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">request failed</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">return</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;">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;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">; }}</span></span></pre><p><p>2.StreamTools</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> package</span><span style="color: #000000;"><span style="color: #000000;">mydemo.mycom.demo2.utils;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">java.io.ByteArrayOutputStream;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Import</span></span><span style="color: #000000;"><span style="color: #000000;">java.io.InputStream;</span></span><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;">Streamtools {</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 readinputstream (inputstream is) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Try</span></span><span style="color: #000000;"><span style="color: #000000;">{bytearrayoutputstream BAOs</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Bytearrayoutputstream (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>Len =0<span style="color: #000000;"><span style="color: #000000;">; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">byte</span></span>[] buffer =<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">byte</span></span>[1024<span style="color: #000000;"><span style="color: #000000;">]; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> while</span>(len=is.read (buffer))!=-1<span style="color: #000000;"><span style="color: #000000;">) {baos.write (buffer,</span></span>0<span style="color: #000000;"><span style="color: #000000;">, len); } is.close (); Baos.close (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">byte</span></span>[] result =<span style="color: #000000;"><span style="color: #000000;">Baos.tobytearray (); String Temp</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">String (result); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">temp; }</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;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">; } }}</span></span></pre><p><p></p></p><p><p>Android vs. server Get or post interaction data</p></p></span>