//目录结构


//strings.xml字符常量文件

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <stringname="title">通过Get和Post两种方式分别提交数据到服务器</string>
  4. <stringname="app_name">GetAndPostRequest</string>
  5. <stringname="book_name">书本名称</string>
  6. <stringname="book_price">书本价格</string>
  7. <stringname="success">提交成功</string>
  8. <stringname="error">提交失败</string>
  9. <stringname="get_request">Get请求提交</string>
  10. <stringname="post_request">Post请求提交</string>
  11. </resources>
//main.xml 布局文件
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns: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. packagesn.len.request;
  2. importjava.io.OutputStream;
  3. importjava.net.HttpURLConnection;
  4. importjava.net.URL;
  5. importjava.net.URLEncoder;
  6. importjava.util.Map;
  7. publicclassRequestService
  8. {
  9. //get请求,有文件长度大小限制
  10. publicstaticbooleangetRequest(StringurlPath)throwsException
  11. {
  12. URLurl=newURL(urlPath);
  13. HttpURLConnectioncon=(HttpURLConnection)url.openConnection();
  14. con.setRequestMethod("GET");
  15. con.setReadTimeout(5*1000);
  16. if(con.getResponseCode()==200)
  17. {
  18. returntrue;
  19. }
  20. returnfalse;
  21. }
  22. //post请求,无文件长度大小限制
  23. publicstaticbooleanpostRequest(StringurlPath,Map<String,String>map)throwsException
  24. {
  25. StringBuilderbuilder=newStringBuilder();//拼接字符
  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. byteb[]=builder.toString().getBytes();
  37. URLurl=newURL(urlPath);
  38. HttpURLConnectioncon=(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. OutputStreamoutStream=con.getOutputStream();
  45. outStream.write(b);//写入数据
  46. outStream.flush();//刷新内存
  47. outStream.close();
  48. //状态码是不成功
  49. if(con.getResponseCode()==200)
  50. {
  51. returntrue;
  52. }
  53. returnfalse;
  54. }
  55. }
//GetAndPostRequestActivity.java 主要的控制类
  1. packagesn.len.getandpostreq;
  2. importjava.util.HashMap;
  3. importjava.util.Map;
  4. importsn.len.request.RequestService;
  5. importAndroid.app.Activity;
  6. importandroid.os.Bundle;
  7. importandroid.util.Log;
  8. importandroid.view.View;
  9. importandroid.view.View.OnClickListener;
  10. importandroid.widget.EditText;
  11. importandroid.widget.Toast;
  12. publicclassGetAndPostRequestActivityextendsActivityimplementsOnClickListener
  13. {
  14. @Override
  15. publicvoidonCreate(BundlesavedInstanceState)
  16. {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.main);
  19. Viewget_button=findViewById(R.id.get_reqeust);
  20. Viewpost_button=findViewById(R.id.post_reqeust);
  21. get_button.setOnClickListener(this);
  22. post_button.setOnClickListener(this);
  23. }
  24. @Override
  25. publicvoidonClick(Viewv)
  26. {
  27. EditTextbook_name=(EditText)findViewById(R.id.book_name);
  28. EditTextbook_price=(EditText)findViewById(R.id.book_price);
  29. Stringbookname=book_name.getText().toString();
  30. Stringbookprice=book_price.getText().toString();
  31. switch(v.getId())
  32. {
  33. caseR.id.get_reqeust://Get请求
  34. {
  35. //第一种可以用字符串拼接
  36. StringurlPath="http://192.168.0.133/web/index.jsp"+"?type=save&book_name="+bookname+"&book_price="+bookprice+"";
  37. StringrealPath=urlPath.replaceAll("","");//把多余的空格替换掉
  38. try
  39. {
  40. if(RequestService.getRequest(realPath))
  41. {
  42. Toast.makeText(this,R.string.success,Toast.LENGTH_LONG).show();
  43. }
  44. }
  45. catch(Exceptione)
  46. {
  47. Toast.makeText(this,R.string.error,Toast.LENGTH_LONG).show();
  48. e.printStackTrace();
  49. }
  50. }break;
  51. caseR.id.post_reqeust://Post请求
  52. {
  53. StringurlPath="http://192.168.0.133/web/index.jsp";
  54. Map<String,String>map=newHashMap<String,String>();//用集合来做,比字符串拼接来得直观
  55. map.put("type","save");
  56. map.put("book_name",bookname);
  57. map.put("book_price",bookprice);
  58. try
  59. {
  60. if(RequestService.postRequest(urlPath,map))
  61. {
  62. Toast.makeText(this,R.string.success,Toast.LENGTH_LONG).show();
  63. }
  64. }
  65. catch(Exceptione)
  66. {
  67. Toast.makeText(this,R.string.error,Toast.LENGTH_LONG).show();
  68. Log.e("ERRORS",e.toString());
  69. e.printStackTrace();
  70. }
  71. }break;
  72. default:break;
  73. }
  74. }
  75. }
//服务器端代码用JSP实现,就没有写Servlet了
  1. <%@pagecontentType="text/html"pageEncoding="GBK"language="java"%>
  2. <%
  3. Stringtype=request.getParameter("type");
  4. if(type!=null&&!"".equals(type))
  5. {
  6. if(type.equals("save"))
  7. {
  8. Stringbook_name=request.getParameter("book_name");
  9. Stringbook_price=request.getParameter("book_price");
  10. if((book_name!=null&&!"".equals(book_name))&&(book_price!=null&&!"".equals(book_price)))
  11. {
  12. System.out.println("书名"+book_name);
  13. System.out.println("价格"+book_price);
  14. }
  15. }
  16. }
  17. %>
//效果


//Tomcat服务端响应效果,书名和价格都已经打出来了。


更多相关文章

  1. 一款常用的 Squid 日志分析工具
  2. GitHub 标星 8K+!一款开源替代 ls 的工具你值得拥有!
  3. RHEL 6 下 DHCP+TFTP+FTP+PXE+Kickstart 实现无人值守安装
  4. Linux 环境下实战 Rsync 备份工具及配置 rsync+inotify 实时同步
  5. Android(安卓)Studio关联源代码
  6. Android(安卓)常用技术框架的基本使用
  7. Retrofit +OKHttp简单封装使用
  8. Android配置文件权限一览表
  9. 头像图片任意截取

随机推荐

  1. Android之EditText特殊小技巧
  2. 手机QQ桌面2.0(Android)发布:新增主题模式
  3. Android内存管理机制之一:lowmemory kille
  4. 填坑总结:通过selector的android:state_ch
  5. Android高手进阶教程(三)之----Android(
  6. Android事件分发机制及如何解决事件冲突
  7. 一年学习总结和分享android广告赚钱
  8. android style(样式)和theme(主题)设置
  9. Android(安卓)多线程-----AsyncTask详解
  10. Android(安卓)主题之安装的APK主题文件