<span style="font-size:18px;"> </span>
<span style="font-size:18px;"><img src="https://img-blog.csdn.net/20160402225506808?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /> </span>
<span style="font-size:18px;">package com.imooc.admin.todayhistory; import android.app.Activity; import android.app.AlertDialog; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.DatePicker; import android.widget.ImageView; import android.widget.ListView; import android.widget.SimpleAdapter; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; import java.util.Calendar; import java.util.HashMap; import java.util.List; import java.util.Map; public class MainActivity extends Activity { public static final int SHOW_RESPONSE = 0; private Button button_get; private Button button_select; private ListView show; final Calendar c = Calendar.getInstance(); int mainYear = c.get(Calendar.YEAR); int mainMonth = c.get(Calendar.MONTH);//获取当前的月份 int mainday = c.get(Calendar.DAY_OF_MONTH);//获取当前月份的日期号码 ; int year = 0; int month = 0; int day = 0; int[] dateGet; AlertDialog dialog; //建立这个handler 用来发送和接收 private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { //对接收到的message进行分类 switch (msg.what) { case SHOW_RESPONSE: SimpleAdapter simpleAdapter = (SimpleAdapter) msg.obj; show.setAdapter(simpleAdapter); //ViewBinder该类可以帮助SimpleAdapter加载图片(如:Bitmap,Drawable) simpleAdapter.setViewBinder(new SimpleAdapter.ViewBinder() { @Override public boolean setViewValue(View view, Object data, String textRepresentation) { //判断如果这个view(这里具体指imageView)是否为imageView 而且 传入的数据是否是bitmap类型 if (view instanceof ImageView && data instanceof Bitmap) { //如果是 那么获得这个View ImageView iv = (ImageView) view; //为view 添加bitmap图片 iv.setImageBitmap((Bitmap) data); return true; } else { return false; } } }); break; case 12345: //传回来的message.obj为数组类型 包含了月和日 int[] mainDate = (int[]) msg.obj; month = mainDate[0]; day = mainDate[1]; senRequestWithHttpConnection(); break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); show = (ListView) findViewById(R.id.listView); button_select = (Button) findViewById(R.id.button_select); //选择日期按钮点击事件 button_select.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //点击以后弹出Dialog 然后在Dialog中装入了一个view view中包含的是一个datePicker final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("请选择时间"); LayoutInflater inflater = LayoutInflater.from(MainActivity.this); View view = View.inflate(MainActivity.this, R.layout.date, null); final DatePicker dp = (DatePicker) view.findViewById(R.id.datePicker); /* datePicker的的触发事件应该是 时间set事件 init()方法 参数: 1.dataPicker的默认年 2.默认月 3.默认日 4.new一个事件set事件 */ dp.init(mainYear, mainMonth, mainday, new DatePicker.OnDateChangedListener() { @Override public void onDateChanged(DatePicker view, int arg1, int arg2, int arg3) { /* 以上参数分别为 选中的view 选中的时间 选中的月 选中的日 ps: 这里选中的年月日中月份是从0开始 比实际要选中的月份要小1 所以在传出这个月份的时候 加1 */ int month = arg2 + 1; int day = arg3; dateGet = new int[]{month, day}; //新建message 并设置what obj 属性 Message msg_date = new Message(); msg_date.what = 12345; msg_date.obj = dateGet; //发送消息 handler.sendMessage(msg_date); //这里设置为点击以后dialog关闭 MainActivity.this.dialog.dismiss(); } }); builder.setView(view); dialog = builder.show(); } }); } private void senRequestWithHttpConnection() { //开启线程 new Thread(new Runnable() { @Override public void run() { HttpURLConnection connection = null; try { URL url = new URL("http://api.juheapi.com/japi/toh?key=b468fc4c9adc2ca464abeb8bffc501b7&v=1.0&month=" + month + "&day=" + day); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(8000); connection.setReadTimeout(8000); InputStream in = connection.getInputStream(); //对获取到的输入流进行读取 BufferedReader reader = new BufferedReader(new InputStreamReader(in)); StringBuilder response = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { response.append(line); } Message message = new Message(); message.what = SHOW_RESPONSE; //服务器结果放在Message中 List<Map<String, Object>> b = parseJOSNWthJONSObject(response.toString()); SimpleAdapter simpleAdapter = getSimpleAdapter(b); message.obj = simpleAdapter; handler.sendMessage(message); } catch (Exception e) { e.printStackTrace(); } finally { if (connection != null) { connection.disconnect(); } } } }).start(); } private SimpleAdapter getSimpleAdapter(List<Map<String, Object>> b) { /*使用SimpleAdapter作为数据适配器 参数: 1.上下文; 2.传入的list 一般是list<extends? Map<?,?>>类型 3.需要加载的布局文件item 4.HashMap中的key String组成的字符串数组 5.从Map中由key解析出的具体内容对应在布局上的控件的ID int[]数组 */ SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, b, R.layout.simple_item, new String[]{"title", "date", "pic", "des"}, new int[]{R.id.title_show, R.id.date_show, R.id.image_show, R.id.des_show}); return adapter; } private List<Map<String, Object>> parseJOSNWthJONSObject(String josnDate) { List<Map<String, Object>> a = new ArrayList<Map<String, Object>>(); Bitmap bitmapWeNeed = null; try { JSONObject result = new JSONObject(josnDate); String resultDate = result.getString("result"); JSONArray array = new JSONArray(resultDate); for (int i = 0; i < array.length(); i++) { JSONObject object = array.getJSONObject(i); //{"_id":"11201101", // "day":1, // "des":"在896年前的今天,1120年11月1日 (农历十月初九),北宋江南方腊起义。", // "lunar":"", // "month":11, // "pic":"http://juheimg.oss-cn-hangzhou.aliyuncs.com/toh/201109/18/7D2278978.jpg", // "title":"北宋江南方腊起义", // "year":1120} String _id = object.getString("_id"); int day = object.getInt("day"); String des = object.getString("des"); String lunar = object.getString("lunar"); int month = object.getInt("month"); String pic = object.getString("pic"); String title = object.getString("title"); int year = object.getInt("year"); bitmapWeNeed = weGetBitmap(pic); Map<String, Object> map = new HashMap<String, Object>(); map.put("title", title); map.put("date", year + "年" + month + "月" + day + "日"); map.put("pic", bitmapWeNeed); map.put("des", des); a.add(map); } } catch (JSONException e) { e.printStackTrace(); } return a; } private Bitmap weGetBitmap(String url) { Bitmap bitmap = null; try { URL urlBitmap = new URL(url); InputStream is = urlBitmap.openStream(); bitmap = BitmapFactory.decodeStream(is); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return bitmap; } } </span>
一个小玩意儿 第一App 历史的今天 简单的API借口数据获取
最新推荐文章于 2022-12-09 14:44:18 发布