您的位置:首页 > 移动开发 > Android开发

Android中"get","post" 最新请求方式

2017-07-14 09:48 323 查看
最近学习连接服务器获取GET,POST请求,发现很多方法已经不实用了,比如DefaultHttpClient在android studio里已经不推荐使用了,而是推荐httpclient,另外不知道为什么我在使用AsyncHttpClient,延迟的厉害,所以本人也是不推荐使用这个的

一.使用HttpURLConnection提交数据

"get"请求

代码:

String path = "http://地址?数据1名字=" + URLEncoder.encode(数据1,"utf-8") + "&数据2名字=" +URLEncoder.encode(数据2,"utf-8");

URL url = new URL(path);

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

//这里设置请求方式要写为大写

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();

  //这样就得到服务器返回的数据了
  result = baos.toString();

}


 

 

"post"请求

URL url = new URL(path);

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

//1这里设置请求方式要写为大写

conn.setRequestMethod("POST");

//设置响应时长

conn.setConnectTimeout(5000);

//2设置http请求数据的类型为表单类型

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

String data = "数据1名字=" +URLEncoder.encode(数据1,"utf-8") + "&数据2名字=" + URLEncoder.encode(数据2,"utf-8");

//3设置给服务器写的数据的长度

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

//4指定要给服务器写数据

conn.setDoOutput(true);

//5开始向服务器写数据

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提交数据

注:HttpClient会被内置到Android SDK中,可以不添加任何额外jar包直接使用,将文件从com文件夹复制粘贴到项目下就可以使用了

Get方式:

String path = "http://地址?数据1名字=" + URLEncoder.encode(数据1,"utf-8") + "&数据2名字" + URLEncoder.encode(数据2,"utf-8");

//可以将其过程理解为用户浏览器操作

//1打开浏览器

HttpClient client = new DefaultHttpClient();

//2输入地址

HttpGet httpGet = new HttpGet(path);

//3敲回车

HttpResponse response = client.execute(httpGet);

//获取状态码

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("数据1名字",数据1));

parameters.add(new BasicNameValuePair("数据2名字",数据1));

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

//3敲回车

HttpResponse response = client.execute(httpPost);

//4获取状态码

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


 

三.使用AsyncHttpClient框架提交数据

该源码可以在网上下载

将下载好的的源码中src目录中源码拷贝到自己的工程的src目录下

GET方式:

//请求路径

String path = "http://地址?数据1名字=" + URLEncoder.encode(数据1) + "&数据2名字" + URLEncoder.encode(数据2);

AsyncHttpClient client = new AsyncHttpClient();

client.get(path,new AsyncHttpResponseHandler() {

  public void onS
4000
uccess(int statusCode,Header[]headers,byte[]responseBody){

  //请求成功

    new String(responseBody);//返回的数据

}

  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("数据1名字",数据1);

params.put("数据2名字",数据2);

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

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

  //请求成功

    new String(responseBody);//返回的数据

}

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

  //请求失败

    String(responseBody);

  }

});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: