你好,游客 登录 注册 搜索
背景:
阅读新闻

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

[日期:2011-12-17] 来源:Linux社区  作者:YangLian20009 [字体:大 中 小]
//GetAndPostRequestActivity.java 主要的控制类
  1. package sn.len.getandpostreq;  
  2.   
  3. import java.util.HashMap;  
  4. import java.util.Map;  
  5.   
  6. import sn.len.request.RequestService;  
  7. import Android.app.Activity;  
  8. import android.os.Bundle;  
  9. import android.util.Log;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.widget.EditText;  
  13. import android.widget.Toast;  
  14.   
  15. public class GetAndPostRequestActivity extends Activity implements OnClickListener  
  16. {  
  17.     @Override  
  18.     public void onCreate(Bundle savedInstanceState)   
  19.     {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.main);  
  22.         View get_button=findViewById(R.id.get_reqeust);  
  23.         View post_button=findViewById(R.id.post_reqeust);  
  24.         get_button.setOnClickListener(this);  
  25.         post_button.setOnClickListener(this);  
  26.     }  
  27.   
  28.     @Override  
  29.     public void onClick(View v)   
  30.     {  
  31.         EditText book_name=(EditText)findViewById(R.id.book_name);  
  32.         EditText book_price=(EditText)findViewById(R.id.book_price);  
  33.         String bookname=book_name.getText().toString();  
  34.         String bookprice=book_price.getText().toString();  
  35.         switch(v.getId())  
  36.         {  
  37.             case R.id.get_reqeust: //Get请求   
  38.             {  
  39.                 //第一种可以用字符串拼接   
  40.                 String urlPath="http://192.168.0.133/web/index.jsp"+"?type=save&book_name="+bookname+"&book_price="+bookprice+"";  
  41.                 String realPath=urlPath.replaceAll(" ", "");//把多余的空格替换掉   
  42.                 try   
  43.                 {  
  44.                     if(RequestService.getRequest(realPath))  
  45.                     {  
  46.                         Toast.makeText(this, R.string.success, Toast.LENGTH_LONG).show();  
  47.                     }  
  48.                 }  
  49.                 catch (Exception e)   
  50.                 {  
  51.                     Toast.makeText(this, R.string.error, Toast.LENGTH_LONG).show();  
  52.                     e.printStackTrace();  
  53.                 }  
  54.             }break;  
  55.             case R.id.post_reqeust: //Post请求   
  56.             {  
  57.                 String urlPath="http://192.168.0.133/web/index.jsp";  
  58.                 Map<String,String> map=new HashMap<String,String>();//用集合来做,比字符串拼接来得直观   
  59.                 map.put("type", "save");  
  60.                 map.put("book_name",bookname);  
  61.                 map.put("book_price",bookprice);  
  62.                 try   
  63.                 {  
  64.                     if(RequestService.postRequest(urlPath, map))  
  65.                     {  
  66.                         Toast.makeText(this, R.string.success, Toast.LENGTH_LONG).show();  
  67.                     }  
  68.                 }   
  69.                 catch (Exception e)   
  70.                 {  
  71.                     Toast.makeText(this, R.string.error, Toast.LENGTH_LONG).show();  
  72.                     Log.e("ERRORS", e.toString());  
  73.                     e.printStackTrace();  
  74.                 }  
  75.             }break;  
  76.             default:break;  
  77.         }  
  78.     }  
  79.       
  80. }  
//服务器端代码用JSP实现,就没有写Servlet了
  1. <%@page contentType="text/html" pageEncoding="GBK" language="java"%>  
  2. <%  
  3.     String type=request.getParameter("type");  
  4.     if(type!=null && !"".equals(type))  
  5.     {  
  6.         if(type.equals("save"))  
  7.         {  
  8.             String book_name=request.getParameter("book_name");  
  9.             String book_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服务端响应效果,书名和价格都已经打出来了。

linux
相关资讯       Android开发教程 
本文评论   查看全部评论 (3)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款
第 3 楼
* 匿名 发表于 2013/1/6 23:05:39
为什么我的一直乱码?
第 2 楼
* aaa 发表于 2012/5/8 20:14:54
aa
第 1 楼
* 宋 发表于 2012/2/15 10:46:12
提交失败是怎么回事啊