1.使用java.net.URL類訪問網(wǎng)絡(luò)數(shù)據(jù)
和網(wǎng)絡(luò)打交道總是難免的,所以很有必要學(xué)一下怎么操作和網(wǎng)絡(luò)交互。
我們通過URL類或者HttpClient類,都可以對網(wǎng)絡(luò)訪問,至于這兩個類的區(qū)別,HttpClient類底層還是使用了Url類,對其封裝了一下。
第一步:建立一個服務(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,新建一個java類,這里首先使用URL類,代碼如下:
其中添加一個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?{
????????//?實例化一個URL對象
????????URL?url?=?new?URL("http://192.168.1.132:8088/WebServer/student.do");
????????//?打開連接
????????HttpURLConnection?conn?=?(HttpURLConnection)?url.openConnection();
????????//?設(shè)置訪問超時時間
????????conn.setConnectTimeout(5?*?1000);
????????//?設(shè)置請求參數(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();
????}
}我們使用單元測試,在manifest.xml里添加
別忘了加入internet訪問權(quán)限,否則就會報java.net.UnknownHostException的錯誤。
接下來新建一個MHttpclientTestCase類,需要繼承AndroidTestCase類
添加一個測試方法:
?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”。
如果要在請求中帶參數(shù)的話,直接把參數(shù)附加到請求路徑的后面就可以了,比如:http://www.baidu.com/s?wd=1&rsv_bp=0&rsv_spt=3&inputT=576
接下來,我們使用post請求。再MHttpClient類中建一個方法:
?public?void?post()?throws?Exception?{
????????String?postData?=?"name=11";
????????//?實例化一個URL對象
????????URL?url?=?new?URL("http://192.168.1.132:8088/WebServer/student.do");
????????//?打開連接
????????HttpURLConnection?conn?=?(HttpURLConnection)?url.openConnection();
????????//?設(shè)置訪問超時時間
????????conn.setReadTimeout(5?*?1000);
????????//?設(shè)置請求方式
????????conn.setRequestMethod("POST");
????????//?發(fā)送POST請求必須設(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();
????}
在單元測試類MHttpclientTestCase再添加一個方法:
?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?{
????????//?實例化一個URL對象
????????URL?url?=?new?URL("http://192.168.1.132:8088/WebServer/student.do");
????????//?打開連接
????????HttpURLConnection?conn?=?(HttpURLConnection)?url.openConnection();
????????//?設(shè)置訪問超時時間
????????conn.setConnectTimeout(5?*?1000);
????????//?設(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();
????}
????public?void?post()?throws?Exception?{
????????String?postData?=?"name=11";
????????byte[]?data?=?postData.getBytes();
????????//?實例化一個URL對象
????????URL?url?=?new?URL("http://192.168.1.132:8088/WebServer/student.do");
????????//?打開連接
????????HttpURLConnection?conn?=?(HttpURLConnection)?url.openConnection();
????????//?設(shè)置訪問超時時間
????????conn.setConnectTimeout(10?*?1000);
????????//?設(shè)置請求方式
????????conn.setRequestMethod("POST");
????????//?發(fā)送POST請求必須設(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");//?維持長連接
????????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





