• 草稿


    package com.mialab.parse;
    
    import java.io.Serializable;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    
    import com.mialab.parse.bean.Person;
    import com.mialab.parse.util.HttpUtil;
    import com.mialab.parse.util.JsonTool;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;
    
    public class MainActivity extends Activity implements OnClickListener {
        private static String PATH = "http://192.168.1.108:8080/jsonserver/servlet/JsonServlet";
        private static final String TAG = "MainActivity";
        private Button person, persons, listString, listMap;
        private Person demoperson;
        private List<Person> list2;
        private List<String> list3;
        private List<Map<String, Object>> list4;
        private Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                case -2:
                    Toast.makeText(MainActivity.this, (String) msg.obj,
                            Toast.LENGTH_LONG).show();
                    // finish();
                    break;
                case 1:
                    Intent intent = new Intent(MainActivity.this,
                            ResultActivity.class);
                    intent.putExtra("click_flag", "person"); // 标记当前intent是按钮 person
                    // intent.putExtra("person", (Serializable) demoperson);
                    intent.putExtra("person", demoperson);
                    startActivity(intent);
                    break;
                case 2:
                    Intent intent2 = new Intent(MainActivity.this,
                            ResultActivity.class);
                    intent2.putExtra("click_flag", "persons"); // 标记当前intent按钮是persons
                    intent2.putExtra("persons", (Serializable) list2);
                    startActivity(intent2);
                    break;
                case 3:
                    Intent intent3 = new Intent(MainActivity.this,
                            ResultActivity.class);
                    intent3.putExtra("click_flag", "listString"); // 标记当前intent按钮是listString
                    intent3.putStringArrayListExtra("listString",
                            (ArrayList<String>) list3); // 参数中的list要加强制转换
                    startActivity(intent3);
                    break;
                case 4:
                    Intent intent4 = new Intent(MainActivity.this, ResultActivity.class);
                    intent4.putExtra("click_flag", "listMap"); // 标记 当前intent按钮是listMap
                    // 用Intent传递List<map<string,object>> 对象,其中HashMap实现了Serializable
                    intent4.putExtra("listMap", (Serializable) list4);
                    startActivity(intent4);
                    break;
                }
            }
        };
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            person = (Button) findViewById(R.id.person);
    
            persons = (Button) findViewById(R.id.persons);
            listString = (Button) findViewById(R.id.listString);
            listMap = (Button) findViewById(R.id.listMap);
    
            person.setOnClickListener(this);
            persons.setOnClickListener(this);
            listString.setOnClickListener(this);
            listMap.setOnClickListener(this);
    
        }
    
        public void sendMessage(int index, String message) {
            Message msg = new Message();
            msg.obj = message;
            msg.what = index;
            handler.sendMessage(msg);
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.person:
                final String path1 = PATH + "?action_flag=person";
                Toast.makeText(MainActivity.this, "正在下载网络数据,请稍候...",
                        Toast.LENGTH_LONG).show();
                new Thread() {
                    public void run() {
                        try {
                            String jsonString1 = HttpUtil.getJsonContent(path1,
                                    "utf-8");
                            demoperson = JsonTool.getPerson("person", jsonString1);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        if (demoperson != null) {
                            sendMessage(1, null);
                        } else {
                            sendMessage(
                                    -2,
                                    getResources().getString(
                                            R.string.network_exception));
                        }
                    }
                }.start();
                break;
    
            case R.id.persons:
                final String path2 = PATH + "?action_flag=persons";
                Toast.makeText(MainActivity.this, "正在下载网络数据,请稍候...",
                        Toast.LENGTH_LONG).show();
                new Thread() {
                    public void run() {
                        try {
                            String jsonString2 = HttpUtil.getJsonContent(path2,
                                    "utf-8");
                            list2 = JsonTool.getPersons("persons", jsonString2);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        if (list2 != null) {
                            sendMessage(2, null);
                        } else {
                            sendMessage(
                                    -2,
                                    getResources().getString(
                                            R.string.network_exception));
                        }
                    }
                }.start();
                break;
            case R.id.listString:
                final String path3 = PATH + "?action_flag=listString";
                Toast.makeText(MainActivity.this, "正在下载网络数据,请稍候...",
                        Toast.LENGTH_LONG).show();
                new Thread() {
                    public void run() {
                        try {
                            String jsonString3 = HttpUtil.getJsonContent(path3,
                                    "utf-8");
                            list3 = JsonTool.getList("listString", jsonString3);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        if (list3 != null) {
                            sendMessage(3, null);
                        } else {
                            sendMessage(
                                    -2,
                                    getResources().getString(
                                            R.string.network_exception));
                        }
                    }
                }.start();
                break;
            case R.id.listMap:
                final String path4 = PATH + "?action_flag=listMap";
                Toast.makeText(MainActivity.this, "正在下载网络数据,请稍候...",
                        Toast.LENGTH_LONG).show();
                new Thread() {
                    public void run() {
                        try {                        
                            String jsonString4 = HttpUtil.getJsonContent(path4, "utf-8");
                            list4 = JsonTool.getListMap("listMap", jsonString4);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        if (list4 != null) {
                            sendMessage(4, null);
                        } else {
                            sendMessage(
                                    -2,
                                    getResources().getString(
                                            R.string.network_exception));
                        }
                    }
                }.start();            
                break;
            }
        }
    
    }
    88888888888888888888888888888
  • 相关阅读:
    2020.12.7
    IDEA修改代码后不用重新启动项目即可刷新
    期中测试人口普查登记题目
    Android去掉标题头
    Android限制输入框内容
    Android:setOnItemClickListener cannot be used with a spinner报错
    Android修改app图标
    将外部sqlite3数据库导入到Android项目中
    IDEA个人常用快捷键
    css选择器
  • 原文地址:https://www.cnblogs.com/lql547485426/p/6754204.html
Copyright © 2020-2023  润新知