手機衛士02-與服務器交互
好啦,我們繼續我們昨天的那個項目,昨天我們隻完成了一個程序啟動時的歡迎界麵,但是提到了,啟動那個界麵時會進行版本的檢查,所以今天我們就做那個版本的檢查那一塊。
既然要做版本檢查,那肯定要有服務器啦,所以我們就用到tomcat啦,因為這個項目是Android, 所以我就不寫那個服務器端的程序啦,我隻用tomcat來進行一個簡單的從服務器讀取數據,然後更新程序。不會專門寫一個服務器端來進行業務的處理。
好,廢話不多說,可能有些人還沒接觸過Web方麵的,所以我把tomcat的搭建也簡單的說一下,大神可以直接跳到下麵去。
首先,我們去tomcat官網上下載一個tomcat 下載地址
在這裏選擇是32位的還是64位的
下載下來後,安裝也很簡單,直接解壓出來就行啦(前提是你的java環境要正確)
解壓好之後,我們就可以去到它的bin目錄下麵,雙擊那個startup.bat文件啦
然後我們就會看到一個黑唿唿的界麵
然後,我們去瀏覽器輸入 https://localhost:8080/ 然後出現下麵的界麵,那就說明你的tomcat配置成功了
那麼,我們的服務器配置好之後,我們就要用它來為我們的app做一些東西啦
首先,我們在tomcat的webapps目錄下載新建一個文件夾叫Security(這個是個人喜歡的,因為我們app叫Security)
update.xml裏麵的內容
- <?xml version="1.0" encoding="utf-8"?>
- <update>
- <version>1.0</version>
- <description>這裏寫一些這個版本的特點</description>
- <apkurl>https://192.168.1.5:8080/Security/new.apk</apkurl>
- <!--這裏的ip地址一定要寫你服務器所在的電腦的ip地址,我們會在Security這個目錄下麵放置一下new.apk的,用來更新的-->
- </update>
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
-
- <string name="serverUrl">https://192.168.1.5:8080/Security/update.xml</string>
-
- </resources>
- package com.xiaobin.security.domain;
- public class UpdateInfo
- {
- private String version;
- private String description;
- private String url;
-
- public String getVersion()
- {
- return version;
- }
- public void setVersion(String version)
- {
- this.version = version;
- }
- public String getDescription()
- {
- return description;
- }
- public void setDescription(String description)
- {
- this.description = description;
- }
- public String getUrl()
- {
- return url;
- }
- public void setUrl(String url)
- {
- this.url = url;
- }
-
- }
- package com.xiaobin.security.engine;
- import java.io.InputStream;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import android.content.Context;
- import com.xiaobin.security.domain.UpdateInfo;
- public class UpdateInfoService
- {
-
- private Context context;
-
- public UpdateInfoService(Context context)
- {
- this.context = context;
- }
-
- public UpdateInfo getUpdateInfo(int urlId) throws Exception
- {
- String path = context.getResources().getString(urlId);//拿到config.xml裏麵存放的地址
- URL url = new URL(path);
- HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();//開啟一個http鏈接
- httpURLConnection.setConnectTimeout(5000);//設置鏈接的超時時間,現在為5秒
- httpURLConnection.setRequestMethod("GET");//設置請求的方式
- InputStream is = httpURLConnection.getInputStream();//拿到一個輸入流。裏麵包涵了update.xml的信息
- return UpdateInfoParser.getUpdateInfo(is);//解析xml
- }
- }
- package com.xiaobin.security.engine;
- import java.io.InputStream;
- import org.xmlpull.v1.XmlPullParser;
- import android.util.Xml;
- import com.xiaobin.security.domain.UpdateInfo;
- public class UpdateInfoParser
- {
-
- public static UpdateInfo getUpdateInfo(InputStream is) throws Exception
- {
- UpdateInfo info = new UpdateInfo();
- XmlPullParser xmlPullParser = Xml.newPullParser();
- xmlPullParser.setInput(is, "utf-8");
- int type = xmlPullParser.getEventType();
- while(type != XmlPullParser.END_DOCUMENT)
- {
- switch(type)
- {
- case XmlPullParser.START_TAG :
- if(xmlPullParser.getName().equals("version"))
- {
- info.setVersion(xmlPullParser.nextText());
- }
- else if(xmlPullParser.getName().equals("description"))
- {
- info.setDescription(xmlPullParser.nextText());
- }
- else if(xmlPullParser.getName().equals("apkurl"))
- {
- info.setUrl(xmlPullParser.nextText());
- }
- break;
-
- default :
- break;
- }
- type = xmlPullParser.next();
- }
- return info;
- }
- }
- package com.xiaobin.security.ui;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.DialogInterface;
- import android.content.pm.PackageInfo;
- import android.content.pm.PackageManager;
- import android.content.pm.PackageManager.NameNotFoundException;
- import android.os.Bundle;
- import android.view.Window;
- import android.view.WindowManager;
- import android.view.animation.AlphaAnimation;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.xiaobin.security.R;
- import com.xiaobin.security.domain.UpdateInfo;
- import com.xiaobin.security.engine.UpdateInfoService;
- public class SplashActivity extends Activity
- {
- private TextView tv_version;
- private LinearLayout ll;
-
- private UpdateInfo info;
-
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.splash);
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
-
- tv_version = (TextView) findViewById(R.id.tv_splash_version);
- String version = getVersion();
- tv_version.setText("版本號 " + version);
-
- ll = (LinearLayout) findViewById(R.id.ll_splash_main);
- AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
- alphaAnimation.setDuration(2000);
- ll.startAnimation(alphaAnimation);
-
- if(isNeedUpdate(version))
- {
- showUpdateDialog();
- }
- }
-
- private void showUpdateDialog()
- {
- AlertDialog.Builder builder = new AlertDialog.Builder(this);
- builder.setIcon(android.R.drawable.ic_dialog_info);
- builder.setTitle("升級提醒");
- builder.setMessage(info.getDescription());
- builder.setCancelable(false);
-
- builder.setPositiveButton("確定", new DialogInterface.OnClickListener()
- {
-
- @Override
- public void onClick(DialogInterface dialog, int which)
- {
- // TODO Auto-generated method stub
-
- }
- });
- builder.setNegativeButton("取消", new DialogInterface.OnClickListener()
- {
- @Override
- public void onClick(DialogInterface dialog, int which)
- {
- // TODO Auto-generated method stub
-
- }
-
- });
- builder.create().show();
- }
- private boolean isNeedUpdate(String version)
- {
- UpdateInfoService updateInfoService = new UpdateInfoService(this);
- try
- {
- info = updateInfoService.getUpdateInfo(R.string.serverUrl);
- String v = info.getVersion();
- if(v.equals(version))
- {
- System.out.println("不用更新");
- return false;
- }
- else
- {
- System.out.println("要更新");
- return true;
- }
- }
- catch (Exception e)
- {
- e.printStackTrace();
- Toast.makeText(this, "獲取更新信息異常,請稍後再試", Toast.LENGTH_SHORT).show();
- }
- return false;
- }
- private String getVersion()
- {
- try
- {
- PackageManager packageManager = getPackageManager();
- PackageInfo packageInfo = packageManager.getPackageInfo(getPackageName(), 0);
-
- return packageInfo.versionName;
- }
- catch (NameNotFoundException e)
- {
- e.printStackTrace();
- return "版本號未知";
- }
- }
- }
- <uses-permission android:name="android.permission.INTERNET"/>

最後更新:2017-04-03 14:54:27