Android GET,POST向服务器端发送数据(发送)


//目录结构


//strings.xml字符常量文件

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   
  4.     <string name="title">通过Get和Post两种方式分别提交数据到服务器</string>  
  5.     <string name="app_name">GetAndPostRequest</string>  
  6.     <string name="book_name">书本名称</string>  
  7.     <string name="book_price">书本价格</string>  
  8.     <string name="success">提交成功</string>  
  9.     <string name="error">提交失败</string>  
  10.     <string name="get_request">Get请求提交</string>  
  11.     <string name="post_request">Post请求提交</string>  
  12. </resources>  
//main.xml 布局文件
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="@string/title" />  
  11.     <TextView   
  12.         android:layout_width="fill_parent"  
  13.         android:layout_height="wrap_content"  
  14.         android:text="@string/book_name"  
  15.         />  
  16.     <EditText   
  17.         android:id="@+id/book_name"  
  18.         android:layout_width="fill_parent"  
  19.         android:layout_height="wrap_content"  
  20.         />  
  21.     <TextView   
  22.         android:layout_width="fill_parent"  
  23.         android:layout_height="wrap_content"  
  24.         android:text="@string/book_price"  
  25.         />  
  26.     <EditText   
  27.         android:id="@+id/book_price"  
  28.         android:numeric="integer"  
  29.         android:layout_width="fill_parent"  
  30.         android:layout_height="wrap_content"  
  31.         />  
  32.     <LinearLayout   
  33.         android:orientation="horizontal"  
  34.         android:layout_width="fill_parent"  
  35.         android:layout_height="wrap_content"  
  36.         >  
  37.         <Button   
  38.             android:id="@+id/get_reqeust"  
  39.             android:layout_width="wrap_content"  
  40.             android:layout_height="wrap_content"  
  41.             android:text="@string/get_request"  
  42.             />  
  43.         <Button   
  44.             android:id="@+id/post_reqeust"  
  45.             android:layout_width="wrap_content"  
  46.             android:layout_height="wrap_content"  
  47.             android:text="@string/post_request"  
  48.             />  
  49.     </LinearLayout>  
  50.   
  51. </LinearLayout>  
//RequestService.java 通过GET 和 Post请求的类
  1. package sn.len.request;  
  2.   
  3. import java.io.OutputStream;  
  4. import java.net.HttpURLConnection;  
  5. import java.net.URL;  
  6. import java.net.URLEncoder;  
  7. import java.util.Map;  
  8.   
  9. public class RequestService   
  10. {  
  11.     //get请求,有文件长度大小限制   
  12.     public static boolean getRequest(String urlPath) throws Exception  
  13.     {  
  14.         URL url=new URL(urlPath);  
  15.         HttpURLConnection con=(HttpURLConnection)url.openConnection();  
  16.         con.setRequestMethod("GET");  
  17.         con.setReadTimeout(5*1000);  
  18.         if(con.getResponseCode()==200)  
  19.         {  
  20.             return true;  
  21.         }  
  22.         return false;  
  23.     }  
  24.     //post请求,无文件长度大小限制   
  25.     public static boolean postRequest(String urlPath,Map<String,String> map) throws Exception  
  26.     {  
  27.         StringBuilder builder=new StringBuilder(); //拼接字符   
  28.         //拿出键值   
  29.         if(map!=null && !map.isEmpty())  
  30.         {  
  31.             for(Map.Entry<String, String> param:map.entrySet())  
  32.             {  
  33.                 builder.append(param.getKey()).append('=').append(URLEncoder.encode(param.getValue(), "utf-8")).append('&');  
  34.             }  
  35.             builder.deleteCharAt(builder.length()-1);  
  36.         }  
  37.         //下面的Content-Length: 是这个URL的二进制数据长度   
  38.         byte b[]=builder.toString().getBytes();  
  39.         URL url=new URL(urlPath);  
  40.         HttpURLConnection con=(HttpURLConnection)url.openConnection();  
  41.         con.setRequestMethod("POST");  
  42.         con.setReadTimeout(5*1000);  
  43.         con.setDoOutput(true);//打开向外输出   
  44.         con.setRequestProperty("Content-Type""application/x-www-form-urlencoded");//内容类型   
  45.         con.setRequestProperty("Content-Length",String.valueOf(b.length));//长度   
  46.         OutputStream outStream=con.getOutputStream();  
  47.         outStream.write(b);//写入数据   
  48.         outStream.flush();//刷新内存   
  49.         outStream.close();  
  50.         //状态码是不成功   
  51.         if(con.getResponseCode()==200)  
  52.         {  
  53.             return true;  
  54.         }  
  55.         return false;   
  56.           
  57.     }  
  58. }  
  • 1
  • 2
  • 下一页

相关内容