android中http访问总结
2015-04-01 21:49
225 查看
在进行,android访问tomcat服务器时候,开发的基本步骤
1.使用URL访问指定的网络资源
2.使用URLConnection提交请求
A.通过调用url对象的openConnection()方法来创建URLConnection对象
B.设置URLConnection的参数和普通请求属性
Get请求
Post请求:
利用Apache HttpClient发送get和post请求
发送post请求:
1.使用URL访问指定的网络资源
URL url = new URL(“HTTP://.........”); InputStream ins = url.openStream();
2.使用URLConnection提交请求
A.通过调用url对象的openConnection()方法来创建URLConnection对象
B.设置URLConnection的参数和普通请求属性
Get请求
/** * 向指定URL发送GET方法的请求 * * @param url * 发送请求的URL * @param params * 请求参数,请求参数应该是name1=value1&name2=value2的形式。 * @return URL所代表远程资源的响应 */ String urlName = url + "?" + params; URL realUrl = new URL(urlName); // 打开和URL之间的连接 URLConnection conn = realUrl.openConnection(); // 设置通用的请求属性 conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); // 建立实际的连接 conn.connect(); // 获取所有响应头字段 Map<String, List<String>> map = conn.getHeaderFields(); // 遍历所有的响应头字段 for (String key : map.keySet()) { System.out.println(key + "--->" + map.get(key)); } // 定义BufferedReader输入流来读取URL的响应 in = new BufferedReader( new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { result += "\n" + line; }
Post请求:
/** * 向指定URL发送POST方法的请求 * * @param url * 发送请求的URL * @param params * 请求参数,请求参数应该是name1=value1&name2=value2的形式。 * @return URL所代表远程资源的响应 */ PrintWriter out = null; BufferedReader in = null; String result = ""; URL realUrl = new URL(url); // 打开和URL之间的连接 URLConnection conn = realUrl.openConnection(); // 设置通用的请求属性 conn.setRequestProperty("accept", "*/*"); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)"); // 发送POST请求必须设置如下两行 conn.setDoOutput(true); conn.setDoInput(true); // 获取URLConnection对象对应的输出流 out = new PrintWriter(conn.getOutputStream()); // 发送请求参数 out.print(params); // flush输出流的缓冲 out.flush(); // 定义BufferedReader输入流来读取URL的响应 in = new BufferedReader( new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { result += "\n" + line; }
利用Apache HttpClient发送get和post请求
// 创建DefaultHttpClient对象 httpClient = new DefaultHttpClient(); // 创建一个HttpGet对象 HttpGet get = new HttpGet("http://192.168.1.88:8888/foo/secret.jsp"); // 发送GET请求 HttpResponse httpResponse = httpClient.execute(get); HttpEntity entity = httpResponse.getEntity(); // 读取服务器响应 BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent())); String line = null; response.setText(""); while ((line = br.readLine()) != null) { // 使用response文本框显示服务器响应 response.append(line + "\n"); }
发送post请求:
HttpPost post = new HttpPost("http://192.168.1.88:8888/foo/login.jsp"); // 如果传递参数个数比较多的话可以对传递的参数进行封装 List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("name", name)); params.add(new BasicNameValuePair("pass", pass)); // 设置请求参数 post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); // 发送POST请求 HttpResponse response = httpClient.execute(post); // 如果服务器成功地返回响应 if (response.getStatusLine.getStatusCode() == 200) { String msg = EntityUtils.toString(response.getEntity()); // 提示登录成功 Toast.makeText(HttpClientTest.this,msg, 5000).show(); }
相关文章推荐
- 关于Android网络访问请求的一些总结 包括HttpClient HttpUrlConnection Volley
- Java访问权限修饰符friendly----同一个包内其它类可以访问(钦波总结大全)(转http://huiqinbo.javaeye.com/blog/577956)
- Android新手之旅(4) 通过HTTP访问web
- 一个android访问http资源的便捷工具类——HttpHelper
- Android 程序访问WEB服务 HTTP 接口使用
- 【Cocos2dx通信(Http&Socket)相关编译到Android细节总结】编译加入curl关联lib与头文件 && 解决pthread的cancel函数NDK不支持,找不到sockaddr_i
- Android通过Apache HTTP访问HTTP资源
- Android permission 访问权限大全 (转http://www.blogjava.net/zdq5337/archive/2010/07/16/326266.html)
- Android进阶2之Http操作访问网络
- 一个android访问http资源的便捷工具类——HttpHelper
- 一个android访问http资源的便捷工具类——HttpHelper
- Android 客户端通过内置API(HttpClient) 访问 服务器(用Spring MVC 架构) 返回的json数据全过程
- Android开发进阶(四)--深入Android通过Apache HTTP访问HTTP资源
- http://android.git.kernel.org/不能访问了
- Android进阶2之Http操作访问网络
- 【原创】一个android访问http资源的便捷工具类——HttpHelper
- Android访问权限总结
- Android HTTP请求访问的方法
- Android通过Apache HTTP访问HTTP资源
- Android非UI线程访问UI线程的方法总结