使用HttpURLConnection采用Post方式请求数据

来源:互联网 发布:共享文件夹的网络路径 编辑:程序博客网 时间:2024/06/05 20:41

1.      服务端

doPost(){

doGet(request,response);

}

2.      Post方式不在URL后面加数据,而是用流的方式传递;GET在URL后传输数据

3.      是否传递---请求头:setRequestProperty();


MainActivity中:

[java] view plain copy
  1. public void doPost(View v) {  
  2.         final String userName = etUserName.getText().toString();  
  3.         final String password = etPassword.getText().toString();  
  4.           
  5.         new Thread(new Runnable() {  
  6.             @Override  
  7.             public void run() {  
  8.                 final String state = NetUtils.loginOfPost(userName, password);  
  9.                 // 执行任务在主线程中  
  10.                 runOnUiThread(new Runnable() {  
  11.                     @Override  
  12.                     public void run() {  
  13.                         // 就是在主线程中操作  
  14.                         Toast.makeText(MainActivity.this, state, 0).show();  
  15.                     }  
  16.                 });  
  17.             }  
  18.         }).start();  
  19.     }  

loginOfPost方法:

[java] view plain copy
  1. /** 
  2.  * 使用post的方式登录 
  3.  * @param userName 
  4.  * @param password 
  5.  * @return   
  6.  */  
  7. public static String loginOfPost(String userName, String password) {  
  8.     HttpURLConnection conn = null;  
  9.     try {  
  10.         URL url = new URL("http://10.0.2.2:8080/ServerItheima28/servlet/LoginServlet");  
  11.           
  12.         conn = (HttpURLConnection) url.openConnection();  
  13.           
  14.         conn.setRequestMethod("POST");  
  15.         conn.setConnectTimeout(10000); // 连接的超时时间  
  16.         conn.setReadTimeout(5000); // 读数据的超时时间  
  17.         conn.setDoOutput(true); // 必须设置此方法, 允许输出****** 默认情况下, 系统不允许向服务器输出内容  
  18. /           conn.setRequestProperty("Content-Length"234);     // 设置请求头消息, 可以设置多个  
  19.           
  20.         // post请求的参数  
  21.         String data = "username=" + userName + "&password=" + password;  
  22.           
  23.         // 获得一个输出流, 用于向服务器写数据, 默认情况下, 系统不允许向服务器输出内容  
  24.         OutputStream out = conn.getOutputStream();    
  25.         out.write(data.getBytes());  
  26.         out.flush();  
  27.         out.close();  
  28.           
  29.         int responseCode = conn.getResponseCode();  
  30.         if(responseCode == 200) {  
  31.             InputStream is = conn.getInputStream();  
  32.             String state = getStringFromInputStream(is);  
  33.             return state;  
  34.         } else {  
  35.             Log.i(TAG, "访问失败: " + responseCode);  
  36.         }  
  37.     } catch (Exception e) {  
  38.         e.printStackTrace();  
  39.     } finally {  
  40.         if(conn != null) {  
  41.             conn.disconnect();  
  42.         }  
  43.     }  
  44.     return null;  
  45. }  

根据流返回一个字符串信息;

[java] view plain copy
  1. /** 
  2.      * 根据流返回一个字符串信息 
  3.      * @param is 
  4.      * @return 
  5.      * @throws IOException  
  6.      */  
  7.     private static String getStringFromInputStream(InputStream is) throws IOException {  
  8.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  9.         byte[] buffer = new byte[1024];  
  10.         int len = -1;  
  11.           
  12.         while((len = is.read(buffer)) != -1) {  
  13.             baos.write(buffer, 0, len);  
  14.         }  
  15.         is.close();  
  16.           
  17.         String html = baos.toString();  // 把流中的数据转换成字符串, 采用的编码是: utf-8  
  18.           
  19. //      String html = new String(baos.toByteArray(), "GBK");  
  20.           
  21.         baos.close();  
  22.         return html;  
  23.     }  
0 0
原创粉丝点击