Android开发学习笔记-实现联网检测程序版本
package com.frank.mobilesafe; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URI; import java.net.URL; import javax.net.ssl.HttpsURLConnection; import org.apache.http.HttpClientConnection; import org.apache.http.client.methods.HttpUriRequest; import org.json.JSONObject; import com.frank.mobilesafe.utils.FileHelper; import android.app.Activity; import android.content.Intent; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.Window; import android.widget.TextView; import android.widget.Toast; public class SplashActivity extends Activity { private static final int SHOW_UPDATE_DIALOG = 0; protected static final int REQUEST_FAILURE = 1; protected static final int REQUEST_ERROR = 2; private static final int SHOW_HOME = 3; private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { HandlerStartMessage(msg.what); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // requestWindowFeature(Window.FEATURE_NO_TITLE);设置无标题 setContentView(R.layout.activity_splash); TextView tv_version = (TextView) findViewById(R.id.tv_version); tv_version.setText(getVersion()); CheckUpdate(); } protected void HandlerStartMessage(int what) { switch (what) { case SHOW_UPDATE_DIALOG: Toast.makeText(this, "该升级了", 0).show(); break; case SHOW_HOME: //Toast.makeText(this, "show home activity", 0).show(); ShowMainActivity(); break; case REQUEST_FAILURE: Toast.makeText(this, "REQUEST_FAILURE", 0).show(); break; default: break; } } /** * 显示主界面 */ private void ShowMainActivity() { // TODO Auto-generated method stub Intent intent = new Intent(this,MainActivity.class); startActivity(intent); finish(); } /** * 检查升级 */ protected void CheckUpdate() { new Thread() { public void run() { long startTime = System.currentTimeMillis(); Message msg = Message.obtain(); try { URL url = new URL("http://192.168.1.104/1.html");// 网络链接地址 HttpURLConnection conn = (HttpURLConnection) url .openConnection();// 创建连接对象 conn.setRequestMethod("GET");// 设置请求方式 conn.setReadTimeout(5000);// 设置请求超时时间 if (200 == conn.getResponseCode())// 当请成功的时候执行 { InputStream inputStream = conn.getInputStream(); FileHelper helper = new FileHelper(); String content = helper.StreamToString(inputStream); Log.i("a", content); JSONObject obj = new JSONObject(content); String version = (String) obj.get("version"); Log.i("version", version); if (version == getVersion()) { msg.what = SHOW_UPDATE_DIALOG; } else { msg.what = SHOW_HOME; } } else { Log.i("a", "请求失败!"); msg.what = REQUEST_FAILURE; } } catch (Exception e) { Log.i("a", "shibai"); msg.what = REQUEST_ERROR; e.printStackTrace(); } finally { long endTime = System.currentTimeMillis(); long spanTime = endTime-startTime; if (spanTime<2000) { try { Thread.sleep(2000-spanTime); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } handler.sendMessage(msg); } }; }.start(); } /** * 获取版本信息 * * @return */ protected String getVersion() { String versionStr = ""; PackageManager packManger = getPackageManager(); try { PackageInfo info = packManger.getPackageInfo(getPackageName(), 0); versionStr = info.versionName;// 获取的版本信息 } catch (NameNotFoundException e) { // TODO Auto-generated catch block versionStr = ""; e.printStackTrace(); } return versionStr; } }
问题总结:
1、在解析JSON的时候需要情况一直遇到异常,出错异常信息为org.json.JSONException: Value ? of type java.lang.String cannot be converted to JSONObject,经查得是在保存json文本的时候是使用的记事本造成的,这也就是说在保存JSON的使用应该使用editplus等文本编辑器进行保存,保存的格式应该为utf8 无bom格式,这样在进行解析的时候就不会造成异常。
2、在联网获取数据的时候,因为是在本机iis上配置的站点,不过在请求数据的时候一直请求不到,遇到的第一个原因就是没有对请求设置请求方式以及请求时间,之后对百度等其他网站进行请求都没问题,却对自己配置的请求不了,经过抓耳挠腮之后,查的原因竟是http后面的反斜线少了一个。大意了!
3、在线程内访问线程外的资源的时候,需要使用java事件机制进行处理。主要方法如下:
private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { HandlerStartMessage(msg.what); } };
protected void CheckUpdate() { new Thread() { public void run() { long startTime = System.currentTimeMillis(); Message msg = Message.obtain(); msg.what = SHOW_UPDATE_DIALOG; handler.sendMessage(msg); } }; }.start();
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。