android 网络 post get
16lz
2021-01-23
引用:http://blog.csdn.net/zuolongsnail/article/details/6373051
Android应用经常会和服务器端交互,这就需要手机客户端发送网络请求,下面介绍四种常用网络请求方式,我这边是通过Android单元测试来完成这四种方法的,还不清楚Android的单元测试的同学们请看Android开发技巧总结中的Android单元测试的步骤一文。
java.net包中的HttpURLConnection类
Get方式:
[java]view plaincopy- //Get方式请求
- publicstaticvoidrequestByGet()throwsException{
- Stringpath="https://reg.163.com/logins.jsp?id=helloworld&pwd=android";
- //新建一个URL对象
- URLurl=newURL(path);
- //打开一个HttpURLConnection连接
- HttpURLConnectionurlConn=(HttpURLConnection)url.openConnection();
- //设置连接超时时间
- urlConn.setConnectTimeout(5*1000);
- //开始连接
- urlConn.connect();
- //判断请求是否成功
- if(urlConn.getResponseCode()==HTTP_200){
- //获取返回的数据
- byte[]data=readStream(urlConn.getInputStream());
- Log.i(TAG_GET,"Get方式请求成功,返回数据如下:");
- Log.i(TAG_GET,newString(data,"UTF-8"));
- }else{
- Log.i(TAG_GET,"Get方式请求失败");
- }
- //关闭连接
- urlConn.disconnect();
- }
Post方式:
[java]view plaincopy- //Post方式请求
- publicstaticvoidrequestByPost()throwsThrowable{
- Stringpath="https://reg.163.com/logins.jsp";
- //请求的参数转换为byte数组
- Stringparams="id="+URLEncoder.encode("helloworld","UTF-8")
- +"&pwd="+URLEncoder.encode("android","UTF-8");
- byte[]postData=params.getBytes();
- //新建一个URL对象
- URLurl=newURL(path);
- //打开一个HttpURLConnection连接
- HttpURLConnectionurlConn=(HttpURLConnection)url.openConnection();
- //设置连接超时时间
- urlConn.setConnectTimeout(5*1000);
- //Post请求必须设置允许输出
- urlConn.setDoOutput(true);
- //Post请求不能使用缓存
- urlConn.setUseCaches(false);
- //设置为Post请求
- urlConn.setRequestMethod("POST");
- urlConn.setInstanceFollowRedirects(true);
- //配置请求Content-Type
- urlConn.setRequestProperty("Content-Type",
- "application/x-www-form-urlencode");
- //开始连接
- urlConn.connect();
- //发送请求参数
- DataOutputStreamdos=newDataOutputStream(urlConn.getOutputStream());
- dos.write(postData);
- dos.flush();
- dos.close();
- //判断请求是否成功
- if(urlConn.getResponseCode()==HTTP_200){
- //获取返回的数据
- byte[]data=readStream(urlConn.getInputStream());
- Log.i(TAG_POST,"Post请求方式成功,返回数据如下:");
- Log.i(TAG_POST,newString(data,"UTF-8"));
- }else{
- Log.i(TAG_POST,"Post方式请求失败");
- }
- }
org.apache.http包中的HttpGet和HttpPost类
Get方式:
[java]view plaincopy- //HttpGet方式请求
- publicstaticvoidrequestByHttpGet()throwsException{
- Stringpath="https://reg.163.com/logins.jsp?id=helloworld&pwd=android";
- //新建HttpGet对象
- HttpGethttpGet=newHttpGet(path);
- //获取HttpClient对象
- HttpClienthttpClient=newDefaultHttpClient();
- //获取HttpResponse实例
- HttpResponsehttpResp=httpClient.execute(httpGet);
- //判断是够请求成功
- if(httpResp.getStatusLine().getStatusCode()==HTTP_200){
- //获取返回的数据
- Stringresult=EntityUtils.toString(httpResp.getEntity(),"UTF-8");
- Log.i(TAG_HTTPGET,"HttpGet方式请求成功,返回数据如下:");
- Log.i(TAG_HTTPGET,result);
- }else{
- Log.i(TAG_HTTPGET,"HttpGet方式请求失败");
- }
- }
Post方式:
[java]view plaincopy- //HttpPost方式请求
- publicstaticvoidrequestByHttpPost()throwsException{
- Stringpath="https://reg.163.com/logins.jsp";
- //新建HttpPost对象
- HttpPosthttpPost=newHttpPost(path);
- //Post参数
- List<NameValuePair>params=newArrayList<NameValuePair>();
- params.add(newBasicNameValuePair("id","helloworld"));
- params.add(newBasicNameValuePair("pwd","android"));
- //设置字符集
- HttpEntityentity=newUrlEncodedFormEntity(params,HTTP.UTF_8);
- //设置参数实体
- httpPost.setEntity(entity);
- //获取HttpClient对象
- HttpClienthttpClient=newDefaultHttpClient();
- //获取HttpResponse实例
- HttpResponsehttpResp=httpClient.execute(httpPost);
- //判断是够请求成功
- if(httpResp.getStatusLine().getStatusCode()==HTTP_200){
- //获取返回的数据
- Stringresult=EntityUtils.toString(httpResp.getEntity(),"UTF-8");
- Log.i(TAG_HTTPGET,"HttpPost方式请求成功,返回数据如下:");
- Log.i(TAG_HTTPGET,result);
- }else{
- Log.i(TAG_HTTPGET,"HttpPost方式请求失败");
- }
- }
以上是一些部分代码,测试的时候在测试类中运行对应的测试方法即可。完整代码点这里下载
更多相关文章
- android 应用程序数据共享shareuserid篇+ContentResolver+Conten
- ArcGIS for Android 离线数据编辑原理
- android 多媒体数据库详解
- android移动数据上网的开关的实现
- Android学习笔记-界面和数据存储以及一些零碎知识点
- android listview继承BaseAdapter,自定义的适配器,getView方法执
- Android Activity四种启动方式