Android应用检查版本与更新源码

一.首先我们思考一下步骤

1,获取当前APK的版本

2,去服务器上获取最新的版本

3,对比后,决定是否更新。

4,更新,去服务器上下载apk.

5.下载后,保存到sd卡,然后安装apk

二,客户端代码如下


[java]  view plain copy print ?
  1. package com.zhenshi.updataapk;  
  2.   
  3. import java.io.File;  
  4.   
  5. import com.zhenshi.util.HttpConnect;  
  6. import com.zhenshi.util.ToastUtil;  
  7.   
  8. import android.app.Activity;  
  9. import android.app.AlertDialog;  
  10. import android.app.Dialog;  
  11. import android.app.ProgressDialog;  
  12. import android.content.Context;  
  13. import android.content.DialogInterface;  
  14. import android.content.Intent;  
  15. import android.content.pm.PackageManager.NameNotFoundException;  
  16. import android.net.Uri;  
  17. import android.os.Bundle;  
  18. import android.os.Handler;  
  19.   
  20. /** 
  21.  * class name:TestUpdataActivity<BR> 
  22.  * class description:<BR> 
  23.  * PS: <BR> 
  24.  *  
  25.  * @version 1.00 2012-4-5 
  26.  * @author ZHENSHI)peijiangping 
  27.  */  
  28. public class TestUpdataActivity extends Activity {  
  29.     private String packageName;  
  30.     private ProgressDialog pBar;  
  31.   
  32.     @Override  
  33.     public void onCreate(Bundle savedInstanceState) {  
  34.         super.onCreate(savedInstanceState);  
  35.         setContentView(R.layout.main);  
  36.         yesOrNoUpdataApk();  
  37.     }  
  38.   
  39.     /** 
  40.      * Role:是否更新版本<BR> 
  41.      * Date:2012-4-5<BR> 
  42.      *  
  43.      * @author ZHENSHI)peijiangping 
  44.      */  
  45.     private int yesOrNoUpdataApk() {  
  46.         packageName = this.getPackageName();  
  47.         String nowVersion = getVerCode(TestUpdataActivity.this);  
  48.         String newVersion = goToCheckNewVersion();  
  49.         if (newVersion.equals("Error")) {  
  50.             return 0;  
  51.         }  
  52.         if (Double.valueOf(newVersion) > Double.valueOf(nowVersion)) {  
  53.             doNewVersionUpdate(nowVersion, newVersion);  
  54.         }  
  55.         return 1;  
  56.     }  
  57.   
  58.     /** 
  59.      * Role:是否进行更新提示框<BR> 
  60.      * Date:2012-4-5<BR> 
  61.      *  
  62.      * @author ZHENSHI)peijiangping 
  63.      */  
  64.     private void doNewVersionUpdate(String nowVersion, String newVersion) {  
  65.         StringBuffer sb = new StringBuffer();  
  66.         sb.append("当前版本:");  
  67.         sb.append(nowVersion);  
  68.         sb.append(", 发现新版本:");  
  69.         sb.append(newVersion);  
  70.         sb.append(", 是否更新?");  
  71.         Dialog dialog = new AlertDialog.Builder(TestUpdataActivity.this)  
  72.                 .setTitle("软件更新")  
  73.                 .setMessage(sb.toString())  
  74.                 // 设置内容  
  75.                 .setPositiveButton("更新",// 设置确定按钮  
  76.                         new DialogInterface.OnClickListener() {  
  77.                             @Override  
  78.                             public void onClick(DialogInterface dialog,  
  79.                                     int which) {  
  80.                                 pBar = new ProgressDialog(  
  81.                                         TestUpdataActivity.this);  
  82.                                 pBar.setTitle("正在下载");  
  83.                                 pBar.setMessage("请稍候...");  
  84.                                 // pBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);  
  85.                                 pBar.show();  
  86.                                 goToDownloadApk();  
  87.                             }  
  88.                         })  
  89.                 .setNegativeButton("暂不更新",  
  90.                         new DialogInterface.OnClickListener() {  
  91.                             public void onClick(DialogInterface dialog,  
  92.                                     int whichButton) {  
  93.                                 // 点击"取消"按钮之后退出程序  
  94.                                 finish();  
  95.                             }  
  96.                         }).create();// 创建  
  97.         // 显示对话框  
  98.         dialog.show();  
  99.     }  
  100.   
  101.     /** 
  102.      * Role:开启下载apk的线程<BR> 
  103.      * Date:2012-4-6<BR> 
  104.      *  
  105.      * @author ZHENSHI)peijiangping 
  106.      */  
  107.     private void goToDownloadApk() {  
  108.         new Thread(new DownloadApkThread(handler)).start();  
  109.     }  
  110.   
  111.     public Handler handler = new Handler() {  
  112.         public void handleMessage(android.os.Message msg) {  
  113.             super.handleMessage(msg);  
  114.             if (msg.what == 1) {  
  115.                 ToastUtil toastUtil = new ToastUtil(TestUpdataActivity.this);  
  116.                 toastUtil.showDefultToast("下载成功!!").show();  
  117.                 Intent intent = new Intent(Intent.ACTION_VIEW);  
  118.                 intent.setDataAndType(  
  119.                         Uri.fromFile(new File("/sdcard/update/""updata.apk")),  
  120.                         "application/vnd.android.package-archive");  
  121.                 startActivity(intent);  
  122.             } else {  
  123.                 ToastUtil toastUtil = new ToastUtil(TestUpdataActivity.this);  
  124.                 toastUtil.showDefultToast("下载失败!!").show();  
  125.             }  
  126.             pBar.cancel();  
  127.         }  
  128.     };  
  129.   
  130.     /** 
  131.      * Role:去获取当前的应用最新版本<BR> 
  132.      * Date:2012-4-5<BR> 
  133.      *  
  134.      * @author ZHENSHI)peijiangping 
  135.      */  
  136.     private String goToCheckNewVersion() {  
  137.         System.out.println("goToCheckNewVersion");  
  138.         String newVersion = null;  
  139.         final String url = "http://192.168.1.41:8080/TestHttpServlet/GetVersionServlet";  
  140.         HttpConnect hc = new HttpConnect(url, this);  
  141.         newVersion = hc.getDataAsString(null);  
  142.         if (newVersion.equals("Error")) {  
  143.             return "Error";  
  144.         }  
  145.         return newVersion;  
  146.     }  
  147.   
  148.     /** 
  149.      * Role:取得程序的当前版本<BR> 
  150.      * Date:2012-4-5<BR> 
  151.      *  
  152.      * @author ZHENSHI)peijiangping 
  153.      */  
  154.     private String getVerCode(Context context) {  
  155.         int verCode = 0;  
  156.         String verName = null;  
  157.         try {  
  158.             verCode = context.getPackageManager()  
  159.                     .getPackageInfo(packageName, 0).versionCode;  
  160.             verName = context.getPackageManager()  
  161.                     .getPackageInfo(packageName, 0).versionName;  
  162.         } catch (NameNotFoundException e) {  
  163.             System.out.println("no");  
  164.         }  
  165.         System.out.println("verCode" + verCode + "===" + "verName" + verName);  
  166.         return verName;  
  167.     }  
  168. }  

[java]  view plain copy print ?
  1. package com.zhenshi.updataapk;  
  2.   
  3. import com.zhenshi.util.HttpDownload;  
  4.   
  5. import android.os.Handler;  
  6. import android.os.Message;  
  7.   
  8. /** 
  9.  * class name:DownloadApkThread<BR> 
  10.  * class description:下载apk的线程<BR> 
  11.  * PS: <BR> 
  12.  *  
  13.  * @version 1.00 2012-4-5 
  14.  * @author ZHENSHI)peijiangping 
  15.  */  
  16. public class DownloadApkThread implements Runnable {  
  17.     private Handler handler;  
  18.     private static final String url = "http://192.168.1.41:8080/TestHttpServlet/apk/TestUpdata.apk";  
  19.     private static final String fileName = "updata.apk";  
  20.     private static final String path = "/sdcard/update";  
  21.   
  22.     public DownloadApkThread(Handler handler) {  
  23.         this.handler = handler;  
  24.     }  
  25.   
  26.     @Override  
  27.     public void run() {  
  28.         System.out.println("下载线程开启");  
  29.         Message message = new Message();  
  30.         message.what = HttpDownload.downLoadFile(url, fileName, path);  
  31.         handler.sendMessage(message);  
  32.     }  
  33. }  

[java]  view plain copy print ?
  1. package com.zhenshi.util;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5. import java.util.List;  
  6.   
  7. import org.apache.http.HttpResponse;  
  8. import org.apache.http.NameValuePair;  
  9. import org.apache.http.client.HttpClient;  
  10. import org.apache.http.client.entity.UrlEncodedFormEntity;  
  11. import org.apache.http.client.methods.HttpPost;  
  12. import org.apache.http.impl.client.DefaultHttpClient;  
  13. import org.apache.http.params.BasicHttpParams;  
  14. import org.apache.http.params.HttpConnectionParams;  
  15. import org.apache.http.params.HttpParams;  
  16. import org.apache.http.protocol.HTTP;  
  17. import org.apache.http.util.EntityUtils;  
  18.   
  19. import android.content.Context;  
  20. import android.util.Log;  
  21.   
  22. /** 
  23.  * class name:HttpConnect<BR> 
  24.  * class description:http请求工具类<BR> 
  25.  * PS: <BR> 
  26.  *  
  27.  * @version 1.00 2011/09/21 
  28.  * @author CODYY)peijiangping 
  29.  */  
  30. public class HttpConnect {  
  31.     /** 
  32.      * 访问的Web服务器地址 
  33.      */  
  34.     private String url;  
  35.     private HttpPost httpRequest;  
  36.     private HttpResponse httpResponse;  
  37.     private HttpParams httpParams;  
  38.     private HttpClient httpClient;  
  39.   
  40.     public HttpConnect(String url, Context context) {  
  41.         this.url = url;  
  42.         httpParams = new BasicHttpParams();  
  43.         HttpConnectionParams.setConnectionTimeout(httpParams, 15000);  
  44.         HttpConnectionParams.setSoTimeout(httpParams, 15000);  
  45.         httpClient = new DefaultHttpClient(httpParams);  
  46.     }  
  47.   
  48.     /** 
  49.      * Role:通过Http请求来获取返回值(InputStream) <BR> 
  50.      * Date:2012-2-10 <BR> 
  51.      *  
  52.      * @author CODYY)peijiangping 
  53.      */  
  54.     public InputStream getDataAsInputStream(List<NameValuePair> params) {  
  55.         InputStream result = null;  
  56.         try {  
  57.             httpRequest = new HttpPost(url);  
  58.             if (params != null) {  
  59.                 httpRequest.setEntity(new UrlEncodedFormEntity(params,  
  60.                         HTTP.UTF_8));  
  61.             }  
  62.             httpResponse = httpClient.execute(httpRequest);  
  63.             if (200 == httpResponse.getStatusLine().getStatusCode()) {  
  64.                 result = httpResponse.getEntity().getContent();  
  65.             }  
  66.         } catch (IOException e) {  
  67.             Log.e("nimeimei", e.getMessage(), e);  
  68.             return result;  
  69.         }  
  70.         return result;  
  71.     }  
  72.   
  73.     /** 
  74.      * Role:通过Http请求来获取返回值(String),异常返回Error<BR> 
  75.      * Date:2012-2-10 <BR> 
  76.      *  
  77.      * @author CODYY)peijiangping 
  78.      */  
  79.     public String getDataAsString(List<NameValuePair> params) {  
  80.         String result = null;  
  81.         try {  
  82.             httpRequest = new HttpPost(url);  
  83.             if (params != null) {  
  84.                 httpRequest.setEntity(new UrlEncodedFormEntity(params,  
  85.                         HTTP.UTF_8));  
  86.             }  
  87.             httpResponse = httpClient.execute(httpRequest);  
  88.             if (200 == httpResponse.getStatusLine().getStatusCode()) {  
  89.                 result = EntityUtils.toString(httpResponse.getEntity());  
  90.                 System.out.println("取得返回值" + result);  
  91.             }  
  92.         } catch (IOException e) {  
  93.             e.printStackTrace();  
  94.             return "Error";  
  95.         }  
  96.         return result;  
  97.     }  
  98.   
  99.     /** 
  100.      * Role:通过Http请求来获取返回值(int),失败返回-1,成功返回数据 <BR> 
  101.      * Date:2012-2-10 <BR> 
  102.      *  
  103.      * @author CODYY)peijiangping 
  104.      */  
  105.     public int getDataAsInt(List<NameValuePair> params) {  
  106.         int result = -1;  
  107.         try {  
  108.             httpRequest = new HttpPost(url);  
  109.             if (params != null) {  
  110.                 httpRequest.setEntity(new UrlEncodedFormEntity(params,  
  111.                         HTTP.UTF_8));  
  112.             }  
  113.             httpResponse = httpClient.execute(httpRequest);  
  114.             if (200 == httpResponse.getStatusLine().getStatusCode()) {  
  115.                 result = Integer.parseInt(EntityUtils.toString(httpResponse  
  116.                         .getEntity()));  
  117.             }  
  118.         } catch (IOException e) {  
  119.             return result;  
  120.         }  
  121.         return result;  
  122.     }  
  123.   
  124.     /** 
  125.      * Role:http请求服务器,不取得返回数据,成功返回1<BR> 
  126.      * Date:2012-2-10 <BR> 
  127.      *  
  128.      * @author CODYY)peijiangping 
  129.      */  
  130.     public int sendToService(List<NameValuePair> params) {  
  131.         try {  
  132.             httpRequest = new HttpPost(url);  
  133.             if (params != null) {  
  134.                 httpRequest.setEntity(new UrlEncodedFormEntity(params,  
  135.                         HTTP.UTF_8));  
  136.             }  
  137.             httpResponse = httpClient.execute(httpRequest);  
  138.         } catch (Exception e) {  
  139.             e.printStackTrace();  
  140.             return 0;  
  141.         }  
  142.         return 1;  
  143.     }  
  144. }  

[java]  view plain copy print ?
  1. package com.zhenshi.util;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.InputStream;  
  7. import java.net.HttpURLConnection;  
  8. import java.net.MalformedURLException;  
  9. import java.net.URL;  
  10.   
  11. /** 
  12.  * 类名:HttpDownload<BR> 
  13.  * 作用:下载文件的类<BR> 
  14.  * 日期:2012-4-5<BR> 
  15.  *  
  16.  * @version V1.0 
  17.  * @author peijiangping 
  18.  */  
  19. public class HttpDownload {  
  20.     /** 
  21.      * 功能:下载文件到指定目录下,成功返回1<BR> 
  22.      * 日期:2012-4-5<BR> 
  23.      * 作者:裴江平 
  24.      */  
  25.     public static int downLoadFile(String httpUrl, String fileName, String path) {  
  26.         FileOutputStream fos = null;  
  27.         InputStream is = null;  
  28.         HttpURLConnection conn = null;  
  29.         // 当存放文件的文件目录不存在的时候创建文件目录  
  30.         File tmpFile = new File(path);  
  31.         if (!tmpFile.exists()) {  
  32.             tmpFile.mkdir();  
  33.         }  
  34.         // 获取文件对象  
  35.         File file = new File(path + "/"+fileName);  
  36.         try {  
  37.             URL url = new URL(httpUrl);  
  38.             try {  
  39.                 conn = (HttpURLConnection) url.openConnection();  
  40.                 is = conn.getInputStream();// 获得http请求返回的InputStream对象。  
  41.                 fos = new FileOutputStream(file);// 获得文件输出流对象来写文件用的  
  42.                 byte[] buf = new byte[256];  
  43.                 conn.connect();// http请求服务器  
  44.                 double count = 0;  
  45.                 // http请求取得响应的时候  
  46.                 if (conn.getResponseCode() >= 400) {  
  47.                     System.out.println("nono");  
  48.                     return 0;  
  49.                 } else {  
  50.                     while (count <= 100) {  
  51.                         if (is != null) {  
  52.                             int numRead = is.read(buf);  
  53.                             if (numRead <= 0) {  
  54.                                 break;  
  55.                             } else {  
  56.                                 fos.write(buf, 0, numRead);  
  57.                             }  
  58.   
  59.                         } else {  
  60.                             break;  
  61.                         }  
  62.                     }  
  63.                 }  
  64.                 conn.disconnect();  
  65.                 fos.close();  
  66.                 is.close();  
  67.             } catch (IOException e) {  
  68.                 e.printStackTrace();  
  69.                 return 0;  
  70.             } finally {  
  71.                 if (conn != null) {  
  72.                     conn.disconnect();  
  73.                     conn = null;  
  74.                 }  
  75.                 if (fos != null) {  
  76.                     try {  
  77.                         fos.close();  
  78.                     } catch (IOException e) {  
  79.                         e.printStackTrace();  
  80.                     }  
  81.                     fos = null;  
  82.                 }  
  83.                 if (is != null) {  
  84.                     try {  
  85.                         is.close();  
  86.                     } catch (IOException e) {  
  87.                         e.printStackTrace();  
  88.                     }  
  89.                     is = null;  
  90.                 }  
  91.             }  
  92.         } catch (MalformedURLException e) {  
  93.             e.printStackTrace();  
  94.             return 0;  
  95.         }  
  96.         return 1;  
  97.     }  
  98. }  

[java]  view plain copy print ?
  1. package com.zhenshi.util;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.drawable.Drawable;  
  5. import android.view.Gravity;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.widget.ImageView;  
  9. import android.widget.LinearLayout;  
  10. import android.widget.Toast;  
  11.   
  12. /** 
  13.  * class name:ToastUtil<BR> 
  14.  * class description:显示toast的一个工具类<BR> 
  15.  * PS: <BR> 
  16.  *  
  17.  * @version 1.00 2011/09/30 
  18.  * @author CODYY)peijiangping 
  19.  */  
  20. public class ToastUtil {  
  21.     private Context context;  
  22.   
  23.     public ToastUtil(Context context) {  
  24.         this.context = context;  
  25.     }  
  26.   
  27.     /** 
  28.      * @param title 
  29.      * @param icon 
  30.      * @return返回一个带图片的toast 
  31.      */  
  32.     public Toast showPicToast(String title, Drawable icon) {  
  33.         Toast toast = Toast.makeText(context, title, Toast.LENGTH_LONG);  
  34.         toast.setGravity(Gravity.CENTER, 00);  
  35.         LinearLayout toastView = (LinearLayout) toast.getView();  
  36.         ImageView imageCodeProject = new ImageView(context);  
  37.         imageCodeProject.setBackgroundDrawable(icon);  
  38.         toastView.addView(imageCodeProject, 0);  
  39.         return toast;  
  40.     }  
  41.   
  42.     /** 
  43.      * @param title 
  44.      * @return返回一个默认的toast 
  45.      */  
  46.     public Toast showDefultToast(String title) {  
  47.         Toast toast = Toast.makeText(context, title, Toast.LENGTH_SHORT);  
  48.         return toast;  
  49.     }  
  50.   
  51.     /** 
  52.      * @param layoutid 
  53.      * @return返回一个自定义的toast 
  54.      */  
  55.     public Toast showDiyToast(int layoutid) {  
  56.         LayoutInflater inflater = LayoutInflater.from(context);  
  57.         View myView = inflater.inflate(layoutid,null);  
  58.         Toast toast = new Toast(context);  
  59.         toast.setGravity(Gravity.RIGHT | Gravity.TOP, 1240);  
  60.         toast.setDuration(Toast.LENGTH_LONG);  
  61.         toast.setView(myView);  
  62.         return toast;  
  63.     }  
  64. }  

三,服务器代码如下:

[java]  view plain copy print ?
  1. package com.zhenshi.servlet;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.Map;  
  5.   
  6. import javax.servlet.ServletException;  
  7. import javax.servlet.http.HttpServlet;  
  8. import javax.servlet.http.HttpServletRequest;  
  9. import javax.servlet.http.HttpServletResponse;  
  10.   
  11. import com.zhenshi.util.SerialPortListener;  
  12.   
  13. /** 
  14.  * class name:GetVersionServlet<BR> 
  15.  * class description:获取最新的软件版本<BR> 
  16.  * PS: <BR> 
  17.  *  
  18.  * @version 1.00 2012-4-6 
  19.  * @author ZHENSHI)peijiangping 
  20.  */  
  21. public class GetVersionServlet extends HttpServlet {  
  22.     private static final long serialVersionUID = 1L;  
  23.     public Map<String, String> configMap;  
  24.   
  25.     /** 
  26.      * @see HttpServlet#HttpServlet() 
  27.      */  
  28.     public GetVersionServlet() {  
  29.         super();  
  30.         // TODO Auto-generated constructor stub  
  31.     }  
  32.   
  33.     /** 
  34.      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse 
  35.      *      response) 
  36.      */  
  37.     protected void doGet(HttpServletRequest request,  
  38.             HttpServletResponse response) throws ServletException, IOException {  
  39.         request.setCharacterEncoding("utf-8");  
  40.         configMap = SerialPortListener.configMap;  
  41.         String version = configMap.get("version");  
  42.         System.out.println(version);  
  43.         response.getWriter().write(version);  
  44.     }  
  45.   
  46.     /** 
  47.      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse 
  48.      *      response) 
  49.      */  
  50.     protected void doPost(HttpServletRequest request,  
  51.             HttpServletResponse response) throws ServletException, IOException {  
  52.         /* 接收post请求 */  
  53.         doGet(request, response);  
  54.     }  
  55.   
  56. }  

[java]  view plain copy print ?
  1. package com.zhenshi.util;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.FileReader;  
  5. import java.io.IOException;  
  6. import java.util.HashMap;  
  7. import java.util.Map;  
  8.   
  9. import javax.servlet.ServletContext;  
  10. import javax.servlet.ServletContextEvent;  
  11. import javax.servlet.ServletContextListener;  
  12.   
  13. /** 
  14.  * class name:SerialPortListener<BR> 
  15.  * class description:WEb服务器启动监听类<BR> 
  16.  * PS: <BR> 
  17.  * Date:2012-2-16<BR> 
  18.  *  
  19.  * @version 1.00 
  20.  * @author CODYY)peijiangping 
  21.  */  
  22. public class SerialPortListener implements ServletContextListener {  
  23.   
  24.     public static Map<String, String> configMap;  
  25.   
  26.     public void contextInitialized(ServletContextEvent event) {  
  27.         ServletContext context = event.getServletContext();  
  28.         String path = context.getRealPath("/");  
  29.         path = path.replace("\\", "/");  
  30.         path = path.replace("/WebContent""");  
  31.         configMap = findConfig(path);  
  32. //      context.setAttribute("configMap", configMap);  
  33.     }  
  34.   
  35.     public void contextDestroyed(ServletContextEvent arg0) {  
  36.   
  37.     }  
  38.   
  39.     public Map<String, String> findConfig(String path) {  
  40.         Map<String, String> configMap = new HashMap<String, String>();  
  41.         FileReader fr = null;  
  42.         BufferedReader br = null;  
  43.         try {  
  44.             fr = new FileReader(path + "conf/config.properties");// 创建FileReader对象,用来读取字符流  
  45.             br = new BufferedReader(fr); // 缓冲指定文件的输入  
  46.             String myreadline; // 定义一个String类型的变量,用来每次读取一行  
  47.             while (br.ready()) {  
  48.                 myreadline = br.readLine();  
  49.                 String[] keyAndValue = myreadline.split("=");  
  50.                 if (keyAndValue.length == 2) {  
  51.                     configMap.put(keyAndValue[0].trim(), keyAndValue[1].trim());  
  52.                 } else if (keyAndValue.length > 2) {  
  53.                     String keyName = myreadline.substring(0,  
  54.                             myreadline.indexOf("="));  
  55.                     String keyValue = myreadline.substring(myreadline  
  56.                             .indexOf("=") + 1);  
  57.                     configMap.put(keyName.trim(), keyValue.trim());  
  58.   
  59.                 }  
  60.             }  
  61.         } catch (IOException e) {  
  62.             e.printStackTrace();  
  63.         } finally {  
  64.   
  65.             if (br != null) {  
  66.                 try {  
  67.                     br.close();  
  68.                 } catch (IOException e) {  
  69.                     e.printStackTrace();  
  70.                 }  
  71.             }  
  72.             if (fr != null) {  
  73.                 try {  
  74.                     fr.close();  
  75.                 } catch (IOException e) {  
  76.                     e.printStackTrace();  
  77.                 }  
  78.             }  
  79.         }  
  80.         return configMap;  
  81.     }  
  82. }  

config.properties内容为:

[java]  view plain copy print ?
  1. #config  
  2. version=1.0  

web.xml内容为:

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">  
  3.   <display-name>TestHttpServlet</display-name>  
  4.   <welcome-file-list>  
  5.     <welcome-file>index.html</welcome-file>  
  6.     <welcome-file>index.htm</welcome-file>  
  7.     <welcome-file>index.jsp</welcome-file>  
  8.     <welcome-file>default.html</welcome-file>  
  9.     <welcome-file>default.htm</welcome-file>  
  10.     <welcome-file>default.jsp</welcome-file>  
  11.   </welcome-file-list>  
  12.   <listener>  
  13.     <listener-class>com.zhenshi.util.SerialPortListener</listener-class>  
  14.   </listener>  
  15.   <servlet>  
  16.     <description></description>  
  17.     <display-name>GetVersionServlet</display-name>  
  18.     <servlet-name>GetVersionServlet</servlet-name>  
  19.     <servlet-class>com.zhenshi.servlet.GetVersionServlet</servlet-class>  
  20.   </servlet>  
  21.   <servlet-mapping>  
  22.     <servlet-name>GetVersionServlet</servlet-name>  
  23.     <url-pattern>/GetVersionServlet</url-pattern>  
  24.   </servlet-mapping>  
  25. </web-app>  

服务器为Tomcat.

Androidmainfest.xml记得加上一些应用相关权限。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值