在进行,android访问tomcat服务器时候,开发的基本步骤
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(); }