让你的APP和你的服务器畅快通讯_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > 让你的APP和你的服务器畅快通讯

让你的APP和你的服务器畅快通讯

 2014/11/26 14:48:05  xiaochao1234  程序员俱乐部  我要评论(0)
  • 摘要:做安卓开发有很多时候都是要和web交互的,我们很难制作本地应用,这次把小弟整出来的安卓和服务器通讯贡献出来,希望能帮到需要的朋友,同时也是加深印象。我们先来搭建安卓客户端,首先写好布局文件:1.布局文件Register.xml:Xml代码<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android
  • 标签:服务器 服务 APP

做安卓开发有很多时候都是要和web交互的,我们很难制作本地应用,这次把小弟整出来的安卓和服务器通讯贡献出来,希望能帮到需要的朋友,同时也是加深印象。

我们先来搭建安卓客户端,首先写好布局文件:

 

1.布局文件Register.xml:

 

Xml代码 javascripts/syntaxhighlighter/clipboard_new.swf" type="application/x-shockwave-flash">ways" /> 
    class="dp-xml" start="1">
  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:background="@drawable/fuction_fond_bg"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <ImageView  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:src="@drawable/setting_btn" >  
  12.     </ImageView>  
  13.   
  14.     <ScrollView  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content" >  
  17.   
  18.         <LinearLayout  
  19.             android:layout_width="fill_parent"  
  20.             android:layout_height="wrap_content"  
  21.             android:layout_gravity="center_horizontal"  
  22.             android:orientation="vertical" >  
  23.   
  24.             <TextView  
  25.                 android:layout_width="fill_parent"  
  26.                 android:layout_height="wrap_content"  
  27.                 android:text="请输入您的注册信息:"  
  28.                 android:textColor="#6495ED"  
  29.                 android:textSize="20dip" >  
  30.             </TextView>  
  31.   
  32.             <View  
  33.                 android:layout_width="fill_parent"  
  34.                 android:layout_height="2dip"  
  35.                 android:background="#FF909090" />  
  36.   
  37.             <LinearLayout  
  38.                 android:layout_width="fill_parent"  
  39.                 android:layout_height="wrap_content"  
  40.                 android:layout_margin="20dip"  
  41.                 android:background="#FFFFFF"  
  42.                 android:orientation="vertical"  
  43.                 android:padding="10dp" >  
  44.   
  45.                 <LinearLayout  
  46.                     android:layout_width="wrap_content"  
  47.                     android:layout_height="wrap_content"  
  48.                     android:orientation="horizontal" >  
  49.   
  50.                     <TextView  
  51.                         android:layout_width="wrap_content"  
  52.                         android:layout_height="wrap_content"  
  53.                         android:text="帐   号:  "  
  54.                         android:textColor="#6495ED" >  
  55.                     </TextView>  
  56.   
  57.                     <EditText  
  58.                         android:id="@+id/username"  
  59.                         android:layout_width="190dip"  
  60.                         android:layout_height="wrap_content" >  
  61.                     </EditText>  
  62.                 </LinearLayout>  
  63.   
  64.                 <View  
  65.                     android:layout_width="fill_parent"  
  66.                     android:layout_height="2dip"  
  67.                     android:background="#FF909090" />  
  68.   
  69.                 <LinearLayout  
  70.                     android:layout_width="wrap_content"  
  71.                     android:layout_height="wrap_content"  
  72.                     android:orientation="horizontal" >  
  73.   
  74.                     <TextView  
  75.                         android:layout_width="wrap_content"  
  76.                         android:layout_height="wrap_content"  
  77.                         android:text="密   码:  "  
  78.                         android:textColor="#6495ED" >  
  79.                     </TextView>  
  80.   
  81.                     <EditText  
  82.                         android:id="@+id/password"  
  83.                         android:layout_width="190dip"  
  84.                         android:layout_height="wrap_content"  
  85.                         android:password="true" >  
  86.                     </EditText>  
  87.                 </LinearLayout>  
  88.   
  89.                 <View  
  90.                     android:layout_width="fill_parent"  
  91.                     android:layout_height="2dip"  
  92.                     android:background="#FF909090" />  
  93.   
  94.                 <LinearLayout  
  95.                     android:layout_width="wrap_content"  
  96.                     android:layout_height="wrap_content"  
  97.                     android:orientation="horizontal" >  
  98.   
  99.                     <TextView  
  100.                         android:layout_width="wrap_content"  
  101.                         android:layout_height="wrap_content"  
  102.                         android:text="确认密码 :  "  
  103.                         android:textColor="#6495ED" >  
  104.                     </TextView>  
  105.   
  106.                     <EditText  
  107.                         android:id="@+id/confirmpsw"  
  108.                         android:layout_width="190dip"  
  109.                         android:layout_height="wrap_content"  
  110.                         android:password="true" >  
  111.                     </EditText>  
  112.                 </LinearLayout>  
  113.   
  114.                 <View  
  115.                     android:layout_width="fill_parent"  
  116.                     android:layout_height="2dip"  
  117.                     android:background="#FF909090" />  
  118.   
  119.                 <LinearLayout  
  120.                     android:layout_width="wrap_content"  
  121.                     android:layout_height="wrap_content"  
  122.                     android:orientation="horizontal" >  
  123.   
  124.                     <TextView  
  125.                         android:layout_width="wrap_content"  
  126.                         android:layout_height="wrap_content"  
  127.                         android:text="昵   称:  "  
  128.                         android:textColor="#6495ED" >  
  129.                     </TextView>  
  130.   
  131.                     <EditText  
  132.                         android:id="@+id/nikename"  
  133.                         android:layout_width="190dip"  
  134.                         android:layout_height="wrap_content" >  
  135.                     </EditText>  
  136.                 </LinearLayout>  
  137.   
  138.                 <View  
  139.                     android:layout_width="fill_parent"  
  140.                     android:layout_height="2dip"  
  141.                     android:background="#FF909090" />  
  142.   
  143.                 <LinearLayout  
  144.                     android:layout_width="wrap_content"  
  145.                     android:layout_height="wrap_content"  
  146.                     android:orientation="horizontal" >  
  147.   
  148.                     <TextView  
  149.                         android:layout_width="wrap_content"  
  150.                         android:layout_height="wrap_content"  
  151.                         android:layout_marginTop="5dp"  
  152.                         android:text="性   别:  "  
  153.                         android:textColor="#6495ED" >  
  154.                     </TextView>  
  155.   
  156.                     <RadioGroup  
  157.                         android:id="@+id/gender_group_rb"  
  158.                         android:layout_width="fill_parent"  
  159.                         android:layout_height="wrap_content"  
  160.                         android:orientation="horizontal" >  
  161.   
  162.                         <RadioButton  
  163.                             android:id="@+id/male_rb"  
  164.                             android:layout_width="wrap_content"  
  165.                             android:layout_height="wrap_content"  
  166.                             android:text="男 "  
  167.                             android:textColor="#6495ED" >  
  168.                         </RadioButton>  
  169.   
  170.                         <RadioButton  
  171.                             android:id="@+id/female_rb"  
  172.                             android:layout_width="wrap_content"  
  173.                             android:layout_height="wrap_content"  
  174.                             android:text="女"  
  175.                             android:textColor="#6495ED" >  
  176.                         </RadioButton>  
  177.                     </RadioGroup>  
  178.                 </LinearLayout>  
  179.   
  180.                 <LinearLayout  
  181.                     android:layout_width="wrap_content"  
  182.                     android:layout_height="wrap_content"  
  183.                     android:orientation="horizontal" >  
  184.   
  185.                     <ImageView  
  186.                         android:layout_width="fill_parent"  
  187.                         android:layout_height="wrap_content" >  
  188.                     </ImageView>  
  189.                 </LinearLayout>  
  190.   
  191.                 <View  
  192.                     android:layout_width="fill_parent"  
  193.                     android:layout_height="2dip"  
  194.                     android:background="#FF909090" />  
  195.   
  196.                 <Button  
  197.                     android:id="@+id/register_btn"  
  198.                     android:layout_width="fill_parent"  
  199.                     android:layout_height="40dp"  
  200.                     android:layout_margin="10dp"  
  201.                     android:background="@drawable/new_button_bg"  
  202.                     android:gravity="center"  
  203.                     android:text="注 册" />  
  204.             </LinearLayout>  
  205.         </LinearLayout>  
  206.     </ScrollView>  
  207.   
  208. </LinearLayout>  

 2.Activity文件:RegisterActivity.java

 

为了让大家看得方便,我逐个为大家讲解:

1.初始化视图控件:

 

Java代码  
  1. /** 
  2.      * 初始化视图控件 
  3.      */  
  4.     private void initView() {  
  5.         username_et = (EditText) findViewById(R.id.username);  
  6.         password_et = (EditText) findViewById(R.id.password);  
  7.         confirmpsw_et = (EditText) findViewById(R.id.confirmpsw);  
  8.         nikename_et = (EditText) findViewById(R.id.nikename);  
  9.         register_btn = (Button) findViewById(R.id.register_btn);  
  10.         register_btn.setOnClickListener(listener);  
  11.         gender_group_rb = (RadioGroup) findViewById(R.id.gender_group_rb);  
  12.         gender_group_rb.setOnCheckedChangeListener(checkListener);  
  13.     }  

 2.当我们填写好数据后,点击注册按钮,调用注册这个方法:

 

 

Java代码 thread%20%3D%20new%20Thread(registerRunnable)%3B%0A%09%09thread.start()%3B%0A%0A%09%7D" /> 
  1. /** 
  2.      * 注册按钮的点击 
  3.      */  
  4.     private OnClickListener listener = new OnClickListener() {  
  5.         @Override  
  6.         public void onClick(View v) {  
  7.             doRegister();  
  8.         }  
  9.     };  
  10.   
  11.     /** 
  12.      * 开始注册 
  13.      */  
  14.     private void doRegister() {  
  15.         pd = ProgressDialog.show(RegisterActivity.this, "正在注册..",  
  16.                 "正在注册中..请稍后....", true, true);  
  17.         // 1.获取数据  
  18.         username = username_et.getText().toString();  
  19.         password = password_et.getText().toString();  
  20.         confirmpsw = confirmpsw_et.getText().toString();  
  21.         nickname = nikename_et.getText().toString();  
  22.         // 2.进行校验,不对返回,否则继续  
  23.         if (checkNull(username) || checkNull(password) || checkNull(confirmpsw)  
  24.                 || checkNull(gender) || checkNull(nickname)) {  
  25.             Toast.makeText(this, "请填写完整数据", Toast.LENGTH_SHORT).show();  
  26.             // TODO 这里本应该完整校验的,暂时偷懒  
  27.             return;  
  28.         }  
  29.         // 3.开始向服务器注册  
  30.         Thread thread = new Thread(registerRunnable);  
  31.         thread.start();  
  32.   
  33.     }  

 因为做的比较简陋,详细的表单校验没有写,点击注册开启一个子线程来处理和服务器打交道的耗时操作。在注册的过程中用的httpUrlConnection连接服务器,向服务器提交注册信息。

 

 

Java代码 HTTP_OK)%20%7B%0A%09%09%09%09%09InputStream%20in%20%3D%20conn.getInputStream()%3B%0A%0A%09%09%09%09%09RegisterResultTool%20resultTool%20%3D%20new%20RegisterResultTool()%3B%0A%09%09%09%09%09String%20result%20%3D%20resultTool.getResultFromInputStream(in)%3B%0A%09%09%09%09%09%2F%2F%204.%E6%8F%90%E4%BA%A4%E7%BB%93%E6%9E%9C%0A%09%09%09%09%09Message%20msg%20%3D%20new%20Message()%3B%0A%09%09%09%09%09msg.obj%20%3D%20result%3B%0A%09%09%09%09%09msg.what%20%3D%20200%3B%0A%09%09%09%09%09registerResultHandler.sendMessage(msg)%3B%0A%0A%09%09%09%09%7D%0A%09%09%09%7D%20catch%20(IOException%20e)%20%7B%0A%09%09%09%09Message%20msg%20%3D%20new%20Message()%3B%0A%09%09%09%09msg.what%20%3D%20400%3B%0A%09%09%09%09registerResultHandler.sendMessage(msg)%3B%0A%09%09%09%09e.printStackTrace()%3B%0A%09%09%09%7D%0A%0A%09%09%09pd.cancel()%3B%0A%09%09%7D%0A%09%7D%3B" /> 
  1. /** 
  2.      * 开始注册的线程 
  3.      */  
  4.     Runnable registerRunnable = new Runnable() {  
  5.         @Override  
  6.         public void run() {  
  7.             // 1.拼装数据  
  8.             StringBuilder sb = new StringBuilder();  
  9.             sb.append("<users>");  
  10.             sb.append("<user>");  
  11.             sb.append("<username>");  
  12.             sb.append(username);  
  13.             sb.append("</username>");  
  14.               
  15.             sb.append("<password>");  
  16.             sb.append(password);  
  17.             sb.append("</password>");  
  18.   
  19.             sb.append("<nickname>");  
  20.             sb.append(nickname);  
  21.             sb.append("</nickname>");  
  22.   
  23.             sb.append("<gender>");  
  24.             sb.append(gender);  
  25.             sb.append("</gender>");  
  26.               
  27.             sb.append("</user>");  
  28.             sb.append("</users>");  
  29.             // 2.开始写数据  
  30.             byte content[] = sb.toString().getBytes();  
  31.             try {  
  32.                 URL url = new URL(Constant.REGISTER_SERVLET);  
  33.                 HttpURLConnection conn = (HttpURLConnection) url  
  34.                         .openConnection();  
  35.                 conn.setDoInput(true);  
  36.                 conn.setDoOutput(true);  
  37.                 conn.setRequestMethod("POST");  
  38.                 conn.setRequestProperty("Content-Type", "mutipart/form-data");  
  39.                 conn.setRequestProperty("Content-Length", content.length + "");  
  40.                 conn.getOutputStream().write(content);  
  41.                 // 3.获取服务器返回的数据  
  42.                 if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {  
  43.                     InputStream in = conn.getInputStream();  
  44.   
  45.                     RegisterResultTool resultTool = new RegisterResultTool();  
  46.                     String result = resultTool.getResultFromInputStream(in);  
  47.                     // 4.提交结果  
  48.                     Message msg = new Message();  
  49.                     msg.obj = result;  
  50.                     msg.what = 200;  
  51.                     registerResultHandler.sendMessage(msg);  
  52.   
  53.                 }  
  54.             } catch (IOException e) {  
  55.                 Message msg = new Message();  
  56.                 msg.what = 400;  
  57.                 registerResultHandler.sendMessage(msg);  
  58.                 e.printStackTrace();  
  59.             }  
  60.   
  61.             pd.cancel();  
  62.         }  
  63.     };  

 向服务器提交数据后,服务器会返回一些结果数据,我们就需要处理这些结果数据:

 

 

Java代码 XMLReader%20xr%20%3D%20sf.newSAXParser().getXMLReader()%3B%0A%0A%09%09%09%09RegisterResultHandler%20rrh%20%3D%20new%20RegisterResultHandler()%3B%0A%09%09%09%09xr.setContentHandler(rrh)%3B%0A%09%09%09%09xr.parse(new%20InputSource(in))%3B%0A%09%09%09%09result%20%3D%20rrh.getResult()%3B%0A%0A%09%09%09%7D%20catch%20(Exception%20e)%20%7B%0A%09%09%09%09e.printStackTrace()%3B%0A%09%09%09%7D%0A%09%09%09return%20result%3B%0A%09%09%7D%0A%0A%09%7D%0A%0A%09%2F**%0A%09%20*%20%E5%9B%A0%E4%B8%BA%E6%9C%8D%E5%8A%A1%E5%99%A8%E8%BF%94%E5%9B%9E%E7%9A%84%E6%98%AFxml%E5%BD%A2%E5%BC%8F%EF%BC%8C%E6%89%80%E4%BB%A5%E9%9C%80%E8%A6%81%E8%A7%A3%E6%9E%90%0A%09%20*%20%0A%09%20*%20%40author%20Larson%0A%09%20*%20%0A%09%20*%2F%0A%09private%20class%20RegisterResultHandler%20extends%20DefaultHandler%20%7B%0A%09%09%2F**%0A%09%09%20*%20%E8%8E%B7%E5%BE%97%E7%9A%84%E6%9C%8D%E5%8A%A1%E5%99%A8%E8%BF%94%E5%9B%9E%E7%BB%93%E6%9E%9C%0A%09%09%20*%2F%0A%09%09private%20String%20result%3B%0A%09%09%2F**%0A%09%09%20*%20%E4%B8%B4%E6%97%B6%E5%AD%98%E5%8F%96%E6%95%B0%E6%8D%AE%E7%9A%84%E5%8F%98%E9%87%8F%0A%09%09%20*%2F%0A%09%09private%20String%20var%3D%22%22%3B%0A%0A%09%09%2F**%0A%09%09%20*%20%E8%8E%B7%E5%8F%96%E8%BF%94%E5%9B%9E%E5%80%BC%0A%09%09%20*%20%0A%09%09%20*%20%40return%0A%09%09%20*%2F%0A%09%09public%20String%20getResult()%20%7B%0A%09%09%09return%20result%3B%0A%09%09%7D%0A%0A%09%09%40Override%0A%09%09public%20void%20startElement(String%20uri%2C%20String%20localName%2C%20String%20qName%2C%0A%09%09%09%09Attributes%20attributes)%20throws%20SAXException%20%7B%0A%09%09%09super.startElement(uri%2C%20localName%2C%20qName%2C%20attributes)%3B%0A%09%09%7D%0A%0A%09%09%40Override%0A%09%09public%20void%20characters(char%5B%5D%20ch%2C%20int%20start%2C%20int%20length)%0A%09%09%09%09throws%20SAXException%20%7B%0A%09%09%09var%20%2B%3D%20new%20String(ch%2C%20start%2C%20length)%3B%0A%09%09%09super.characters(ch%2C%20start%2C%20length)%3B%0A%09%09%7D%0A%0A%09%09%40Override%0A%09%09public%20void%20endElement(String%20uri%2C%20String%20localName%2C%20String%20qName)%0A%09%09%09%09throws%20SAXException%20%7B%0A%09%09%09%2F**%0A%09%09%09%20*%20%E5%8F%96%E5%87%BAinfo%E6%A0%87%E7%AD%BE%E7%9A%84%E6%95%B0%E6%8D%AE%EF%BC%88%E6%9C%8D%E5%8A%A1%E5%99%A8%E8%BF%94%E5%9B%9E%E7%9A%84%E6%B3%A8%E5%86%8C%E7%BB%93%E6%9E%9C%EF%BC%89%0A%09%09%09%20*%2F%0A%09%09%09if%20(qName.equals(%22info%22))%20%7B%0A%09%09%09%09result%20%3D%20var.trim()%3B%0A%09%09%09%7D%0A%09%09%09super.endElement(uri%2C%20localName%2C%20qName)%3B%0A%09%09%7D%0A%09%7D" /> 
  1. /** 
  2.      * 处理服务器返回结果的类 
  3.      *  
  4.      * @author Larson 
  5.      *  
  6.      */  
  7.     private class RegisterResultTool {  
  8.         /** 
  9.          * 从服务器返回的数据中读取返回结果 
  10.          *  
  11.          * @param in 
  12.          * @return 
  13.          */  
  14.         public String getResultFromInputStream(InputStream in) {  
  15.             String result = "";  
  16.             SAXParserFactory sf = SAXParserFactory.newInstance();  
  17.   
  18.             try {  
  19.                 XMLReader xr = sf.newSAXParser().getXMLReader();  
  20.   
  21.                 RegisterResultHandler rrh = new RegisterResultHandler();  
  22.                 xr.setContentHandler(rrh);  
  23.                 xr.parse(new InputSource(in));  
  24.                 result = rrh.getResult();  
  25.   
  26.             } catch (Exception e) {  
  27.                 e.printStackTrace();  
  28.             }  
  29.             return result;  
  30.         }  
  31.   
  32.     }  
  33.   
  34.     /** 
  35.      * 因为服务器返回的是xml形式,所以需要解析 
  36.      *  
  37.      * @author Larson 
  38.      *  
  39.      */  
  40.     private class RegisterResultHandler extends DefaultHandler {  
  41.         /** 
  42.          * 获得的服务器返回结果 
  43.          */  
  44.         private String result;  
  45.         /** 
  46.          * 临时存取数据的变量 
  47.          */  
  48.         private String var="";  
  49.   
  50.         /** 
  51.          * 获取返回值 
  52.          *  
  53.          * @return 
  54.          */  
  55.         public String getResult() {  
  56.             return result;  
  57.         }  
  58.   
  59.         @Override  
  60.         public void startElement(String uri, String localName, String qName,  
  61.                 Attributes attributes) throws SAXException {  
  62.             super.startElement(uri, localName, qName, attributes);  
  63.         }  
  64.   
  65.         @Override  
  66.         public void characters(char[] ch, int start, int length)  
  67.                 throws SAXException {  
  68.             var += new String(ch, start, length);  
  69.             super.characters(ch, start, length);  
  70.         }  
  71.   
  72.         @Override  
  73.         public void endElement(String uri, String localName, String qName)  
  74.                 throws SAXException {  
  75.             /** 
  76.              * 取出info标签的数据(服务器返回的注册结果) 
  77.              */  
  78.             if (qName.equals("info")) {  
  79.                 result = var.trim();  
  80.             }  
  81.             super.endElement(uri, localName, qName);  
  82.         }  
  83.     }  

 获取结果后,在handler中刷新ui通知用户:

 

 

Java代码 break%3B%0A%09%09%09case%20200%3A%0A%09%09%09%09String%20str%20%3D%20(String)%20msg.obj%3B%0A%09%09%09%09if(str.contains(%22regist_ok%22))%0A%09%09%09%09%09Toast.makeText(RegisterActivity.this%2C%20%22%E6%B3%A8%E5%86%8C%E6%88%90%E5%8A%9F.%22%2C%20Toast.LENGTH_SHORT).show()%3B%0A%09%09%09%09else%0A%09%09%09%09%09Toast.makeText(RegisterActivity.this%2C%20%22%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%9C%AA%E5%BC%80%E6%94%BE.%22%2C%20Toast.LENGTH_SHORT).show()%3B%0A%09%09%09%09break%3B%0A%09%09%09default%3A%0A%09%09%09%09break%3B%0A%09%09%09%7D%0A%09%09%7D%3B%0A%09%7D%3B" /> 
  1. /** 
  2.  * 处理返回结果的handler 
  3.  * 如果返回的数据是服务器给的成功数据,注册成功,否则就是服务器有问题 
  4.  */  
  5. private Handler registerResultHandler = new Handler() {  
  6.     public void handleMessage(Message msg) {  
  7.         System.out.println(msg.obj+"-------------"+msg.what);  
  8.         switch (msg.what) {  
  9.         case 400:  
  10.             Toast.makeText(RegisterActivity.this, "注册失败.", Toast.LENGTH_SHORT).show();  
  11.             break;  
  12.         case 200:  
  13.             String str = (String) msg.obj;  
  14.             if(str.contains("regist_ok"))  
  15.                 Toast.makeText(RegisterActivity.this, "注册成功.", Toast.LENGTH_SHORT).show();  
  16.             else  
  17.                 Toast.makeText(RegisterActivity.this, "服务器未开放.", Toast.LENGTH_SHORT).show();  
  18.             break;  
  19.         default:  
  20.             break;  
  21.         }  
  22.     };  
  23. };  

 接下来我们来写服务器端:

 

服务器段用registerServlet处理客户端请求:registerServlet.java:

 

Java代码  
  1. package com.larson.pm;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5. import java.io.PrintWriter;  
  6.   
  7. import javax.servlet.ServletException;  
  8. import javax.servlet.http.HttpServlet;  
  9. import javax.servlet.http.HttpServletRequest;  
  10. import javax.servlet.http.HttpServletResponse;  
  11.   
  12. import com.larson.tools.RegisterResultTool;  
  13.   
  14. public class RegisterServlet extends HttpServlet {  
  15.   
  16.   
  17.     public void doGet(HttpServletRequest request, HttpServletResponse response)  
  18.             throws ServletException, IOException {  
  19.   
  20.     }  
  21.   
  22.     public void doPost(HttpServletRequest request, HttpServletResponse response)  
  23.             throws ServletException, IOException {  
  24.         //1.获取输入输出流  
  25.         response.setContentType("text/html;charset=UTF-8");  
  26.         PrintWriter out = response.getWriter();  
  27.         InputStream in = request.getInputStream();  
  28.         //2.处理客户端发送的数据  
  29.         RegisterResultTool tool = new RegisterResultTool();  
  30.         String result = tool.register(in);  
  31.           
  32.         //3.向客户端写相应数据  
  33.         out.write(result);  
  34.         out.flush();  
  35.         out.close();  
  36.         in.close();  
  37.     }  
  38.   
  39.   
  40. }  

 servlet拿到客户端的数据inputStream,用一个工具类RegisterResultTool.java处理这些数据:

 

 

Java代码  
  1. package com.larson.tools;  
  2.   
  3. import java.io.InputStream;  
  4.   
  5. import javax.xml.parsers.SAXParserFactory;  
  6.   
  7. import org.xml.sax.InputSource;  
  8. import org.xml.sax.XMLReader;  
  9.   
  10. import com.larson.bean.UserInfo;  
  11. import com.larson.daoimpl.UserDao;  
  12. import com.larson.handler.RegisterHandler;  
  13.   
  14. /** 
  15.  * 处理客户端发送过来数据的类 
  16.  *  
  17.  * @author Larson 
  18.  *  
  19.  */  
  20. public class RegisterResultTool {  
  21.   
  22.     /** 
  23.      * 处理客户端发送过来数据的类 
  24.      * @param in 
  25.      *            客户端的流 
  26.      * @return 处理结果 
  27.      */  
  28.     public String register(InputStream in) {  
  29.         String result = null;  
  30.         StringBuilder sb = new StringBuilder();  
  31.         //1.获取解析工厂  
  32.         SAXParserFactory factory = SAXParserFactory.newInstance();  
  33.         XMLReader reader= null;  
  34.         try {  
  35.             //2.自定义解析过程  
  36.             reader = factory.newSAXParser().getXMLReader();  
  37.             RegisterHandler handler = new RegisterHandler();  
  38.             reader.setContentHandler(handler);  
  39.               
  40.             //3.开始解析客户端过来的输入流,并把数据封装到userInfo的bean中  
  41.             reader.parse(new InputSource(in));  
  42.             UserInfo user = handler.getUser();  
  43.               
  44.             //4.往服务器的数据库添加一条用户信息  
  45.             UserDao dao = new UserDao();  
  46.             dao.add(user);  
  47.               
  48.             //5.完成后给客户端反馈消息  
  49.             sb.append("<info>");  
  50.             sb.append("regist_ok");  
  51.             sb.append("</info>");  
  52.         } catch (Exception e) {  
  53.             //TODO 解析失败  
  54.             sb.append("<info>");  
  55.             sb.append("regist_fail");  
  56.             sb.append("</info>");  
  57.             e.printStackTrace();  
  58.         }  
  59.         result = sb.toString();  
  60.         return result;  
  61.     }  
  62.   
  63. }  

 再次过程中,我们需要自定义一个sax解析xml的handler对数据进行处理封装到bean文件中:

 

RegisterHandler.java:

 

Java代码 helpers.DefaultHandler%3B%0A%0Aimport%20com.larson.bean.UserInfo%3B%0A%0Apublic%20class%20RegisterHandler%20extends%20DefaultHandler%20%7B%0A%09private%20UserInfo%20user%3B%0A%09private%20String%20var%3D%22%22%3B%0A%09%0A%09%0A%09%40Override%0A%09public%20void%20startElement(String%20uri%2C%20String%20localName%2C%20String%20qName%2C%0A%09%09%09Attributes%20attributes)%20throws%20SAXException%20%7B%0A%09%09%2F%2F%E9%81%87%E5%88%B0user%E6%A0%87%E7%AD%BE%E5%B0%B1%E8%A1%A8%E7%A4%BA%E6%98%AF%E4%B8%80%E4%B8%AAuser%E5%AF%B9%E8%B1%A1%0A%09%09if(qName.equals(%22user%22))%0A%09%09%09user%20%3D%20new%20UserInfo()%3B%0A%09%09super.startElement(uri%2C%20localName%2C%20qName%2C%20attributes)%3B%0A%09%7D%0A%0A%09%40Override%0A%09public%20void%20endElement(String%20uri%2C%20String%20localName%2C%20String%20qName)%0A%09%09%09throws%20SAXException%20%7B%0A%09%09if(%22username%22.equals(qName))%0A%09%09%09user.setUsername(var)%3B%0A%09%09if(%22password%22.equals(qName))%0A%09%09%09user.setPassword(var)%3B%0A%09%09if(%22nickname%22.equals(qName))%0A%09%09%09user.setNickname(var)%3B%0A%09%09if(%22gender%22.equals(qName))%0A%09%09%09user.setGender(var)%3B%0A%09%09%0A%09%09%2F%2F%E8%AE%BE%E7%BD%AE%E5%AE%8C%E4%B8%B4%E6%97%B6%E5%B1%9E%E6%80%A7%E7%BD%AE%E7%A9%BA%0A%09%09var%3D%22%22%3B%0A%09%09super.endElement(uri%2C%20localName%2C%20qName)%3B%0A%09%7D%0A%0A%09%40Override%0A%09public%20void%20characters(char%5B%5D%20ch%2C%20int%20start%2C%20int%20length)%0A%09%09%09throws%20SAXException%20%7B%0A%09%09var%2B%3Dnew%20String(ch%2C%20start%2C%20length)%3B%0A%09%09super.characters(ch%2C%20start%2C%20length)%3B%0A%09%7D%0A%0A%09%2F**%0A%09%20*%20%E8%8E%B7%E5%8F%96%E5%A1%AB%E5%85%85%E5%AE%8C%E5%B1%9E%E6%80%A7%E7%9A%84user%E5%AF%B9%E8%B1%A1%0A%09%20*%20%40return%0A%09%20*%2F%0A%09public%20UserInfo%20getUser()%20%7B%0A%09%09return%20user%3B%0A%09%7D%0A%0A%7D%0A" /> 
  1. package com.larson.handler;  
  2.   
  3. import org.xml.sax.Attributes;  
  4. import org.xml.sax.SAXException;  
  5. import org.xml.sax.helpers.DefaultHandler;  
  6.   
  7. import com.larson.bean.UserInfo;  
  8.   
  9. public class RegisterHandler extends DefaultHandler {  
  10.     private UserInfo user;  
  11.     private String var="";  
  12.       
  13.       
  14.     @Override  
  15.     public void startElement(String uri, String localName, String qName,  
  16.             Attributes attributes) throws SAXException {  
  17.         //遇到user标签就表示是一个user对象  
  18.         if(qName.equals("user"))  
  19.             user = new UserInfo();  
  20.         super.startElement(uri, localName, qName, attributes);  
  21.     }  
  22.   
  23.     @Override  
  24.     public void endElement(String uri, String localName, String qName)  
  25.             throws SAXException {  
  26.         if("username".equals(qName))  
  27.             user.setUsername(var);  
  28.         if("password".equals(qName))  
  29.             user.setPassword(var);  
  30.         if("nickname".equals(qName))  
  31.             user.setNickname(var);  
  32.         if("gender".equals(qName))  
  33.             user.setGender(var);  
  34.           
  35.         //设置完临时属性置空  
  36.         var="";  
  37.         super.endElement(uri, localName, qName);  
  38.     }  
  39.   
  40.     @Override  
  41.     public void characters(char[] ch, int start, int length)  
  42.             throws SAXException {  
  43.         var+=new String(ch, start, length);  
  44.         super.characters(ch, start, length);  
  45.     }  
  46.   
  47.     /** 
  48.      * 获取填充完属性的user对象 
  49.      * @return 
  50.      */  
  51.     public UserInfo getUser() {  
  52.         return user;  
  53.     }  
  54.   
  55. }  

 ,处理完毕以后,需要把这些数据写入到数据库,生成一条新的用户信息记录,因此需要些一个UserDao。,而我们习惯性用jdbcutils管理连接,这里为了代码可重用更好,我把所有修改的方法集合到一个update方法里面:JdbcUtils.java:

Java代码  
  1. package com.larson.tools;  
  2.   
  3. import java.io.InputStream;  
  4. import java.sql.Connection;  
  5. import java.sql.DriverManager;  
  6. import java.sql.PreparedStatement;  
  7. import java.sql.ResultSet;  
  8. import java.sql.SQLException;  
  9. import java.sql.Statement;  
  10. import java.util.Properties;  
  11.   
  12. import com.larson.handler.ResultSetHandler;  
  13.   
  14. /** 
  15.  * 数据库相关 
  16.  * @author Larson 
  17.  * 
  18.  */  
  19. public class JdbcUtils {  
  20.       
  21. //    private static ComboPooledDataSource ds = null;  
  22.     private static Connection conn;  
  23.       
  24.     static{  
  25.         try{  
  26.             InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties");  
  27.             Properties properties = new Properties();  
  28.             properties.load(in);  
  29.             String url = properties.getProperty("url");  
  30.             String user = properties.getProperty("user");  
  31.             String passwd  = properties.getProperty("password");  
  32.             String driver =  properties.getProperty("driver");  
  33.               
  34.               
  35.             Class.forName(driver);  
  36.             conn = DriverManager.getConnection(url, user, passwd);  
  37.               
  38.               
  39. //          ds = new ComboPooledDataSource();  
  40. //          ds.setDriverClass(driver);  
  41. //          ds.setJdbcUrl(url);  
  42. //          ds.setUser(user);  
  43. //          ds.setPassword(passwd);  
  44. //            
  45. //          ds.setInitialPoolSize(10);  
  46. //          ds.setMinPoolSize(5);  
  47. //          ds.setMaxPoolSize(20);  
  48. //            
  49. //          ds = new ComboPooledDataSource();  
  50.               
  51.         }catch (Exception e) {  
  52.             throw new ExceptionInInitializerError(e);  
  53.         }  
  54.     }  
  55.       
  56.     /** 
  57.      * 获取连接 
  58.      * @return 
  59.      * @throws SQLException 
  60.      */  
  61.     public static Connection getConnection() throws SQLException{  
  62. //      return ds.getConnection();  
  63.         return conn;  
  64.     }  
  65.       
  66.     /** 
  67.      * 释放连接 
  68.      * @param conn 
  69.      * @param st 
  70.      * @param rs 
  71.      */  
  72.     public static void release(Connection conn,Statement st,ResultSet rs){  
  73.           
  74.         if(rs!=null){  
  75.             try{  
  76.                 rs.close();  
  77.             }catch (Exception e) {  
  78.                 e.printStackTrace();  
  79.             }  
  80.             rs = null;  
  81.   
  82.         }  
  83.         if(st!=null){  
  84.             try{  
  85.                 st.close();  
  86.             }catch (Exception e) {  
  87.                 e.printStackTrace();  
  88.             }  
  89.         }  
  90.         if(conn!=null){  
  91.             try{  
  92.                 conn.close();  
  93.             }catch (Exception e) {  
  94.                 e.printStackTrace();  
  95.             }  
  96.         }  
  97.     }  
  98.       
  99.       
  100.     /** 
  101.      * 替换dao中的增删改方法 
  102.      * @param sql 
  103.      * @param params 
  104.      * @throws SQLException 
  105.      */  
  106.     public static void update(String sql,Object params[]) throws SQLException{  
  107.         Connection conn = null;  
  108.         PreparedStatement st = null;  
  109.         ResultSet rs = null;  
  110.           
  111.         try{  
  112.             conn = getConnection();  
  113.             st = conn.prepareStatement(sql);  
  114.             for(int i=0;i<params.length;i++){  
  115.                 st.setObject(i+1, params[i]);  
  116.             }  
  117.             st.executeUpdate();  
  118.               
  119.         }finally{  
  120.             release(conn, st, rs);  
  121.         }  
  122.     }  
  123.       
  124.     /** 
  125.      * 替换所有dao中的查询   策略模式 
  126.      * @param sql 
  127.      * @param params 
  128.      * @param rsh 
  129.      * @return 
  130.      * @throws SQLException 
  131.      */  
  132.     public static Object query(String sql,Object params[],ResultSetHandler rsh) throws SQLException{  
  133.           
  134.         Connection conn = null;  
  135.         PreparedStatement st = null;  
  136.         ResultSet rs = null;  
  137.           
  138.         try{  
  139.             conn = getConnection();  
  140.             st = conn.prepareStatement(sql);  
  141.             for(int i=0;i<params.length;i++){  
  142.                 st.setObject(i+1, params[i]);  
  143.             }  
  144.             rs = st.executeQuery();  
  145.             return rsh.handler(rs);  
  146.               
  147.         }finally{  
  148.             release(conn, st, rs);  
  149.         }  
  150.     }  
  151. }  

 然后便是UserDao.java:

Java代码  
  1. package com.larson.daoimpl;  
  2.   
  3. import com.larson.bean.UserInfo;  
  4. import com.larson.exception.DaoException;  
  5. import com.larson.handler.BeanHandler;  
  6. import com.larson.tools.JdbcUtils;  
  7.   
  8. public class UserDao {  
  9.   
  10.     public void add(UserInfo user) {  
  11.         try {  
  12.             String sql = "insert into user(username,password,gender,nickname) values(?,?,?,?)";  
  13.             Object params[] = {  user.getUsername(),  
  14.                     user.getPassword(), user.getGender(),user.getNickname() };  
  15.   
  16. for(Object obj:params)  
  17.     System.out.println("userdao add---"+obj);  
  18.               
  19.             JdbcUtils.update(sql, params);  
  20.         } catch (Exception e) {  
  21.             throw new DaoException(e);  
  22.         }  
  23.     }  
  24.   
  25.     public void update(UserInfo user) {  
  26.         try {  
  27.             String sql = "update user set user=?,password=?,gender=?,nickname=? where id=?";  
  28.             Object params[] = { user.getId(), user.getUsername(),  
  29.                     user.getPassword(), user.getNickname(), user.getId() };  
  30.             JdbcUtils.update(sql, params);  
  31.         } catch (Exception e) {  
  32.             throw new DaoException(e);  
  33.         }  
  34.     }  
  35.   
  36.     public void delete(String id) {  
  37.         try {  
  38.             String sql = "delete from user where id=?";  
  39.             Object params[] = { id };  
  40.             JdbcUtils.update(sql, params);  
  41.         } catch (Exception e) {  
  42.             throw new DaoException(e);  
  43.         }  
  44.     }  
  45.   
  46.     public UserInfo find(String id) {  
  47.         try {  
  48.             String sql = "select * from user where id=?";  
  49.             Object params[] = { id };  
  50.             return (UserInfo) JdbcUtils.query(sql, params, new BeanHandler(  
  51.                     UserInfo.class));  
  52.         } catch (Exception e) {  
  53.             throw new DaoException(e);  
  54.         }  
  55.     }  
  56.   
  57. }  

 为了方便,我把bean文件也贴出来:

userInfo.java:

Java代码  
  1. package com.larson.bean;  
  2.   
  3. public class UserInfo {  
  4.     private int id;  
  5.     private String username;  
  6.     private String password;  
  7.     private String gender;  
  8.     private String nickname;  
  9.   
  10.     public String getUsername() {  
  11.         return username;  
  12.     }  
  13.   
  14.     public void setUsername(String username) {  
  15.         this.username = username;  
  16.     }  
  17.   
  18.     public int getId() {  
  19.         return id;  
  20.     }  
  21.   
  22.     public void setId(int id) {  
  23.         this.id = id;  
  24.     }  
  25.   
  26.     public String getPassword() {  
  27.         return password;  
  28.     }  
  29.   
  30.     public void setPassword(String password) {  
  31.         this.password = password;  
  32.     }  
  33.   
  34.     public String getGender() {  
  35.         return gender;  
  36.     }  
  37.   
  38.     public void setGender(String gender) {  
  39.         this.gender = gender;  
  40.     }  
  41.   
  42.     public String getNickname() {  
  43.         return nickname;  
  44.     }  
  45.   
  46.     public void setNickname(String nickname) {  
  47.         this.nickname = nickname;  
  48.     }  
  49.   
  50. }  

 本来准备用数据库连接池的,可是老是报错,就是上面注释的那几行,有懂的人麻烦告诉我,感激不尽。

发表评论
用户名: 匿名