国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設(shè)計 > 正文

post請求與get請求

2019-11-09 14:47:10
字體:
供稿:網(wǎng)友

一.使用HttpURLConnection提交數(shù)據(jù)

"get"請求

代碼:

String path = "http://地址?數(shù)據(jù)1名字=" + URLEncoder.encode(數(shù)據(jù)1,"utf-8") + "&數(shù)據(jù)2名字=" +URLEncoder.encode(數(shù)據(jù)2,"utf-8");

URL url = new URL(path);

HttpURLConnection conn = (HttpURLConnection)url.openConnection();

//這里設(shè)置請求方式要寫為大寫

conn.setRequestMethod("GET");

conn.setConnectTimeout(5000);

int code = conn.getResponseCode();

if(code == 200){

  InputStream is = conn.getInputStream();

  ByteArrayOutputStream baos = new ByteArrayOutputStream();  int len = -1;  byte[] buffer = new byte[1024];  while ((len = is.read(buffer)) != -1) {    baos.write(buffer, 0, len);  }  is.close();

  //這樣就得到服務(wù)器返回的數(shù)據(jù)了  result = baos.toString();

}

 

 

"post"請求

URL url = new URL(path);

HttpURLConnection conn = (HttpURLConnection)url.openConnection();

//1這里設(shè)置請求方式要寫為大寫

conn.setRequestMethod("POST");

//設(shè)置響應(yīng)時長

conn.setConnectTimeout(5000);

//2設(shè)置http請求數(shù)據(jù)的類型為表單類型

conn.setRequestPRoperty("Content-type","application/x-www-form-urlencoded");

String data = "數(shù)據(jù)1名字=" +URLEncoder.encode(數(shù)據(jù)1,"utf-8") + "&數(shù)據(jù)2名字=" + URLEncoder.encode(數(shù)據(jù)2,"utf-8"); 

//3設(shè)置給服務(wù)器寫的數(shù)據(jù)的長度

conn.setRequestProperty("Content-Length",String.valueOf(data.length()));

//4指定要給服務(wù)器寫數(shù)據(jù)

conn.setDoOutput(true);

//5開始向服務(wù)器寫數(shù)據(jù)

conn.getOutputStream().write(data.getBytes);

int code = conn.getResponseCode();

if(code == 200){

  InputStream is = conn.getInputStream();

  ByteArrayOutputStream baos = new ByteArrayOutputStream();  int len = -1;  byte[] buffer = new byte[1024];  while ((len = is.read(buffer)) != -1) {    baos.write(buffer, 0, len);  }  is.close();

  //注意:這里回流的編碼默認是"utf-8"的

  result = baos.toString();

}

二.使用HttpClient提交數(shù)據(jù)

注:HttpClient會被內(nèi)置到Android SDK中,可以不添加任何額外jar包直接使用,將文件從com文件夾復(fù)制粘貼到項目下就可以使用了

Get方式:

String path = "http://地址?數(shù)據(jù)1名字=" + URLEncoder.encode(數(shù)據(jù)1,"utf-8") + "&數(shù)據(jù)2名字" + URLEncoder.encode(數(shù)據(jù)2,"utf-8");

//可以將其過程理解為用戶瀏覽器操作

//1打開瀏覽器

HttpClient client = new DefaultHttpClient();

//2輸入地址

HttpGet httpGet = new HttpGet(path);

//3敲回車

HttpResponse response = client.execute(httpGet);

//獲取狀態(tài)碼

int code = response.getStatusLine().getStatusCode();

 

Post方式:

String path = "http://地址";

//1打開瀏覽器

HttpClient client = new DefaultHttpClient();

//2輸入地址

HttpPost httpPost = new HttpPost(path);

List<NameValuePair> parameters = new ArrayList<NameValuePair>();

parameters.add(new BasicNameValuePair("數(shù)據(jù)1名字",數(shù)據(jù)1));

parameters.add(new BasicNameValuePair("數(shù)據(jù)2名字",數(shù)據(jù)1));

httpPost.setEntity(new UrlEncodedFormEntity(parameters,"utf-8"));

//3敲回車

HttpResponse response = client.execute(httpPost);

//4獲取狀態(tài)碼

int code = response.getStatusLine().getStatusCode();

 

三.使用AsyncHttpClient框架提交數(shù)據(jù)

源碼可以在網(wǎng)上下載

將下載好的的源碼中src目錄中源碼拷貝到自己的工程的src目錄下

GET方式:

//請求路徑

String path = "http://地址?數(shù)據(jù)1名字=" + URLEncoder.encode(數(shù)據(jù)1) + "&數(shù)據(jù)2名字" + URLEncoder.encode(數(shù)據(jù)2);

AsyncHttpClient client = new AsyncHttpClient();

client.get(path,new AsyncHttpResponseHandler() {

  public void onSuccess(int statusCode,Header[]headers,byte[]responseBody){

  //請求成功

    new String(responseBody);//返回的數(shù)據(jù)

}

  public void onFailure(int statusCode,Header[]headers,byte[]responseBody,Throwable error) {

  //請求失敗

    String(responseBody);

  }

});

 

POST方式:

String path = "http://地址";

AsyncHttpClient client = new AsyncHttpClient();

RequestParams params = new RequestParams();

params.put("數(shù)據(jù)1名字",數(shù)據(jù)1);

params.put("數(shù)據(jù)2名字",數(shù)據(jù)2);

client.post(path,params,new AsyncHttpResponseHandler() {

  public void onSuccess(int statusCode,Header[]headers,byte[]responseBody){

  //請求成功

    new String(responseBody);//返回的數(shù)據(jù)

}

  public void onFailure(int statusCode,Header[]headers,byte[]responseBody,Throwable error) {

  //請求失敗

    String(responseBody);

  }

});

 


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 花莲县| 延津县| 沛县| 曲麻莱县| 科技| 沁源县| 望江县| 长顺县| 攀枝花市| 桂东县| 汤原县| 桃源县| 突泉县| 灵璧县| 团风县| 濮阳市| 农安县| 孝义市| 南宫市| 兴化市| 高邑县| 犍为县| 邹平县| 西乌珠穆沁旗| 阿瓦提县| 工布江达县| 金阳县| 凌海市| 安仁县| 道孚县| 凤阳县| 漠河县| 湖南省| 邵东县| 沂水县| 商河县| 东乌| 唐山市| 三明市| 深圳市| 安西县|