从服务器下载&&OTA升级&&JSON文件解析_hejunw的博客-程序员宅基地

技术标签: Android JSON文件解析  java  OTA  Android  

mainActivity :

package com.example.he.toby.otademo;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


    private TextView textView;
    private Button SerxmlOta;
    private Button downLoadJar;

    Update manager;
    private static final int DOWNLOAD = 1;

    private static final int DOWNLOAD_FINISH = 2;
    private String tag = getClass().getSimpleName();
    private static final String MJAR_TEST = "http://192.168.11.39:8080/Uiauto-Jar/first.txt";

    Update update ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.textView = (TextView) findViewById(R.id.textView);

         update = new Update(MainActivity.this);
        // TODO: 2016/5/20
        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                UpdateManger mUpdatemanger = new UpdateManger(MainActivity.this);
                mUpdatemanger.checkUpdateInfo();
                Toast.makeText(getApplicationContext(), "调用updatemanger", Toast.LENGTH_SHORT).show();
            }
        });

        // TODO: 2016/5/20  版本
        findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String versionName = getVersionName();
                int versionCode = getVersionCode();
                Toast.makeText(getApplicationContext(), "VersionName-->" + versionName + "\n" +
                        "VersionCode-->" + versionCode, Toast.LENGTH_SHORT).show();
                textView.setText("APP版本:" + "\nVersionName-->" + versionName + "\n" + "VersionCode-->" + versionCode);
                Log.e("MainActivity", "VersionName-->" + versionName+","+"VersionCode-->" + versionCode);

            }
        });

        // TODO: 2016/5/20  serxml解析
        this.SerxmlOta = (Button) findViewById(R.id.button3);
        SerxmlOta.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "serxml 解析更新xml文件", Toast.LENGTH_SHORT).show();
                update.checkUpdate();
            }
        });

        // TODO: 2016/5/20  download JAR
        findViewById(R.id._dlj).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e(tag, "down load jar ");
                update.resultJson();
            }
        });

    }


    private String getVersionName() {
        String versionname;
        try {
            PackageManager packageManager = getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(this.getPackageName(), 0);
            versionname = packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            versionname = null;
        }
        return versionname;
    }
    private int getVersionCode() {
        Log.e(tag, "当前的版本调用");
        int versionCode;
        try {
            PackageManager packageManager = getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(this.getPackageName(), 0);
            versionCode = packageInfo.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            versionCode = -1;
        }
        return versionCode;
    }
}

UpdateManger类 
package com.example.he.toby.otademo;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;

/**
 * Created by toby.he on 2016/3/17.
 */
public class UpdateManger {

    // 应用程序Context
    private Context mContext;
    // 提示消息
    private String updateMsg = "有最新的软件包,请下载!";
    // 下载安装包的网络路径
    private String apkUrl = "http://192.168.11.39:8080/Tracer/GigasetTracers1.0.apk";
    private Dialog noticeDialog;// 提示有软件更新的对话框
    private Dialog downloadDialog;// 下载对话框
    private static final String savePath = Environment.getExternalStorageDirectory().toString()+File.separator; // 保存apk的文件夹

    private static final String saveFileName = savePath + "UpdateDemoRelease.apk";
    // 进度条与通知UI刷新的handlermsg常量
    private ProgressBar mProgress;
    private static final int DOWN_UPDATE = 1;
    private static final int DOWN_OVER = 2;
    private int progress;// 当前进度

    private Thread downLoadThread; // 下载线程

    private boolean interceptFlag = false;// 用户取消下载

    private  String TAG = getClass().getSimpleName();

    HashMap<String, String> mHashMap;


    // 通知处理刷新界面的handler
    private Handler mHandler = new Handler() {
        @SuppressLint("HandlerLeak")
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case DOWN_UPDATE:
                    mProgress.setProgress(progress);
                    break;
                case DOWN_OVER:
                    installApk();
                    break;
            }
            super.handleMessage(msg);
        }
    };
    public UpdateManger(Context context) {
        this.mContext = context;
    }
    // 显示更新程序对话框,供主程序调用
    public void checkUpdateInfo() {
     //  if (isUpdate()) {

           showNoticeDialog();
    //    }
//    else {
//            Toast.makeText(mContext, "不更新", Toast.LENGTH_LONG).show();
//        }

    }

    private int getVersionCode(Context context) {
        int versionCode = 0;
        try {
            versionCode = context.getPackageManager().getPackageInfo("com.example.he.toby.otademo", 0).versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionCode;
    }

    private boolean isUpdate() {
        {

            int versionCode = getVersionCode(mContext);

            //本地的XML更新
            // InputStream inStream = ParseXmlService.class
            //
            // .getResourceAsStream("version.xml");
            // InputStream inStream = ParseXmlService.class
            // .getClassLoader()
            // .getResourceAsStream(
            // "version1.xml");

            //解析网络xml进行更新软件
            URL url = null;
            try {
                url = new URL("http://192.168.11.39:8080/Tracer/ServerVersion.xml");
                Log.e(TAG, "down load ");
            } catch (MalformedURLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            HttpURLConnection conn = null;
            try {
                conn = (HttpURLConnection)url.openConnection();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            conn.setConnectTimeout(5000);
            InputStream is = null;
            try {
                is = conn.getInputStream();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }


            ParseXmlService service = new ParseXmlService();
            try {
                mHashMap = service
                        .parseXml(is);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (null != mHashMap) {
                int serviceCode = Integer.valueOf(mHashMap.get("version"));

                if (serviceCode > versionCode) {
                    Log.e(TAG,"当前版本是version-->"+versionCode+"\n服务server版本是-->"+serviceCode);
                    return true;
                }
            }
            return false;
        }
    }


    private void showNoticeDialog() {
        android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(
                mContext);// Builder,可以通过此builder设置改变AleartDialog的默认的主题样式及属性相关信息
        builder.setTitle("软件版本更新");
        builder.setMessage(updateMsg);
        builder.setPositiveButton("下载", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();// 当取消对话框后进行操作一定的代码?取消对话框
                showDownloadDialog();
            }
        });
        Log.e("shownoticeD","show noticedialog");
        builder.setNegativeButton("以后再说", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        noticeDialog = builder.create();
        noticeDialog.show();
    }
    protected void showDownloadDialog() {
        android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(mContext);
        builder.setTitle("软件版本更新");
        final LayoutInflater inflater = LayoutInflater.from(mContext);
        View v = inflater.inflate(R.layout.progress, null);

        mProgress = (ProgressBar) v.findViewById(R.id.progress);
        builder.setView(v);// 设置对话框的内容为一个View
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                interceptFlag = true;
            }
        });
        downloadDialog = builder.create();
        downloadDialog.show();
        downloadApk();
    }

    private void downloadApk() {
        downLoadThread = new Thread(mdownApkRunnable);
        downLoadThread.start();
    }

    protected void installApk() {
        File apkfile = new File(saveFileName);
        if (!apkfile.exists()) {
            return;
        }
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setDataAndType(Uri.parse("file://" + apkfile.toString()),
                "application/vnd.android.package-archive");// File.toString()会返回路径信息
        mContext.startActivity(i);
    }

    private Runnable mdownApkRunnable = new Runnable() {
        @Override
        public void run() {
            URL url;
            try {
                url = new URL(apkUrl);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.connect();
                int length = conn.getContentLength();

                conn.setConnectTimeout(5000);
                InputStream ins = conn.getInputStream();

                File file = new File(savePath);
                if (!file.exists()) {
                    file.mkdir();
                }
                String apkFile = saveFileName;
                File ApkFile = new File(apkFile);
                FileOutputStream outStream = new FileOutputStream(ApkFile);
                int count = 0;
                byte buf[] = new byte[1024];
                do {
                    int numread = ins.read(buf);
                    count += numread;
                    progress = (int) (((float) count / length) * 100);
                    // 下载进度
                    mHandler.sendEmptyMessage(DOWN_UPDATE);
                    if (numread <= 0) {
                        // 下载完成通知安装
                        mHandler.sendEmptyMessage(DOWN_OVER);
                        break;
                    }
                    outStream.write(buf, 0, numread);
                } while (!interceptFlag);// 点击取消停止下载
                outStream.close();
                ins.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };



}



Update 类  
package com.example.he.toby.otademo;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by toby.he on 2016/3/22.
 */
public class Update {

    private static final int DOWNLOAD_ING = 1;

    private static final int DOWNLOAD_FINISH = 2;

    private String TAG = getClass().getSimpleName().toString();

    HashMap<String, String> mHashMap;

    private String mSavePath;

    private int progress;

    private boolean cancelUpdate = false;

    private Context mContext;

    private ProgressBar mProgress;

    private Dialog mDownloadDialog;


    private static final String PATH = "http://192.168.11.39:8080/Tracer/ServerVersion.html";

    private String mVersion_code;
    private String mVersion_name;
    private String mVersion_desc;
    private String mVersion_path;


    // TODO: 2016/4/12 jar文件地址
    private  String mJar_name;
    private  String mJar_url;
    public static final String MJAR_PATH = "http://192.168.11.39:8080/Uiauto-Jar/test.html";


    String[] nameList=null ;

    // 通知处理刷新界面的handler
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {

            switch (msg.what) {
                case DOWNLOAD_ING:
                    mProgress.setProgress(progress);
                    break;
                case DOWNLOAD_FINISH:
                    // 隐藏当前下载对话框
                    mDownloadDialog.dismiss();
                    //安装APK
                    installApk();
                    break;
                default:
                    break;
            }
        }
    };

    // TODO: 2016/4/12  获取版本
    private Handler mGetVersion = new Handler() {
        public void handleMessage(Message msg) {
            JSONObject jsonObject = (JSONObject) msg.obj;
            System.out.println(jsonObject.toString());
            try {
                mVersion_code = jsonObject.getString("version_code");
                mVersion_name = jsonObject.getString("version_name");
                mVersion_desc = jsonObject.getString("version_desc");
                mVersion_path = jsonObject.getString("version_path");

                if (isUpdate()) {
                    Toast.makeText(mContext, "需要更新", Toast.LENGTH_SHORT).show();
                    // 显示提示更新对话框
                    showNoticeDialog();
                } else {
                    Toast.makeText(mContext, "已是最新版本", Toast.LENGTH_SHORT).show();
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };


    public Update(Context context) {
        this.mContext = context;
        Log.e(TAG, "update");
    }


    //检查更新
    public void checkUpdate() {

        RequestQueue requestQueue = Volley.newRequestQueue(mContext);
        JsonObjectRequest request = new JsonObjectRequest(PATH, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Message msg = Message.obtain();
                msg.obj = jsonObject;
                mGetVersion.sendMessage(msg);
                Log.e(TAG, jsonObject.toString());
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.e(TAG, volleyError.toString());
            }
        });
        requestQueue.add(request);


    }


    public static int getLineNumber(Exception e){
        StackTraceElement[] trace =e.getStackTrace();
        if(trace==null||trace.length==0) return -1; //
        return trace[0].getLineNumber();
    }


    // TODO: 2016/4/12 检测jar文件
   public void  checkJar()
    {
        try {
            String result = readParse(mJar_url);
            Toast.makeText(mContext,"result-->"+result,Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    Handler mGetJarHandler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            JSONArray jsonArray = null;
            ArrayList<HashMap<String, String>> list = new ArrayList<>();

            try {
                jsonArray = new JSONArray(msg.obj);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            //   JSONObject jsonObject = (JSONObject) msg.obj;
         //   System.out.println(jsonObject.toString());
            for (int i = 0 ; i <jsonArray.length();i++)
            {
                try {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                HashMap<String,String> map = new HashMap<String,String>();
                map.put("NAME",mJar_name);
                map.put("URL", mJar_url);
                list.add(map);
            }

            Iterator<HashMap<String, String>> it = list.iterator();
            StringBuffer strbf = new StringBuffer();
            while (it.hasNext()) {
                HashMap<String, String> ma = it.next();
                  //  strbf.append(ma.)
            }


        }
    };


    // TODO: 2016/4/12  多选对话框
    private void MultiChoiceDialog() {

        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setIcon(R.mipmap.ic_launcher);
        builder.setTitle("所有的JAR");


       // final String[] hobbies = {"篮球", "足球", "网球", "斯诺克"};
        //    设置一个单项选择下拉框
        /**
         * 第一个参数指定我们要显示的一组下拉多选框的数据集合
         * 第二个参数代表哪几个选项被选择,如果是null,则表示一个都不选择,如果希望指定哪一个多选选项框被选择,
         * 需要传递一个boolean[]数组进去,其长度要和第一个参数的长度相同,例如 {true, false, false, true};
         * 第三个参数给每一个多选项绑定一个监听器
         */
        builder.setMultiChoiceItems(nameList, null, new DialogInterface.OnMultiChoiceClickListener()
        {
            StringBuffer sb = new StringBuffer(100);
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked)
            {
                if(isChecked)
                {
                    sb.append(nameList[which] + ", ");
                }
              //  Toast.makeText(mContext, "爱好为:" + sb.toString(), Toast.LENGTH_SHORT).show();
            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {

                Log.e(TAG, "sure");
              //  Toast.makeText(mContext,"这里添加要下载执行",Toast.LENGTH_SHORT).show();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {

            }
        });
        builder.show();
    }




    /*
     * 与本地版本比较判断是否需要更新
    */
    public boolean isUpdate() {

        int serverVersion = Integer.parseInt(mVersion_code);
        int localVersion = 1;

        localVersion = getVersionCode(mContext);

        if (serverVersion > localVersion) {
            Log.e(TAG, "当前版本是version-->" + localVersion + "\n服务server版本是-->" + serverVersion);
            return true;
        }


        return false;
    }


    private int getVersionCode(Context context) {
        int versionCode = 0;
        try {
            versionCode = context.getPackageManager().getPackageInfo("com.example.he.toby.otademo", 0).versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionCode;
    }


    private void showNoticeDialog() {
        Log.e(TAG, "showNoticeDialog()");

        AlertDialog.Builder builder = new Builder(mContext);
        builder.setTitle("提示");
        String msg = "有软件更新,要下载更新么 ?\n" + mVersion_desc;
        builder.setMessage(msg);
        builder.setPositiveButton("UPDATE", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                //隐藏对话框
                dialog.dismiss();

                //显示下载对话框
                showDownloadDialog();
            }
        });

        builder.setNegativeButton("Cancle", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

        builder.create().show();
    }

    private void installApk() {
        File apkfile = new File(mSavePath, mVersion_name);
        if (!apkfile.exists()) {
            return;
        }
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setDataAndType(Uri.parse("file://" + apkfile.toString()), "application/vnd.android.package-archive");
        mContext.startActivity(i);
    }


    private void showDownloadDialog() {

        Builder builder = new Builder(
                mContext);
        builder.setTitle("更新中");

        final LayoutInflater inflater = LayoutInflater.from(mContext);
        View v = inflater.inflate(R.layout.progress, null);
        mProgress = (ProgressBar) v.findViewById(R.id.progress);
        builder.setView(v);

        builder.setNegativeButton(R.string.soft_update_cancel, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        cancelUpdate = true;
                    }
                });
        mDownloadDialog = builder.create();
        mDownloadDialog.show();
        // TODO: 2016/3/22 下载
        downloadApk();
    }

    private void downloadApk() {

        new downloadApkThread().start();
    }

    private class downloadApkThread extends Thread {
        @Override
        public void run() {
            Log.e(TAG, " Will Downloadapk");
            try {
                if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                    String sdpath = Environment.getExternalStorageDirectory() + "/";
                    mSavePath = sdpath + "download";

                    File file = new File(mSavePath);
                    if (!file.exists()) {
                        file.mkdir();
                    }

                    // TODO: 2016/4/1 下载
                    HttpURLConnection conn = (HttpURLConnection) new URL(mVersion_path).openConnection();
                    conn.connect();
                    int length = conn.getContentLength();
                    InputStream is = conn.getInputStream();

                    File apkFile = new File(mSavePath, mVersion_name);
                    FileOutputStream fos = new FileOutputStream(apkFile);
                    int count = 0;
                    byte buf[] = new byte[1024];
                    do {
                        int numread = is.read(buf);
                        count += numread;
                        progress = (int) (((float) count / length) * 100);

                        // 更新进度条
                        mHandler.sendEmptyMessage(DOWNLOAD_ING);
                        if (numread < 0) {
                            mHandler.sendEmptyMessage(DOWNLOAD_FINISH);
                            break;
                        }
                        fos.write(buf, 0, numread);
                    } while (!cancelUpdate);
                    fos.close();
                    is.close();
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            //  mDownloadDialog.dismiss();
        }
    }



    /**
     * 从指定的URL中获取数组
     * @param urlPath
     * @return
     * @throws Exception
     */
    public String readParse(String urlPath) throws Exception {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] data = new byte[1024];
        int len = 0;
        URL url = new URL(urlPath);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        InputStream inStream = conn.getInputStream();
        while ((len = inStream.read(data)) != -1) {
            outStream.write(data, 0, len);
        }
        inStream.close();
        return new String((outStream.toByteArray()));//通过out.Stream.toByteArray获取到写的数据
    }


    /**
     * 解析
     *
     * @throws JSONException
     */
    private  ArrayList<HashMap<String, Object>> Analysis(String jsonStr)
            throws JSONException {
        /******************* 解析 ***********************/
        JSONArray jsonArray = null;
        Log.d(TAG, "后:" + jsonStr);
        // 初始化list数组对象
        ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
        //转化成 JSONObject        JSONObject js = new JSONObject(jsonStr);

        if (js.has("TestJar"))
        {
            jsonArray  = js.getJSONArray("TestJar");
            if (jsonArray.length()>0)
            {
                for (int  i = 0; i<jsonArray.length();i++)
                {
                    JSONObject jsonOj = jsonArray.getJSONObject(i);

                    String NAME = "";
                    String URL = "";
                    if(jsonOj.has("NAME")){
                        NAME = jsonOj.getString("NAME");
                    }
                    if(jsonOj.has("URL")){
                        URL = jsonOj.getString("URL");
                    }

                    // 初始化map数组对象
                    HashMap<String, Object> map = new HashMap<String, Object>();
                    map.put("NAME", NAME);
                    map.put("URL", URL);

                    list.add(map);

                    Log.d(TAG, "SearchResultJson>>>" + " NAME=" + NAME + " URL=" + URL);
                    System.out.println("name:" + NAME);


                }
            }

        }

    //    nameList = new String[jsonArray.length()];

        return list;
    }


    /**
     * readParseString)从服务器端获取数据
     * AnalysisString)解析json数据
     */
    ArrayList<HashMap<String, Object>> allData ;
    String  jsonObject = null;

    public  void resultJson() {

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {

                    jsonObject = readParse(MJAR_PATH);
                    Log.e(TAG, "从服务器中读取到的数据:\n"+jsonObject);


                    allData = Analysis(jsonObject);


                    Iterator<HashMap<String, Object>> it = allData.iterator();
                    while (it.hasNext()) {
                        Map<String, Object> ma = it.next();

                        Log.e("name&&url","NAME: "+ma.get("NAME")+",URL:"+ma.get("URL")+"\n");

                    }

             //       MultiChoiceDialog();


                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
//                try {
//                    allData = Analysis(readParse(MJAR_PATH));

    }




}


xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="UpdateManger_OTA"
        android:id="@+id/button"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="版本code"
        android:id="@+id/button2"
        android:layout_below="@+id/button"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignRight="@+id/button"
        android:layout_alignEnd="@+id/button" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView"
        android:layout_below="@+id/button2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Server-xml解析-OTA"
        android:id="@+id/button3"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="DownLoadJar从服务器上下载"
        android:id="@+id/_dlj"
        android:layout_below="@+id/button3"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignRight="@+id/button3"
        android:layout_alignEnd="@+id/button3" />
</RelativeLayout>







版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/hejunw/article/details/51482221

智能推荐

理解 RESTful API 面向资源 幂等性-程序员宅基地

如何理解RESTful的幂等性 我来答分享举报浏览 3612 次1个回答#再见,2018!# 2018要结束了,你还有哪些心愿没完成??最佳答案热心网友 2017-07-02等幂性(Idempotence)等幂性简单点说就是一次请求和多次请求,资源的状态是一样。比如GET和HEAD,不论你请求多少次,资源还是在那里。请注意,DELETE和PUT也是等幂的,...

grid宽度自适应中auto-fit和auto-fill的区别_grid auto-fill-程序员宅基地

除了显式的指定列大小之外,CSS Grid 还有个非常强大的功能 —— 模式填充(repeat-to-fill)列然后对内容进行自动布局。也就是说,开发者只需要指定列数,自适应方面的事情(视口尺寸小则显示列数少,反之则多)交给浏览器来处理就行了,也不需要用媒体查询。比如我们可以用grid-template-columns:repeat(6,1fr)来指定该容器中包含6列,每列宽度轨道..._grid auto-fill

第二周项目3-两种排序算法的运行时间-程序员宅基地

问题及代码:/* *Copyright (c)2016,烟台大学计算机与控制工程学院 *All rights reserved. *文件名称:qq.cpp *作 者:房斐 *完成日期:2015年9月8日 *版 本 号:v1.0 * *问题描述:排序是计算机科学中的一个基本问题,产生了很多种适合不同情况下适 用的算法,也一直作为...

kali对未知web server透测试2-程序员宅基地

环境介绍主机IP(NAT)kali192.168.252.128靶机2未知靶机下载:https://pan.baidu.com/s/1u5xbmU13wheXE4_M508qFw提取码:1234打开靶机主机探测NAT网卡位于192.168.252.0/24网段进行主机探测netdiscover -i eth0 -r 192.168.252.0/24信息收集访问web界面没有发现可疑渗透点,这里使用目录爆破,看一下有没有隐藏界面。 d

面试题笔试-带答案-1_叭11p厂三三1ry目中:::‘l才上成月rq-程序员宅基地

一. 选择题(234)1.下面中哪两个可以在 A 的子类中使用:( )class A {protected int method1 (int a, int b) {return 0;}}A. public int method 1 (int a, int b) { return 0; } B. private int method1 (int a, int b) { return 0; } C. private int method1 (int a, long b) { return 0; }_叭11p厂三三1ry目中:::‘l才上成月rq

comparable和comparator之间的区别_请说明使用comparator比使用comparable好在什么地方-程序员宅基地

comparable和comparator之间的区别Java 中为我们提供了两种比较机制:Comparable 和 Comparator,二者都是用来实现对象的比较、排序。下面分别对Comparable 和 Comparator做具体介绍并总结。Comparable:Comparable可以认为是一个内比较器,实现了Comparable接口的类有一个特点,就是这些类是可以和自己比较的,至于具体和另一个实现了Comparable接口的类如何比较,则依赖compareTo方法的实现。如果add进入一个_请说明使用comparator比使用comparable好在什么地方

随便推点

Problem F: C语言习题 不等长字符串排序_问题 f: c语言习题 不等长字符串排序-程序员宅基地

Time Limit: 1 Sec Memory Limit:128 MBSubmit: 527 Solved: 282[Submit][Status][Web Board]Description在主函数中输入n(nInputn和n个不等长字符串Outputn个已排好序的字符串Sample Input5abc_问题 f: c语言习题 不等长字符串排序

C语言中auto,register,static,const,volatile的区别-程序员宅基地

1)auto  这个关键字用于声明变量的生存期为自动,即将不在任何类、结构、枚举、联合和函数中定义的变量视为全局变量,而在函数中定义的变量视为局部变量。这个关键字不怎么多写,因为所有的变量默认就是auto的。 (2)register  这个关键字命令编译器尽可能的将变量存在CPU内部寄存器中而不是通过内存寻址访问以提高效率。 (3)static  常见的两种用途

统计字符 c语言程序,统计字符个数C语言程序.doc-程序员宅基地

统计字符个数C语言程序统计字符个数的C语言程序输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。[题解分析]:统计程序的典型结构:初值归零: int n=0; // n的取名最好与被统计的对象相关。输入数据。计数: 循环事件{ if ( 相关对象出现 ) n++; }输出。//当存在多项统计时,注意输出序列不要混乱。//注解:因为统计是针对输入的,所以输入可以放在循环事件内。..._编写程序统计分析文本,求其中出现的字符种数

js调用摄像头拍照_js 怎么调整摄像头分辨率_香煎三文鱼的博客-程序员宅基地

<!DOCTYPE html><html lang="zh"><head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>拍照_js 怎么调整摄像头分辨率

JVM初级篇-程序员宅基地

JVM1:如何查看虚拟机是哪个类型cmd命令 查看 java -version 查看本地虚拟机版本,我本机安装的是HotSpot3:特点一次编译,到处运行 自动内存管理 自动垃圾回收机机制 JVM就是二进制字节码的运行环境4: 结构硬件-----windows/linux 操作系统----JVM虚拟机---- 二进制字节码文件-----java文件5:jvm ,jre , jdk, javase , javaee 都是什么jvm 是虚拟机 j...

JDBC:JDBCUtils工具类_jdbcutils.class-程序员宅基地

一、JDBCUtils工具类作用:使代码更加的简洁,更具有逻辑性和复用性。简介:连接数据库的四个参数:驱动类、URL、用户名、密码若要更改连接数据库信息,那么就可以直接在配置文件中修改这四个参数;具体实现代码如下:JdbcUtils.classpackage com.health.JDBC.util;import java.io.FileNotFoundExcept..._jdbcutils.class