知识点:
1 AlterDialog
2 使用GET方式请求HTTP服务信息
3 解析JSON
4 利用Handler来进行Message消息处理
5 多线程Thread进行网络获取数据
详细解释:
1 AlterDialog:
/**
* 展示更新对话框
*/
protected void ShowDialog() {
AlertDialog.Builder Dialog = new AlertDialog.Builder(this);
Dialog.setTitle("检查更新");
Dialog.setMessage(mDescription);
Dialog.setPositiveButton("立即更新", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
Dialog.setNegativeButton("以后再说", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
Dialog.show();
}
2, 3使用GET方式请求HTTP服务信息与JSON解析数据
/**
* 检查版本
* 知识点:message使用,HTTP获得网络数据,JSON对象的解析
*/
private void CheckVerison()
{
System.out.println("Thread!!!");
new Thread(){
@Override
public void run() {
Message message = Message.obtain();
System.out.println("Run!!!");
try {
//建立HTTP连接 并使用GET方法进行获取数据
URL url = new URL("http://192.168.56.1:8080/updata.json");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.connect();
//服务器连接正常
int responseCode = connection.getResponseCode();
System.out.println("responseCode : "+responseCode+"!!!!");
if(responseCode==200)
{
//获取数据,并将数据保存到result中
InputStream inputStream = connection.getInputStream();
String result = StreamUtils.readFromStream(inputStream);
System.out.println("网络数据: "+result);
//解析json
JSONObject json;
try {
json = new JSONObject(result);
mVersionName = json.getString("VersionName");
mVersionCode = json.getString("VersionCode");
mDescription = json.getString("Description");
mDownLoadUrl = json.getString("DownLoadUrl");
System.out.println("Version _ json : "+mVersionName);
System.out.println("description _ json "+ mDescription);
if(GetVerisonCode()<Integer.parseInt(mVersionCode))
{
message.what=CODE_NEDD_UPDATA;
System.out.println("需要更新");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
message.what=CODE_JSON_ERROR;
e.printStackTrace();
}
}
System.out.println("URL: "+connection.getDate()+"");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
message.what=CODE_URL_ERROR;
e.printStackTrace();
System.out.println("CODE_URL_ERROR");
} catch (IOException e) {
// TODO Auto-generated catch block
message.what=CODE_NET_ERROR;
e.printStackTrace();
System.out.println("CODE_NET_ERROR");
}
finally{
mhandler.sendMessage(message);
}
}
}.start();
}
3 利用Handler来进行Message消息处理
(1)声明全局handler变量,重新handlemessage消息处理函数
// 消息处理handler
private Handler mhandler = new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case CODE_NEDD_UPDATA:
ShowDialog();
break;
case CODE_JSON_ERROR:
Toast.makeText(getBaseContext(), "Json_ERROR", Toast.LENGTH_SHORT).show();
break;
case CODE_URL_ERROR:
Toast.makeText(getBaseContext(), "URL错误", Toast.LENGTH_SHORT).show();
break;
case CODE_NET_ERROR:
Toast.makeText(getBaseContext(), "网络错误", Toast.LENGTH_SHORT).show();
System.out.println("handler net error!!!");
break;
default:
break;
}
};
};
(2)获取message,并将数据放入到message中:
Message message = Message.obtain();
message.what=CODE_NEDD_UPDATA;
message.what=CODE_JSON_ERROR;
message.what=CODE_URL_ERROR;
message.what=CODE_NET_ERROR;
(3)在handler中发送message消息
mhandler.sendMessage(message);
5 Thread获取网络数据:
new Thread(){
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
}
}.start();
整体代码:
在splashAcitivty中先检查版本,如果有版本更新,使用handlermessage进行消息处理,并弹窗提示更新。
package com.example.mobliesoft.Activity;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import org.json.JSONException;
import org.json.JSONObject;
import com.example.mobliesoft.R;
import com.example.mobliesoft.R.id;
import com.example.mobliesoft.R.layout;
import com.example.mobliesoft.utils.StreamUtils;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
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.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class SplashActivity extends Activity {
protected static final int CODE_NEDD_UPDATA = 1; //更新
protected static final int CODE_JSON_ERROR = 0;//json错误
protected static final int CODE_URL_ERROR = 2;//URL错误
protected static final int CODE_NET_ERROR = 3;//网络错误
private String mVersionName;
private String mDescription;
private String mVersionCode;
private String mDownLoadUrl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView Version = (TextView)findViewById(R.id.tv_version);
Version.setText("Version " + GetVerisonName());
CheckVerison();
}
/**
* 获取版本名称
* @return
*/
private String GetVerisonName(){
PackageManager packageManager = getPackageManager();
try {
PackageInfo packageInfo = packageManager.getPackageInfo(getPackageName(), 0);
int versionCode = packageInfo.versionCode;
String versionName = packageInfo.versionName;
System.out.println("versionName" + versionName + "versionCode "+ versionCode +";");
return versionName;
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
/**
* 获取版本号
* @return
*/
private int GetVerisonCode(){
PackageManager packageManager = getPackageManager();
try {
PackageInfo packageInfo = packageManager.getPackageInfo(getPackageName(), 0);
int versionCode = packageInfo.versionCode;
return versionCode;
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return -1;
}
// 消息处理handler
private Handler mhandler = new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case CODE_NEDD_UPDATA:
ShowDialog();
break;
case CODE_JSON_ERROR:
Toast.makeText(getBaseContext(), "Json_ERROR", Toast.LENGTH_SHORT).show();
break;
case CODE_URL_ERROR:
Toast.makeText(getBaseContext(), "URL错误", Toast.LENGTH_SHORT).show();
break;
case CODE_NET_ERROR:
Toast.makeText(getBaseContext(), "网络错误", Toast.LENGTH_SHORT).show();
System.out.println("handler net error!!!");
break;
default:
break;
}
};
};
/**
* 检查版本
* 知识点:message使用,HTTP获得网络数据,JSON对象的解析
*/
private void CheckVerison()
{
System.out.println("Thread!!!");
new Thread(){
@Override
public void run() {
Message message = Message.obtain();
System.out.println("Run!!!");
try {
//建立HTTP连接 并使用GET方法进行获取数据
URL url = new URL("http://192.168.56.1:8080/updata.json");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.connect();
//服务器连接正常
int responseCode = connection.getResponseCode();
System.out.println("responseCode : "+responseCode+"!!!!");
if(responseCode==200)
{
//获取数据,并将数据保存到result中
InputStream inputStream = connection.getInputStream();
String result = StreamUtils.readFromStream(inputStream);
System.out.println("网络数据: "+result);
//解析json
JSONObject json;
try {
json = new JSONObject(result);
mVersionName = json.getString("VersionName");
mVersionCode = json.getString("VersionCode");
mDescription = json.getString("Description");
mDownLoadUrl = json.getString("DownLoadUrl");
System.out.println("Version _ json : "+mVersionName);
System.out.println("description _ json "+ mDescription);
if(GetVerisonCode()<Integer.parseInt(mVersionCode))
{
message.what=CODE_NEDD_UPDATA;
System.out.println("需要更新");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
message.what=CODE_JSON_ERROR;
e.printStackTrace();
}
}
System.out.println("URL: "+connection.getDate()+"");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
message.what=CODE_URL_ERROR;
e.printStackTrace();
System.out.println("CODE_URL_ERROR");
} catch (IOException e) {
// TODO Auto-generated catch block
message.what=CODE_NET_ERROR;
e.printStackTrace();
System.out.println("CODE_NET_ERROR");
}
finally{
mhandler.sendMessage(message);
}
}
}.start();
}
/**
* 展示更新对话框
*/
protected void ShowDialog() {
AlertDialog.Builder Dialog = new AlertDialog.Builder(this);
Dialog.setTitle("检查更新");
Dialog.setMessage(mDescription);
Dialog.setPositiveButton("立即更新", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
Dialog.setNegativeButton("以后再说", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
Dialog.show();
}
};