public String sendJson() {
VersionDownApk downapk = new VersionDownApk();
downapk.setCmd(getVersionName());// 应用版本号
List<Clients> listclinets = new ArrayList<Clients>();
Clients clients = new Clients();
clients.setMac(getLocalMacAddress() + "");// mac地址
// clients.setNetType(getCurrentNetType(this));//联网类型
clients.setNetType("");
clients.setType(android.os.Build.MODEL);// 手机类型
clients.setSn(Secure.getString(getContentResolver(), Secure.ANDROID_ID));// 机身串号
clients.setOs("android");
listclinets.add(clients);
downapk.setListclient(listclinets);
JSONObject j = new JSONObject();
JSONObject js = new JSONObject();
JSONObject jso = new JSONObject();
JSONObject jsapp = new JSONObject();
try {
jso.put("os", "android");
jso.put("mac", "00-00-00-00-00-00");
// jso.put("mac", clients.getMac()+"");
jso.put("sn", clients.getSn());
jso.put("type", clients.getType());
jso.put("netType", " ");
jsapp.put("updateTime", " ");
jsapp.put("version", getVersionName());
jsapp.put("name", "pospad");
jsapp.put("lastVersion", getVersionName());
js.put("client", jso);
js.put("app", jsapp);
j.put("data", js);
j.put("cmd", "getNewVersion");
System.out.println(j.toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return j.toString();
}
/**
** @方法名: SocketClient
* @描述: TODO(更新版本)
* @作者 张丽平
* @参数 @param Host
* @参数 @param port
* @返回值 void
* @throws
*/
public void SocketClient(String Host, int port) {
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
Thread.currentThread().sleep(100);
int flag = 0;
ByteBuffer buf = ByteBuffer.allocate(1024);
ByteBuffer send = ByteBuffer.allocate(1024);
boolean updateapk = false;
int totalPacks=0;
JsonObjects jsonobjects = new JsonObjects();
try {
socket = SocketChannel.open(new InetSocketAddress(
"192.168.1.116", 12306));
socket.configureBlocking(false);
Selector mSelector = Selector.open();
socket.register(mSelector, SelectionKey.OP_READ
| SelectionKey.OP_WRITE);
int packeindexint = 0;
int ii = 0;
while (mSelector.select() > 0) {
for (SelectionKey sk : mSelector.selectedKeys()) {
// 向服务器端发送数据
if (flag == 0) {
SocketChannel scwrite = (SocketChannel) sk
.channel();
ByteBuffer sendBuffer = ByteBuffer
.wrap(sendJson().getBytes("UTF-8"));
scwrite.write(sendBuffer);
flag = 1;
}
// 读取服务器端数据
if (sk.isReadable()) {
// 使用NIO读取Channel中的数据
SocketChannel sc = (SocketChannel) sk
.channel();
ByteBuffer buffer = ByteBuffer
.allocate(1024);
sc.read(buffer);
buffer.flip();
String receivedString = Charset
.forName("UTF-8").newDecoder()
.decode(buffer).toString();
sk.interestOps(SelectionKey.OP_READ);
// Log.e("errors", "接收到来自服务器"
// + sc.socket()
// .getRemoteSocketAddress()
// + "的信息:" + receivedString);
JSONObject jreads;
JSONObject jsonData;
try {
jreads = new JSONObject(receivedString
.toString());
jsonData = jreads.getJSONObject("data");
String jscmd = jreads.getString("cmd");
if (jscmd.equals("getNewVersion")) {
JSONObject objectapp = jsonData
.getJSONObject("app");
JSONObject objectclient = jsonData
.getJSONObject("client");
totalPacks = objectapp
.getInt("totalPacks");
if (objectclient.get("needUpdate")
.equals("true")) {
File dir = new File(IMP.SAVE_APK,
"tests.apk");
if (!dir.exists()){
dir.delete();
}
/**
* 分包请求服务器端
*/
JSONObject js = new JSONObject();
JSONObject jsono = new JSONObject();
JSONObject jsonobjectmsg = new JSONObject();
JSONObject jsonobjectapp = new JSONObject();
JSONObject jsonobjectclient = new JSONObject();
totalPacks = objectapp
.getInt("totalPacks");
jsonobjectmsg
.put("packSize",
objectapp
.getInt("packSize"));
jsonobjectmsg.put("packIndex",
ii);
jsonobjectmsg
.put("newVersion",
objectapp
.get("newVersion"));
jsonobjectmsg
.put("newVersionName",
objectapp
.get("newVersionName"));
jsonobjectmsg
.put("newVersionSize",
objectapp
.get("newVersionSize"));
jsonobjectapp
.put("updateTime",
objectapp
.get("updateTime"));
jsonobjectapp
.put("version",
objectapp
.get("version"));
jsonobjectapp.put("name",
objectapp.get("name"));
jsonobjectapp.put(
"lastVersion",
getVersionName());
jsonobjectclient.put("type",
android.os.Build.MODEL);
jsonobjectclient.put("mac",
"00-00-00-00-00-00");
jsonobjectclient.put("os",
"android");
jsonobjectclient.put(
"sn",
Secure.getString(
getContentResolver(),
Secure.ANDROID_ID));
jsonobjectclient.put("netType",
" ");
jsono.put("msg", jsonobjectmsg);
jsono.put("app", jsonobjectapp);
jsono.put("client",
jsonobjectclient);
js.put("data", jsono);
js.put("cmd", "update");
jsonobjects.setJso(js);
SocketChannel scwrite = (SocketChannel) sk
.channel();
ByteBuffer sendBuffer = ByteBuffer
.wrap(js.toString()
.getBytes(
"UTF-8"));
scwrite.write(sendBuffer);
}
} else if (jscmd.equals("update")) {
// 下载数据
try {
BASE64Decoder decoder = new BASE64Decoder();
File path = new File(
IMP.SAVE_APK);
if (!path.exists()) {
path.mkdirs();
}
File dir = new File(path,
"tests.apk");
if (!dir.exists())
dir.createNewFile();
String datas = jsonData
.getJSONObject("msg")
.getString("data");
try {
decoderBase64File(datas,IMP.SAVE_APK
+ "/tests.apk");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//writeapkbuffer.append(datas);
} catch (JSONException e) {
// TODO Auto-generated catch
// block
e.printStackTrace();
}
SocketChannel scwrite = (SocketChannel) sk
.channel();
JSONObject jswrite = jsonobjects
.getJso();
ii++;
jswrite.getJSONObject("data").getJSONObject("msg").put("packIndex",ii);
ByteBuffer sendBuffer = ByteBuffer
.wrap(jswrite.toString()
.getBytes("UTF-8"));
scwrite.write(sendBuffer);
if(ii>=totalPacks){
try {
Log.e("erros", writeapkbuffer.toString());
// toFile(writeapkbuffer.toString(), IMP.SAVE_APK
// + "/tests.apk");
installApk(new File(IMP.SAVE_APK+"/tests.apk"));
} catch (Exception e) {
// TODO Auto-generated catch
// block
e.printStackTrace();
}
return;
}
}
} catch (JSONException e2) {
// TODO Auto-generated catch block
Log.e("error", ii+".....................");
packeindexint = ii;
e2.printStackTrace();
}
}
mSelector.selectedKeys().remove(sk);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
// 安装apk
protected void installApk(File file) {
Intent intent = new Intent();
// 执行动作
intent.setAction(Intent.ACTION_VIEW);
// 执行的数据类型
intent.setDataAndType(Uri.fromFile(file),
"application/vnd.android.package-archive");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}