歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android GET,POST向服務器端發送數據(發送)

Android GET,POST向服務器端發送數據(發送)

日期:2017/3/1 10:47:17   编辑:Linux編程

//目錄結構


//strings.xml字符常量文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string name="title">通過Get和Post兩種方式分別提交數據到服務器</string>
  4. <string name="app_name">GetAndPostRequest</string>
  5. <string name="book_name">書本名稱</string>
  6. <string name="book_price">書本價格</string>
  7. <string name="success">提交成功</string>
  8. <string name="error">提交失敗</string>
  9. <string name="get_request">Get請求提交</string>
  10. <string name="post_request">Post請求提交</string>
  11. </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. <TextView
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. android:text="@string/title" />
  10. <TextView
  11. android:layout_width="fill_parent"
  12. android:layout_height="wrap_content"
  13. android:text="@string/book_name"
  14. />
  15. <EditText
  16. android:id="@+id/book_name"
  17. android:layout_width="fill_parent"
  18. android:layout_height="wrap_content"
  19. />
  20. <TextView
  21. android:layout_width="fill_parent"
  22. android:layout_height="wrap_content"
  23. android:text="@string/book_price"
  24. />
  25. <EditText
  26. android:id="@+id/book_price"
  27. android:numeric="integer"
  28. android:layout_width="fill_parent"
  29. android:layout_height="wrap_content"
  30. />
  31. <LinearLayout
  32. android:orientation="horizontal"
  33. android:layout_width="fill_parent"
  34. android:layout_height="wrap_content"
  35. >
  36. <Button
  37. android:id="@+id/get_reqeust"
  38. android:layout_width="wrap_content"
  39. android:layout_height="wrap_content"
  40. android:text="@string/get_request"
  41. />
  42. <Button
  43. android:id="@+id/post_reqeust"
  44. android:layout_width="wrap_content"
  45. android:layout_height="wrap_content"
  46. android:text="@string/post_request"
  47. />
  48. </LinearLayout>
  49. </LinearLayout>
//RequestService.java 通過GET 和 Post請求的類
  1. package sn.len.request;
  2. import java.io.OutputStream;
  3. import java.net.HttpURLConnection;
  4. import java.net.URL;
  5. import java.net.URLEncoder;
  6. import java.util.Map;
  7. public class RequestService
  8. {
  9. //get請求,有文件長度大小限制
  10. public static boolean getRequest(String urlPath) throws Exception
  11. {
  12. URL url=new URL(urlPath);
  13. HttpURLConnection con=(HttpURLConnection)url.openConnection();
  14. con.setRequestMethod("GET");
  15. con.setReadTimeout(5*1000);
  16. if(con.getResponseCode()==200)
  17. {
  18. return true;
  19. }
  20. return false;
  21. }
  22. //post請求,無文件長度大小限制
  23. public static boolean postRequest(String urlPath,Map<String,String> map) throws Exception
  24. {
  25. StringBuilder builder=new StringBuilder(); //拼接字符
  26. //拿出鍵值
  27. if(map!=null && !map.isEmpty())
  28. {
  29. for(Map.Entry<String, String> param:map.entrySet())
  30. {
  31. builder.append(param.getKey()).append('=').append(URLEncoder.encode(param.getValue(), "utf-8")).append('&');
  32. }
  33. builder.deleteCharAt(builder.length()-1);
  34. }
  35. //下面的Content-Length: 是這個URL的二進制數據長度
  36. byte b[]=builder.toString().getBytes();
  37. URL url=new URL(urlPath);
  38. HttpURLConnection con=(HttpURLConnection)url.openConnection();
  39. con.setRequestMethod("POST");
  40. con.setReadTimeout(5*1000);
  41. con.setDoOutput(true);//打開向外輸出
  42. con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");//內容類型
  43. con.setRequestProperty("Content-Length",String.valueOf(b.length));//長度
  44. OutputStream outStream=con.getOutputStream();
  45. outStream.write(b);//寫入數據
  46. outStream.flush();//刷新內存
  47. outStream.close();
  48. //狀態碼是不成功
  49. if(con.getResponseCode()==200)
  50. {
  51. return true;
  52. }
  53. return false;
  54. }
  55. }
Copyright © Linux教程網 All Rights Reserved