當前位置:九游会j9娱乐平台-九游ag登录中心网址 » 安卓系統 » androidget請求

androidget請求-九游会j9娱乐平台

發布時間: 2024-01-18 22:17:51

1. android怎麼在網路請求頭里加參數

android中網路通信分為socket編程和http編程,這里只介紹htt方面。網路請求方式可分為get請求,post兩種請求方式,get方式在進行數據請求時,會把數據附加到url後面傳遞給伺服器,比如常見的:http://xxx.xxx.xxx/xx.aspx?id=1,post方式則是將請求的數據放到http請求頭中,作為請求頭的一部分傳入伺服器。
所以,在進行http編程前,首先要明確究竟使用的哪種方式進行數據請求的。

android中http編程有兩種:1、httpurlconnection;2、httpclient

首先介紹一下httpurlconnection方式的get請求和post請求方法:

[java] view
plainprint?

private map paramsvalue;

string urlpath=null;// 發送地http://192.168.100.91:8080/myweb/login?username=abc&password=123

public void initdata(){urlpath="http://192.168.100.91:8080/myweb/login";

paramsvalue=new hashmap();

paramsvalue.put("username", "111");

paramsvalue.put("password", "222");

}


private map paramsvalue;
string urlpath=null;

// 發送地http://192.168.100.91:8080/myweb/login?username=abc&password=123
public void initdata(){

urlpath="http://192.168.100.91:8080/myweb/login";
paramsvalue=new hashmap();
paramsvalue.put("username", "111");
paramsvalue.put("password", "222");
}

get方式發起請求:

[java] view
plainprint?

private boolean sendgetrequest(string path, map params) throws exception {

boolean success=false;// stringbuilder是用來組拼請求地址和參數

stringbuilder sb = new stringbuilder();

sb.append(path).append("?");

if (params != null && params.size() != 0) {

for (map.entry entry : params.entryset()) {

// 如果請求參數中有中文,需要進行urlencoder編碼 gbk/utf8

sb.append(entry.getkey()).append("=").append(urlencoder.encode(entry.getvalue(), "utf-8"));

sb.append("&");

}

sb.deletecharat(sb.length() - 1);

}url url = new );

httpurlconnection conn = (httpurlconnection) url.openconnection();

conn.setconnecttimeout(20000);

conn.setrequestmethod("get");if (conn.getresponsecode() == 200) {

success= true;

}

if(conn!=null)

conn.disconnect();

return success;

}
private boolean sendgetrequest(string path, map params) throws exception {
boolean success=false;

// stringbuilder是用來組拼請求地址和參數
stringbuilder sb = new stringbuilder();
sb.append(path).append("?");
if (params != null && params.size() != 0) {
for (map.entry entry : params.entryset()) {
// 如果請求參數中有中文,需要進行urlencoder編碼 gbk/utf8
sb.append(entry.getkey()).append("=").append(urlencoder.encode(entry.getvalue(), "utf-8"));
sb.append("&");
}
sb.deletecharat(sb.length() - 1);
}

url url = new );
httpurlconnection conn = (httpurlconnection) url.openconnection();
conn.setconnecttimeout(20000);
conn.setrequestmethod("get");

if (conn.getresponsecode() == 200) {
success= true;
}
if(conn!=null)
conn.disconnect();
return success;
}

postt方式發起請求:

[java] view
plainprint?

private boolean sendpostrequest(string path,map params) throws exception{

boolean success=false;

//stringbuilder是用來組拼請求參數

stringbuilder sb = new stringbuilder();if(params!=null &¶ms.size()!=0){for (map.entry entry : params.entryset()) {sb.append(entry.getkey()).append("=").append(urlencoder.encode(entry.getvalue(), "utf-8"));

sb.append("&");}

sb.deletecharat(sb.length()-1);

}//entity為請求體部分內容

//如果有中文則以utf-8編碼為username=中国&password=123

byte[] entity = sb.tostring().getbytes();url url = new ;

httpurlconnection conn = (httpurlconnection) url.openconnection();

conn.setconnecttimeout(2000);

// 設置以post方式

conn.setrequestmethod("post");

// post 請求不能使用緩存

// urlconn.setusecaches(false);

//要向外輸出數據,要設置這個

conn.setdooutput(true);

// 配置本次連接的content-type,配置為application/x-www-form-urlencoded

//設置content-type獲得輸出流,便於想伺服器發送信息。

//post請求這個一定要設置

conn.setrequestproperty("content-type", "application/x-www-form-urlencoded");

conn.setrequestproperty("content-length", entity.length "");

// 要注意的是connection.getoutputstream會隱含的進行connect。

outputstream out = conn.getoutputstream();

//寫入參數值

out.write(entity);

//刷新、關閉

out.flush();

out.close();if (conn.getresponsecode() == 200) {

success= true;

}

if(conn!=null)

conn.disconnect();

return success;}
private boolean sendpostrequest(string path,map params) throws exception{
boolean success=false;
//stringbuilder是用來組拼請求參數
stringbuilder sb = new stringbuilder();

if(params!=null &¶ms.size()!=0){

for (map.entry entry : params.entryset()) {

sb.append(entry.getkey()).append("=").append(urlencoder.encode(entry.getvalue(), "utf-8"));
sb.append("&");

}
sb.deletecharat(sb.length()-1);
}

//entity為請求體部分內容
//如果有中文則以utf-8編碼為username=中国&password=123
byte[] entity = sb.tostring().getbytes();

url url = new ;
httpurlconnection conn = (httpurlconnection) url.openconnection();
conn.setconnecttimeout(2000);
// 設置以post方式
conn.setrequestmethod("post");
// post 請求不能使用緩存
// urlconn.setusecaches(false);
//要向外輸出數據,要設置這個
conn.setdooutput(true);
// 配置本次連接的content-type,配置為application/x-www-form-urlencoded
//設置content-type獲得輸出流,便於想伺服器發送信息。
//post請求這個一定要設置
conn.setrequestproperty("content-type", "application/x-www-form-urlencoded");
conn.setrequestproperty("content-length", entity.length "");
// 要注意的是connection.getoutputstream會隱含的進行connect。
outputstream out = conn.getoutputstream();
//寫入參數值
out.write(entity);
//刷新、關閉
out.flush();
out.close();

if (conn.getresponsecode() == 200) {
success= true;
}
if(conn!=null)
conn.disconnect();
return success;

}

在介紹一下httpclient方式,相比httpurlconnection,httpclient封裝的得更簡單易用一些,看一下實例:

get方式發起請求:

[java] view
plainprint?

public string getrequest(string urlpath,map params){

string content=null;

stringbuilder buf = new stringbuilder();

if(params!=null &¶ms.size()!=0){for (map.entry entry : params.entryset()) {buf.append(entry.getkey()).append("=").append(urlencoder.encode(entry.getvalue(), "utf-8"));

buf.append("&");}

buf.deletecharat(buf.length()-1);

}

content= buf.tostring();httpclient httpclient = new defaulthttpclient();

httpget getmethod = new httpget(content);httpresponse response = null;

try {

response = httpclient.execute(getmethod);

} catch (clientprotocolexception e) {

e.printstacktrace();

} catch (ioexception e) {

e.printstacktrace();

}catch (exception e) {

e.printstacktrace();

}if (response!=null&&response.getstatusline().getstatuscode() == httpstatus.sc_ok) {

try {

content = entityutils.tostring(response.getentity());

} catch (parseexception e) {

e.printstacktrace();

} catch (ioexception e) {

e.printstacktrace();

}

}return content;

}
public string getrequest(string urlpath,map params){
string content=null;
stringbuilder buf = new stringbuilder();
if(params!=null &¶ms.size()!=0){

for (map.entry entry : params.entryset()) {

buf.append(entry.getkey()).append("=").append(urlencoder.encode(entry.getvalue(), "utf-8"));
buf.append("&");

}
buf.deletecharat(buf.length()-1);
}
content= buf.tostring();

httpclient httpclient = new defaulthttpclient();
httpget getmethod = new httpget(content);

httpresponse response = null;
try {
response = httpclient.execute(getmethod);
} catch (clientprotocolexception e) {
e.printstacktrace();
} catch (ioexception e) {
e.printstacktrace();
}catch (exception e) {
e.printstacktrace();
}

if (response!=null&&response.getstatusline().getstatuscode() == httpstatus.sc_ok) {
try {
content = entityutils.tostring(response.getentity());
} catch (parseexception e) {
e.printstacktrace();
} catch (ioexception e) {
e.printstacktrace();
}
}

return content;
}

postt方式發起請求:

[java] view
plainprint?

private boolean sendpostrequesthttpclient(string path,map params) throws exception {

boolean success = false;

// 封裝請求參數

list pair = new arraylist();

if (params != null && !params.isempty()) {

for (map.entry entry : params.entryset()) {

pair.add(new basicnamevaluepair(entry.getkey(), entry

.getvalue()));

}

}

// 把請求參數變成請求體部分

urlencodedformentity uee = new urlencodedformentity(pair, "utf-8");

// 使用httppost對象設置發送的url路徑

httppost post = new httppost(path);

// 發送請求體

post.setentity(uee);

// 創建一個瀏覽器對象,以把post對象向伺服器發送,並返回響應消息

defaulthttpclient dhc = new defaulthttpclient();

httpresponse response = dhc.execute(post);

if (response.getstatusline().getstatuscode() == 200) {

success = true;

}

return success;

}
private boolean sendpostrequesthttpclient(string path,map params) throws exception {
boolean success = false;
// 封裝請求參數
list pair = new arraylist();
if (params != null && !params.isempty()) {
for (map.entry entry : params.entryset()) {
pair.add(new basicnamevaluepair(entry.getkey(), entry
.getvalue()));
}
}
// 把請求參數變成請求體部分
urlencodedformentity uee = new urlencodedformentity(pair, "utf-8");
// 使用httppost對象設置發送的url路徑
httppost post = new httppost(path);
// 發送請求體
post.setentity(uee);
// 創建一個瀏覽器對象,以把post對象向伺服器發送,並返回響應消息
defaulthttpclient dhc = new defaulthttpclient();
httpresponse response = dhc.execute(post);
if (response.getstatusline().getstatuscode() == 200) {
success = true;
}
return success;
}

2. android webview中的loadurl方法是get請求還是post請求

我寫過的一般是get請求 因為一般這個loadurl就是請求的完整地址 如果其實也很容易看 ,請求的時候介面端給你的介面是拼接了參數 的是get 請求 如果沒有拼接參數的話 可以是get也可以是post 這個咨詢下你們的寫介面的那個人也可以的 ,

3. android studio使用retrofit框架,get發送請求,gson解析返回的json數據時報錯怎麼辦

資料庫一直以來給我的感覺就是——麻煩!!!
接觸了realm之後才終於可以開開心心的使用資料庫了。
本文總結一些realm資料庫的常用知識點,包括多線程訪問,以及如何與retrofit2.0一起使用等...
看懂這些知識點之後,個人認為就可以在一般的項目中使用realm了。

1. model類必須extends realmobject,所有屬性必須用private修飾

2. model中支持基本數據結構:boolean, byte, short, ìnt, long, float, double, string, dateand byte[]

3.若要使用list必須用realmlist,或者繼承realmlist

4.與retrofit2.*一起使用,通過gson來解析json數據並直接生成realmobject,可參考如下寫法:

[java] view plain
gson gson = new gsonbuilder()
.setexclusionstrategies(new exclusionstrategy() {
@override
public boolean shouldskipfield(fieldattributes f) {
return f.getdeclaringclass().equals(realmobject.class);
}

@override
public boolean shouldskipclass(class clazz) {
return false;
}

4. android get和post的區別

1. get是從伺服器上獲取數據,post是向伺服器傳送數據。
2. get是把參數數據隊列加到提交表單的action屬性所指的url中,值和表單內各個欄位一一對應,在url中可以看到。post是通過http post機制,將表單內各個欄位與其內容放置在html header內一起傳送到action屬性所指的url地址。用戶看不到這個過程。
3. 對於get方式,伺服器端用request.querystring獲取變數的值,對於post方式,伺服器端用request.form獲取提交的數據。
4. get傳送的數據量較小,不能大於2kb。post傳送的數據量較大,一般被默認為不受限制。但理論上,iis4中最大量為80kb,iis5中為100kb。
5. get安全性非常低,post安全性較高。但是執行效率卻比post方法好。

建議:
1、get方式的安全性較post方式要差些,包含機密信息的話,建議用post數據提交方式;
2、在做數據查詢時,建議用get方式;而在做數據添加、修改或刪除時,建議用post方式;

5. android網路編程向伺服器發送get請求時,發送了請求,但沒有返回結果是怎麼回事

開個debug,看看結果返回

6. android okhttp post json和get有什麼區別

區別是:
get:是以實體的方式得到由請求uri所指定資源的信息,如果請求uri只是一個數據產生過程,那麼最終要在響應實體中返回的是處理過程的結果所指向的資源,而不是處理過程的描述。
post:用燃游晌來向目的伺服器發出請求,要求它接受被附在請求後的實體,並把它磨棗當作請求隊列中請求uri所指定資源的附加新子項,post被設計成用統一的方法實現下列功能:
1:對現有資源的解釋
2:向電子公告欄、新聞組、郵件列表或類似討論組發信息。
3:提交數據塊
4:通過附加操作來擴展資料庫

android系統提供了兩種http通信類,httpurlconnection和httpclient。
關於httpurlconnection和httpclient的選擇>>官方博客
盡管google在大部分安卓版本皮鋒中推薦使用httpurlconnection,但是這個類相比httpclient實在是太難用,太弱爆了。
okhttp是一個相對成熟的解決方案,據說android4.4的源碼中可以看到httpurlconnection已經替換成okhttp實現了。所以我們更有理由相信okhttp的強大。

okhttp 處理了很多網路疑難雜症:會從很多常用的連接問題中自動恢復。如果您的伺服器配置了多個ip地址,當第一個ip連接失敗的時候,okhttp會自動嘗試下一個ip。okhttp還處理了代理伺服器問題和ssl握手失敗問題。
使用 okhttp 無需重寫您程序中的網路代碼。okhttp實現了幾乎和java.net.httpurlconnection一樣的api。如果你用了 apache httpclient,則okhttp也提供了一個對應的okhttp-apache 模塊。

熱點內容
愛奇藝正義聯盟為啥不能緩存 發布:2024-01-20 00:52:13 瀏覽:248
caccess查詢資料庫 發布:2024-01-20 00:43:10 瀏覽:769
xp文件夾圖標更改 發布:2024-01-20 00:43:03 瀏覽:19
python和node 發布:2024-01-20 00:37:12 瀏覽:194
android拖拉 發布:2024-01-20 00:00:49 瀏覽:583
少兒編程課程體系介紹 發布:2024-01-20 00:00:48 瀏覽:846
我說你做下載ftp 發布:2024-01-20 00:00:47 瀏覽:8
安卓驅動培訓哪裡好 發布:2024-01-19 23:55:41 瀏覽:987
python轉為字元串 發布:2024-01-19 23:51:39 瀏覽:272
合同文件夾 發布:2024-01-19 23:50:52 瀏覽:740
网站地图