Android中"get","post" 最新请求方式
2017-07-14 09:48
323 查看
最近学习连接服务器获取GET,POST请求,发现很多方法已经不实用了,比如DefaultHttpClient在android studio里已经不推荐使用了,而是推荐httpclient,另外不知道为什么我在使用AsyncHttpClient,延迟的厉害,所以本人也是不推荐使用这个的
一.使用HttpURLConnection提交数据
"get"请求
代码:
"post"请求
二.使用HttpClient提交数据
注:HttpClient会被内置到Android SDK中,可以不添加任何额外jar包直接使用,将文件从com文件夹复制粘贴到项目下就可以使用了
Get方式:
Post方式:
三.使用AsyncHttpClient框架提交数据
该源码可以在网上下载
将下载好的的源码中src目录中源码拷贝到自己的工程的src目录下
GET方式:
POST方式:
一.使用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); } });
相关文章推荐
- Android中的post和get请求(HttpURLConnection)方式
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- Android传统HTTP请求get----post方式提交数据(包括乱码问题)
- android之使用get和post方式向服务器提交请求
- Android中post和get两种方式发送请求
- android中用get和post方式向服务器提交请求
- "Android下使用HttpUrlConntion"进行最简单的get,post请求(包括创建服务器)
- Android 网络请求方式之HttpClient,GET,POST,PUT请求使用方法
- Android 以Http协议 使用get和post方式请求站点
- android 向serverGet和Post请求的两种方式,android向server发送文件,自己组装协议和借助第三方开源
- Android Volley框架的几种post提交请求方式和探究POST、GET提交
- Android中两种请求方式HttpGet和HttpPost验证用户登录
- Android 网络请求通用的get与post方式
- Android使用HttpClient以Post、Get请求服务器发送数据的方式(普通和json)
- Android 网络请求方式之HttpClient,GET,POST,PUT请求使用方法
- Android使用HttpClient完成Post和Get方式的联网请求
- android之使用get和post方式向服务器提交请求
- android中post和get两种方式发送请求