Android开发中的几种网络请求方式详解
程序员文章站
2024-03-01 15:30:40
android应用经常会和服务器端交互,这就需要手机客户端发送网络请求,下面介绍四种常用网络请求方式,我这边是通过android单元测试来完成这四种方法的,还不清楚andr...
android应用经常会和服务器端交互,这就需要手机客户端发送网络请求,下面介绍四种常用网络请求方式,我这边是通过android单元测试来完成这四种方法的,还不清楚android的单元测试的同学们请看android开发技巧总结中的android单元测试的步骤一文。
java.net包中的httpurlconnection类
get方式:
// get方式请求 public static void requestbyget() throws exception { string path = "//www.jb51.net/logins.jsp?id=helloworld&pwd=android"; // 新建一个url对象 url url = new url(path); // 打开一个httpurlconnection连接 httpurlconnection urlconn = (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, new string(data, "utf-8")); } else { log.i(tag_get, "get方式请求失败"); } // 关闭连接 urlconn.disconnect(); }
post方式:
// post方式请求 public static void requestbypost() throws throwable { string path = "//www.jb51.net/logins.jsp"; // 请求的参数转换为byte数组 string params = "id=" + urlencoder.encode("helloworld", "utf-8") + "&pwd=" + urlencoder.encode("android", "utf-8"); byte[] postdata = params.getbytes(); // 新建一个url对象 url url = new url(path); // 打开一个httpurlconnection连接 httpurlconnection urlconn = (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(); // 发送请求参数 dataoutputstream dos = new dataoutputstream(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, new string(data, "utf-8")); } else { log.i(tag_post, "post方式请求失败"); } }
org.apache.http包中的httpget和httppost类
get方式:
// httpget方式请求 public static void requestbyhttpget() throws exception { string path = "//www.jb51.net/logins.jsp?id=helloworld&pwd=android"; // 新建httpget对象 httpget httpget = new httpget(path); // 获取httpclient对象 httpclient httpclient = new defaulthttpclient(); // 获取httpresponse实例 httpresponse httpresp = httpclient.execute(httpget); // 判断是够请求成功 if (httpresp.getstatusline().getstatuscode() == http_200) { // 获取返回的数据 string result = entityutils.tostring(httpresp.getentity(), "utf-8"); log.i(tag_httpget, "httpget方式请求成功,返回数据如下:"); log.i(tag_httpget, result); } else { log.i(tag_httpget, "httpget方式请求失败"); } }
post方式:
// httppost方式请求 public static void requestbyhttppost() throws exception { string path = "//www.jb51.net/"; // 新建httppost对象 httppost httppost = new httppost(path); // post参数 list<namevaluepair> params = new arraylist<namevaluepair>(); params.add(new basicnamevaluepair("id", "helloworld")); params.add(new basicnamevaluepair("pwd", "android")); // 设置字符集 httpentity entity = new urlencodedformentity(params, http.utf_8); // 设置参数实体 httppost.setentity(entity); // 获取httpclient对象 httpclient httpclient = new defaulthttpclient(); // 获取httpresponse实例 httpresponse httpresp = httpclient.execute(httppost); // 判断是够请求成功 if (httpresp.getstatusline().getstatuscode() == http_200) { // 获取返回的数据 string result = entityutils.tostring(httpresp.getentity(), "utf-8"); log.i(tag_httpget, "httppost方式请求成功,返回数据如下:"); log.i(tag_httpget, result); } else { log.i(tag_httpget, "httppost方式请求失败"); } }
以上是一些部分代码,测试的时候在测试类中运行对应的测试方法即可。完整代码点这里下载:
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!