日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁(yè) > 芯聞號(hào) > 充電吧
[導(dǎo)讀]和網(wǎng)絡(luò)打交道總是難免的,所以很有必要學(xué)一下怎么操作和網(wǎng)絡(luò)交互。我們通過(guò)URL類(lèi)或者HttpClient類(lèi),都可以對(duì)網(wǎng)絡(luò)訪問(wèn),至于這兩個(gè)類(lèi)的區(qū)別,HttpClient類(lèi)底層還是使用了Url類(lèi),對(duì)其封裝了

和網(wǎng)絡(luò)打交道總是難免的,所以很有必要學(xué)一下怎么操作和網(wǎng)絡(luò)交互。

我們通過(guò)URL類(lèi)或者HttpClient類(lèi),都可以對(duì)網(wǎng)絡(luò)訪問(wèn),至于這兩個(gè)類(lèi)的區(qū)別,HttpClient類(lèi)底層還是使用了Url類(lèi),對(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類(lèi),這里首先使用URL類(lèi),代碼如下:


其中添加一個(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類(lèi),需要繼承AndroidTestCase類(lèi)

添加一個(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類(lèi)中建一個(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è)試類(lèi)MHttpclientTestCase再添加一個(gè)方法:



?public?void?testPost()?throws?Exception?{
????????MHttpClient?url?=?new?MHttpClient();
????????url.post();
????}

在方法名稱(chēng)右鍵->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








本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專(zhuān)欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除( 郵箱:macysun@21ic.com )。
換一批
延伸閱讀
關(guān)閉