【移動應(yīng)用開發(fā)技術(shù)】怎么在Android中使用URLConnection實現(xiàn)網(wǎng)絡(luò)編程_第1頁
【移動應(yīng)用開發(fā)技術(shù)】怎么在Android中使用URLConnection實現(xiàn)網(wǎng)絡(luò)編程_第2頁
【移動應(yīng)用開發(fā)技術(shù)】怎么在Android中使用URLConnection實現(xiàn)網(wǎng)絡(luò)編程_第3頁
【移動應(yīng)用開發(fā)技術(shù)】怎么在Android中使用URLConnection實現(xiàn)網(wǎng)絡(luò)編程_第4頁
【移動應(yīng)用開發(fā)技術(shù)】怎么在Android中使用URLConnection實現(xiàn)網(wǎng)絡(luò)編程_第5頁
免費預(yù)覽已結(jié)束,剩余1頁可下載查看

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

【移動應(yīng)用開發(fā)技術(shù)】怎么在Android中使用URLConnection實現(xiàn)網(wǎng)絡(luò)編程

這篇文章將為大家詳細(xì)講解有關(guān)怎么在Android中使用URLConnection實現(xiàn)網(wǎng)絡(luò)編程,文章內(nèi)容質(zhì)量較高,因此在下分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。具體如下:URL的openConnection()方法將返回一個URLConnection,該對象表示應(yīng)用程序和URL之間的通信連接,程序可以通過URLConnection實例向該URL發(fā)送請求,讀取URL引用的資源。通常創(chuàng)建一個和URL的連接,并發(fā)送請求,讀取此URL引用的資源。需要如下步驟:a)通過調(diào)用URL對象openConnection()方法來創(chuàng)建URLConnection對象b)設(shè)置URLConnection的參數(shù)和普通請求屬性conn.setRequestProperty("accept","*/*");

conn.setRequestProperty("connection","Keep-Alive");

conn.setRequestProperty("user-agent","Mozilla/4.0(compatible;MSIE

6.0;Windows

NT

5.1;SV1)");

conn.setRequestProperty("Content-Type",

"application/x-www-form-urlencoded");發(fā)送POST請求必須設(shè)置如下兩行conn.setDoInput(true):設(shè)置該URLConnection的doInput請求頭字段的值coon.setDoOutput(true):c)調(diào)用connect():打開到此URL引用的資源的通信鏈接(如果尚未建立這樣的連接)。如果在已打開連接(此時connected字段的值為true)的情況下調(diào)用connect方法,則忽略該調(diào)用.URLConnection對象經(jīng)歷兩個階段:首先創(chuàng)建對象,然后建立連接。在創(chuàng)建對象之后,建立連接之前,可指定各種選項(例如doInput和UseCaches).連接后再進(jìn)行設(shè)置就會發(fā)生錯誤。連接后才能進(jìn)行的操作(例如getContentLength),如有必要,將隱式執(zhí)行連接.d)如果只是發(fā)送GET方式請求,使用connect方法建立和遠(yuǎn)程資源之間的實際連接即可,在請求的地址中傳入數(shù)據(jù)。如果需要發(fā)送Post方法請求。需要獲取URLConnection實例對應(yīng)的輸出流來發(fā)送請求參數(shù),PrintWriter

out=new

PrintWriter(conn.getOutputStream());

//解決亂碼問題

String

n=EncodingUtils.getString("張三".getBytes(),"UTF-8");

out.write("name="+n+"&pwd="+pwd);

out.flush();//刷新輸出流的緩沖e)遠(yuǎn)程資源變?yōu)榭捎茫绦蚩梢栽L問遠(yuǎn)程資源的頭字段或通過輸入流讀取遠(yuǎn)程資源的數(shù)據(jù)。getInputStream()獲取輸入流。從輸入流讀取response的數(shù)據(jù)。注意:1)如果既要使用輸入流讀取URLConnection響應(yīng)的內(nèi)容,也要使用輸出流發(fā)送請求參數(shù),一定要先使用輸出流,再使用輸入流。2)借助于URLConnection類的幫助,應(yīng)用程序可以非常方便地與指定站點交換信息,包括發(fā)送GET請求,POST請求,并獲取網(wǎng)站的響應(yīng)等。代碼編寫步驟如下:1.先寫一個服務(wù)器-web工程新建一個Servlet--LoginServlet,簡單實現(xiàn)用戶的登錄~@WebServlet("/LoginServlet")

public

class

LoginServlet

extends

HttpServlet

{

private

static

final

long

serialVersionUID

=

1L;

public

LoginServlet()

{

//

TODO

Auto-generated

constructor

stub

}

protected

void

doGet(HttpServletRequest

request,

HttpServletResponse

response)

throws

ServletException,

IOException

{

//

TODO

Auto-generated

method

stub

doPost(request,

response);

}

protected

void

doPost(HttpServletRequest

request,

HttpServletResponse

response)

throws

ServletException,

IOException

{

//

TODO

Auto-generated

method

stub

String

name=request.getParameter("name");

String

pwd=request.getParameter("pwd");

System.out.println(name+"

"+pwd);

OutputStream

os=response.getOutputStream();

if("xuxu".equals(name)&&"123".equals(pwd)){

os.write(("成功").getBytes("UTF-8"));

}else{

os.write(("失敗").getBytes("UTF-8"));

}

os.flush();

os.close();

}

}2.新建一個android項目,在MainActivity中分別使用get方法和post方法實現(xiàn)用戶的登錄public

class

MainActivity

extends

Activity

{

private

EditText

name,pwd;

public

void

get(View

view){

new

Thread(){

public

void

run()

{

try

{

URL

url=new

URL("41:8090/ConnectionServlet/LoginServlet"+

"?name="+name+"&pwd="+pwd);

URLConnection

conn=url.openConnection();

conn.connect();//真正的建立網(wǎng)絡(luò)連接

BufferedReader

reader=new

BufferedReader(new

InputStreamReader(conn.getInputStream()));

String

line=null;

StringBuffer

stringBuffer=new

StringBuffer();//字符串,都可以存儲和操作字符串,它是變量

while

((line=reader.readLine())!=null)

{

stringBuffer.append(line);

}

System.out.println(stringBuffer.toString());

}

catch

(Exception

e)

{

//

TODO

Auto-generated

catch

block

e.printStackTrace();

}

};

}.start();

}

public

void

post(View

view){

new

Thread(){

public

void

run()

{

try

{

URL

url=new

URL("41:8090/ConnectionServlet/LoginServlet"

);

URLConnection

conn=url.openConnection();

//必須設(shè)置

conn.setDoInput(true);

conn.setDoOutput(true);

conn.connect();//真正的建立網(wǎng)絡(luò)連接

PrintWriter

printWriter=new

PrintWriter(conn.getOutputStream());

printWriter.write("name="+name+"&pwd="+pwd);

printWriter.flush();

printWriter.close();

BufferedReader

reader=new

BufferedReader(new

InputStreamReader(conn.getInputStream()));

String

line=null;

StringBuffer

stringBuffer=new

StringBuffer();

while

((line=reader.readLine())!=null)

{

stringBuffer.append(line);

}

System.out.println(stringBuffer.toString());

}

catch

(Exception

e)

{

//

TODO

Auto-generated

catch

block

e.printStackTrace();

}

};

}.start();

}

@Override

protected

void

onCreate(Bundle

savedInstanceState)

{

super.onCreate(savedInsta

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論