Android平台向web应用get、post方式提交信息案例


1、效果图展示


2、界面布局

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <LinearLayout  
  8.     android:orientation="horizontal"  
  9.     android:layout_width="fill_parent"  
  10.     android:layout_height="wrap_content"  
  11.     android:gravity="center"  
  12.     >  
  13. <Button  
  14.     android:id="@+id/get"    
  15.     android:layout_width="wrap_content"   
  16.     android:layout_height="wrap_content"   
  17.     android:text="@string/get"  
  18.     />  
  19. <Button  
  20.     android:id="@+id/post"    
  21.     android:layout_width="wrap_content"   
  22.     android:layout_height="wrap_content"   
  23.     android:text="@string/post"  
  24.     />         
  25. </LinearLayout>     
  26. <EditText    
  27.     android:id="@+id/show"  
  28.     android:layout_width="fill_parent"   
  29.     android:layout_height="fill_parent"   
  30.     android:editable="false"  
  31.     android:cursorVisible="false"  
  32.     android:gravity="top"  
  33.     />  
  34. </LinearLayout>  
3、改发送get、post请求的工具类,如下:
  1. public class GetPostUtil  
  2. {  
  3.     /** 
  4.      * 向指定URL发送GET方法的请求 
  5.      *  
  6.      * @param url 
  7.      *            发送请求的URL 
  8.      * @param params 
  9.      *            请求参数,请求参数应该是name1=value1&name2=value2的形式。 
  10.      * @return URL所代表远程资源的响应 
  11.      */  
  12.     public static String sendGet(String url, String params)  
  13.     {  
  14.         String result = "";  
  15.         BufferedReader in = null;  
  16.         try  
  17.         {  
  18.             String urlName = url + "?" + params;  
  19.             URL realUrl = new URL(urlName);  
  20.             // 打开和URL之间的连接   
  21.             URLConnection conn = realUrl.openConnection();  
  22.             // 设置通用的请求属性   
  23.             conn.setRequestProperty("accept""*/*");  
  24.             conn.setRequestProperty("connection""Keep-Alive");  
  25.             conn.setRequestProperty("user-agent",  
  26.                 "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");  
  27.             // 建立实际的连接   
  28.             conn.connect();  
  29.             // 获取所有响应头字段   
  30.             Map<String, List<String>> map = conn.getHeaderFields();  
  31.             // 遍历所有的响应头字段   
  32.             for (String key : map.keySet())  
  33.             {  
  34.                 System.out.println(key + "--->" + map.get(key));  
  35.             }  
  36.             // 定义BufferedReader输入流来读取URL的响应   
  37.             in = new BufferedReader(  
  38.                 new InputStreamReader(conn.getInputStream()));  
  39.             String line;  
  40.             while ((line = in.readLine()) != null)  
  41.             {  
  42.                 result += "\n" + line;  
  43.             }  
  44.         }  
  45.         catch (Exception e)  
  46.         {  
  47.             System.out.println("发送GET请求出现异常!" + e);  
  48.             e.printStackTrace();  
  49.         }  
  50.         // 使用finally块来关闭输入流   
  51.         finally  
  52.         {  
  53.             try  
  54.             {  
  55.                 if (in != null)  
  56.                 {  
  57.                     in.close();  
  58.                 }  
  59.             }  
  60.             catch (IOException ex)  
  61.             {  
  62.                 ex.printStackTrace();  
  63.             }  
  64.         }  
  65.         return result;  
  66.     }  
  67.   
  68.     /**  
  69.      * 向指定URL发送POST方法的请求  
  70.      *   
  71.      * @param url  
  72.      *            发送请求的URL  
  73.      * @param params  
  74.      *            请求参数,请求参数应该是name1=value1&name2=value2的形式。  
  75.      * @return URL所代表远程资源的响应  
  76.      */  
  77.     public static String sendPost(String url, String params)  
  78.     {  
  79.         PrintWriter out = null;  
  80.         BufferedReader in = null;  
  81.         String result = "";  
  82.         try  
  83.         {  
  84.             URL realUrl = new URL(url);  
  85.             // 打开和URL之间的连接   
  86.             URLConnection conn = realUrl.openConnection();  
  87.             // 设置通用的请求属性   
  88.               
  89.             conn.setRequestProperty("accept""*/*");  
  90.             conn.setRequestProperty("connection""Keep-Alive");  
  91.             conn.setRequestProperty("user-agent",  
  92.                 "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");  
  93.             // 发送POST请求必须设置如下两行   
  94.             conn.setDoOutput(true);  
  95.             conn.setDoInput(true);  
  96.             // 获取URLConnection对象对应的输出流   
  97.             out = new PrintWriter(conn.getOutputStream());  
  98.             // 发送请求参数   
  99.             out.print(params);  
  100.             // flush输出流的缓冲   
  101.             out.flush();  
  102.             // 定义BufferedReade r输入流来读取URL的响应   
  103.             in = new BufferedReader(  
  104.                 new InputStreamReader(conn.getInputStream()));  
  105.             String line;  
  106.             while ((line = in.readLine()) != null)  
  107.             {  
  108.                 result += "\n" + line;  
  109.             }  
  110.         }  
  111.         catch (Exception e)  
  112.         {  
  113.             System.out.println("发送POST请求出现异常!" + e);  
  114.             e.printStackTrace();  
  115.         }  
  116.         // 使用finally块来关闭输出流、输入流   
  117.         finally  
  118.         {  
  119.             try  
  120.             {  
  121.                 if (out != null)  
  122.                 {  
  123.                     out.close();  
  124.                 }  
  125.                 if (in != null)  
  126.                 {  
  127.                     in.close();  
  128.                 }  
  129.             }  
  130.             catch (IOException ex)  
  131.             {  
  132.                 ex.printStackTrace();  
  133.             }  
  134.         }  
  135.         return result;  
  136.     }  
  137. }  
如果需要发送get请求只要调用URLConnection的connect()方法去建立实际的连接即可;如果需要发送post请求,则需要获取URLConnection的OutputStream,然后再向网络中输出请求参数,如以上程序!!!

4、activity程序代码

  1. public class GetPostMain extends Activity  
  2. {  
  3.     Button get , post;  
  4.     EditText show;  
  5.     @Override  
  6.     public void onCreate(Bundle savedInstanceState)  
  7.     {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.main);  
  10.         get = (Button) findViewById(R.id.get);  
  11.         post = (Button) findViewById(R.id.post);  
  12.         show = (EditText)findViewById(R.id.show);  
  13.         get.setOnClickListener(new OnClickListener()  
  14.         {  
  15.             @Override  
  16.             public void onClick(View v)  
  17.             {  
  18.                 String response = GetPostUtil  
  19.                     .sendGet("http://192.168.65.1:8080/abc/a.jsp" , null);  
  20.                 show.setText(response);  
  21.                   
  22.             }             
  23.         });  
  24.         post.setOnClickListener(new OnClickListener()  
  25.         {  
  26.             @Override  
  27.             public void onClick(View v)  
  28.             {  
  29.                 String response = GetPostUtil  
  30.                     .sendPost("http://192.168.65.1:8080/abc/login.jsp"  
  31.                     , "name=crazyit.org&pass=leegang");  
  32.                 show.setText(response);  
  33.                   
  34.             }             
  35.         });   
  36.     }  
  37. }  
该程序所发送的get、post请求都是向本地局域网内:http://192/168.65.1:8080/abc应用下两个页面发送,这个应用都是部署在本机的web应用;

相关内容