1.使用java.net.URL類訪問(wèn)網(wǎng)絡(luò)數(shù)據(jù)
和網(wǎng)絡(luò)打交道總是難免的,所以很有必要學(xué)一下怎么操作和網(wǎng)絡(luò)交互。
我們通過(guò)URL類或者HttpClient類,都可以對(duì)網(wǎng)絡(luò)訪問(wèn),至于這兩個(gè)類的區(qū)別,HttpClient類底層還是使用了Url類,對(duì)其封裝了一下。
第一步:建立一個(gè)服務(wù)器端,其中g(shù)et方法返回"you had receied the message from http://192.168.1.132:8088/WebServer/student.do”;post方法返回輸入的name。
package?org.servlet; import?java.io.IOException; import?java.io.PrintWriter; import?javax.servlet.ServletException; import?javax.servlet.http.HttpServlet; import?javax.servlet.http.HttpServletRequest; import?javax.servlet.http.HttpServletResponse; public?class?StudentAction?extends?HttpServlet?{ ????private?static?final?long?serialVersionUID?=?1L; ????public?StudentAction()?{ ????} ????protected?void?doGet(HttpServletRequest?request,?HttpServletResponse?response)?throws?ServletException,?IOException?{ ??????PrintWriter?out?=?response.getWriter(); ????????out.write("you?had?receied?the?message?from?http://192.168.1.132:8088/WebServer/student.do"); ????} ????protected?void?doPost(HttpServletRequest?request,?HttpServletResponse?response)?throws?ServletException,?IOException?{ ?????request.setCharacterEncoding("UTF-8"); ????????String?name?=?request.getParameter("name"); ????????PrintWriter?out?=?response.getWriter(); ????????out.write(name); ????} }
第二步:新建android projiect,新建一個(gè)java類,這里首先使用URL類,代碼如下:
其中添加一個(gè)get方法:
package?org.example.httpclient; import?java.io.ByteArrayOutputStream; import?java.io.InputStream; import?java.net.HttpURLConnection; import?java.net.URL; public?class?MHttpClient?{ ????public?void?get()?throws?Exception?{ ????????//?實(shí)例化一個(gè)URL對(duì)象 ????????URL?url?=?new?URL("http://192.168.1.132:8088/WebServer/student.do"); ????????//?打開(kāi)連接 ????????HttpURLConnection?conn?=?(HttpURLConnection)?url.openConnection(); ????????//?設(shè)置訪問(wèn)超時(shí)時(shí)間 ????????conn.setConnectTimeout(5?*?1000); ????????//?設(shè)置請(qǐng)求參數(shù) ????????conn.setRequestMethod("GET"); ????????//?得到輸入流 ????????InputStream?in?=?conn.getInputStream(); ????????//?從輸入流中讀取內(nèi)容放到內(nèi)存中 ????????ByteArrayOutputStream?out?=?new?ByteArrayOutputStream(); ????????byte[]?buffer?=?new?byte[1024]; ????????int?len?=?0; ????????while?((len?=?in.read(buffer))?!=?-1)?{ ????????????out.write(buffer,?0,?len); ????????} ????????System.out.println(new?String(out.toByteArray())); ????????out.close(); ????????in.close(); ????} }
我們使用單元測(cè)試,在manifest.xml里添加
別忘了加入internet訪問(wèn)權(quán)限,否則就會(huì)報(bào)java.net.UnknownHostException的錯(cuò)誤。
接下來(lái)新建一個(gè)MHttpclientTestCase類,需要繼承AndroidTestCase類
添加一個(gè)測(cè)試方法:
?public?void?testGet()?throws?Exception?{ ????????MHttpClient?url?=?new?MHttpClient(); ????????url.get(); ????}
然后在該文件右鍵->run as->android JUnit Test
可以在logcat中看到返回的信息“you had receied the message from http://192.168.1.132:8088/WebServer/student.do”。
如果要在請(qǐng)求中帶參數(shù)的話,直接把參數(shù)附加到請(qǐng)求路徑的后面就可以了,比如:http://www.baidu.com/s?wd=1&rsv_bp=0&rsv_spt=3&inputT=576
接下來(lái),我們使用post請(qǐng)求。再M(fèi)HttpClient類中建一個(gè)方法:
?public?void?post()?throws?Exception?{ ????????String?postData?=?"name=11"; ????????//?實(shí)例化一個(gè)URL對(duì)象 ????????URL?url?=?new?URL("http://192.168.1.132:8088/WebServer/student.do"); ????????//?打開(kāi)連接 ????????HttpURLConnection?conn?=?(HttpURLConnection)?url.openConnection(); ????????//?設(shè)置訪問(wèn)超時(shí)時(shí)間 ????????conn.setReadTimeout(5?*?1000); ????????//?設(shè)置請(qǐng)求方式 ????????conn.setRequestMethod("POST"); ????????//?發(fā)送POST請(qǐng)求必須設(shè)置允許輸出 ????????conn.setDoOutput(true); ????????conn.setRequestProperty("Charset",?"utf-8"); ????????conn.setRequestProperty("Content-Length",?postData.length()?+?""); ????????OutputStream?out?=?conn.getOutputStream(); ????????out.write(postData.getBytes()); ????????out.flush(); ????????if?(conn.getResponseCode()?==?200)?{ ????????????InputStream?in?=?conn.getInputStream(); ????????????//?從輸入流中讀取內(nèi)容放到內(nèi)存中 ????????????ByteArrayOutputStream?out2?=?new?ByteArrayOutputStream(); ????????????byte[]?buffer?=?new?byte[1024]; ????????????int?len?=?0; ????????????while?((len?=?in.read(buffer))?!=?-1)?{ ????????????????out2.write(buffer,?0,?len); ????????????} ????????????System.out.println(new?String(out2.toByteArray())); ????????????out2.close(); ????????????in.close(); ????????} ????????out.close(); ????}
在單元測(cè)試類MHttpclientTestCase再添加一個(gè)方法:
?public?void?testPost()?throws?Exception?{ ????????MHttpClient?url?=?new?MHttpClient(); ????????url.post(); ????}
在方法名稱右鍵->run as->android JUnit Test
可在logcat里看到返回結(jié)果 “11”
貼上全部代碼:
MHttpClient.java
package?org.example.httpclient; import?java.io.ByteArrayOutputStream; import?java.io.InputStream; import?java.io.OutputStream; import?java.net.HttpURLConnection; import?java.net.URL; public?class?MHttpClient?{ ????public?void?get()?throws?Exception?{ ????????//?實(shí)例化一個(gè)URL對(duì)象 ????????URL?url?=?new?URL("http://192.168.1.132:8088/WebServer/student.do"); ????????//?打開(kāi)連接 ????????HttpURLConnection?conn?=?(HttpURLConnection)?url.openConnection(); ????????//?設(shè)置訪問(wèn)超時(shí)時(shí)間 ????????conn.setConnectTimeout(5?*?1000); ????????//?設(shè)置請(qǐng)求方式 ????????conn.setRequestMethod("GET"); ????????//?得到輸入流 ????????InputStream?in?=?conn.getInputStream(); ????????//?從輸入流中讀取內(nèi)容放到內(nèi)存中 ????????ByteArrayOutputStream?out?=?new?ByteArrayOutputStream(); ????????byte[]?buffer?=?new?byte[1024]; ????????int?len?=?0; ????????while?((len?=?in.read(buffer))?!=?-1)?{ ????????????out.write(buffer,?0,?len); ????????} ????????System.out.println(new?String(out.toByteArray())); ????????out.close(); ????????in.close(); ????} ????public?void?post()?throws?Exception?{ ????????String?postData?=?"name=11"; ????????byte[]?data?=?postData.getBytes(); ????????//?實(shí)例化一個(gè)URL對(duì)象 ????????URL?url?=?new?URL("http://192.168.1.132:8088/WebServer/student.do"); ????????//?打開(kāi)連接 ????????HttpURLConnection?conn?=?(HttpURLConnection)?url.openConnection(); ????????//?設(shè)置訪問(wèn)超時(shí)時(shí)間 ????????conn.setConnectTimeout(10?*?1000); ????????//?設(shè)置請(qǐng)求方式 ????????conn.setRequestMethod("POST"); ????????//?發(fā)送POST請(qǐng)求必須設(shè)置允許輸出 ????????conn.setDoOutput(true); ????????conn.setRequestProperty("Charset",?"UTF-8"); ????????conn.setRequestProperty("Content-Length",?data.length?+?""); ????????conn.setRequestProperty("Content-Type",?"application/x-www-form-urlencoded"); ????????conn.setRequestProperty("Connection",?"Keep-Alive");//?維持長(zhǎng)連接 ????????OutputStream?out?=?conn.getOutputStream(); ????????out.write(data); ????????out.flush(); ????????if?(conn.getResponseCode()?==?200)?{ ????????????InputStream?in?=?conn.getInputStream(); ????????????//?從輸入流中讀取內(nèi)容放到內(nèi)存中 ????????????ByteArrayOutputStream?out2?=?new?ByteArrayOutputStream(); ????????????byte[]?buffer?=?new?byte[1024]; ????????????int?len?=?0; ????????????while?((len?=?in.read(buffer))?!=?-1)?{ ????????????????out2.write(buffer,?0,?len); ????????????} ????????????System.out.println(new?String(out2.toByteArray())); ????????????out2.close(); ????????????in.close(); ????????} ????????out.close(); ????} }
MHttpclientTestCase.java
package?org.example.httpclient; import?android.test.AndroidTestCase; public?class?MHttpclientTestCase?extends?AndroidTestCase?{ ????public?void?testGet()?throws?Exception?{ ????????MHttpClient?url?=?new?MHttpClient(); ????????url.get(); ????} ????public?void?testPost()?throws?Exception?{ ????????MHttpClient?url?=?new?MHttpClient(); ????????url.post(); ????} }
AndroidManifest.xml