{"id":317,"date":"2022-09-30T21:35:51","date_gmt":"2022-09-30T13:35:51","guid":{"rendered":"http:\/\/blog.nhl520.cn\/?p=317"},"modified":"2022-11-11T19:32:32","modified_gmt":"2022-11-11T11:32:32","slug":"android%e5%8a%a0%e8%bd%bd%e5%88%97%e8%a1%a8%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/blog.nhl520.cn\/index.php\/2022\/09\/30\/android%e5%8a%a0%e8%bd%bd%e5%88%97%e8%a1%a8%e6%96%b9%e6%b3%95\/","title":{"rendered":"Android\u52a0\u8f7d\u5217\u8868\u65b9\u6cd5"},"content":{"rendered":"\n<p>\u9700\u8981\u914d\u7f6e\u8054\u7f51\u6743\u9650<\/p>\n\n\n\n<p>\u9700\u8981\u914d\u7f6e\u4e24\u4e2axml\u9875\u9762<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"xml\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    \/\/\u5217\u8868   \n&lt;ListView\n            android:scrollbars=\"none\"\n            android:overScrollMode=\"never\"\n            android:overScrollFooter=\"@android:color\/white\"\n            android:overScrollHeader=\"@android:color\/black\"\n            android:divider=\"@null\"\n            android:id=\"@+id\/listview\"\n            android:layout_width=\"match_parent\"\n            android:layout_height=\"wrap_content\">\n        &lt;\/ListView><\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">    private static final String TAG =\"json\";\n    private List&lt;Map&lt;String, Object>> lists;\n    private SimpleAdapter adapter;\n    private ListView listView;\n\n\n\nnew Thread() {\n            @Override\n            public void run() {\n                \/\/\u521b\u5efaokhtto\u8bf7\u6c42\u7684\u5bf9\u8c61\n                OkHttpClient okHttpClient = new OkHttpClient();\n                \/\/\u521b\u5efa\u8bf7\u6c42\u8fde\u63a5\uff0curl\u91cc\u9762\u5b58\u653e\u8bf7\u6c42\u8fde\u63a5\uff0cget\u8868\u793a\u5176\u5b9eget\u8bf7\u6c42\n                Request request = new Request.Builder().url(\"https:\/\/www.nhl520.cn\/json.php\").get().build();\n\n                try {\n\n                    Response response = okHttpClient.newCall(request).execute();\n                    \/\/\u5b9a\u4e49\u5b57\u7b26\u4e32\u63a5\u6536\u8bf7\u6c42\u4fe1\u606f\n                    String string2 = response.body().string();\n                    System.out.println(\"String:\" + string2);\n\n\n                    JSONObject jsonObjectALL = new JSONObject(string2);\n                    \/\/ \u901a\u8fc7\u6807\u8bc6(person)\uff0c\u83b7\u53d6JSON\u6570\u7ec4\n                    JSONArray jsonArray = jsonObjectALL.getJSONArray(\"person\");\n\n                    \/\/ Log.e(\"\u63d0\u4ea4\u5931\u8d25\", jsonArray+\"\");\n                    lists = new ArrayList&lt;>();\n\n                    for (int i = 0; i &lt; jsonArray.length(); i++) {\n                        \/\/ JSON\u6570\u7ec4\u91cc\u9762\u7684\u5177\u4f53-JSON\u5bf9\u8c61\n                        JSONObject jsonObject = jsonArray.getJSONObject(i);\n                        String name = jsonObject.optString(\"name\", null);\n                        String time = jsonObject.optString(\"time\", null);\n                        String content = jsonObject.optString(\"content\", null);\n                        String imgtx = jsonObject.optString(\"tx\", null);\n                        String imgurl = jsonObject.optString(\"url\", null);\n                        \/\/System.out.println(age);\n\n\n                        Map&lt;String, Object> map = new HashMap&lt;>();\n                        map.put(\"name\", name);\n                        map.put(\"time\", time);\n                        map.put(\"content\", content);\n                        map.put(\"tx\", imgtx);\n                        map.put(\"urlimg\", imgurl);\n                        lists.add(map);\n                       \/\/ lists2.add(imgurl);\n                    }\n\n\n                    \/\/\u9002\u914d\u5668\u6307\u5b9a\u5e94\u7528\u81ea\u5df1\u5b9a\u4e49\u7684xml\u683c\u5f0f\n                    adapter = new SimpleAdapter(getActivity(), lists, R.layout.item, new String[]{\"name\", \"time\", \"content\",\"tx\",\"urlimg\"}, new int[]{R.id.text1, R.id.text2, R.id.text3,R.id.itemtx,R.id.image1});\n                    listView = view.findViewById(R.id.listview);\n\n                    \/\/\u5229\u7528glide\u5728\u5217\u8868\u52a0\u8f7d\u56fe\u7247\u5f00\u59cb\n                    adapter.setViewBinder(new SimpleAdapter.ViewBinder() {\n                        @Override\n                        public boolean setViewValue(View view, Object data, String textRepresentation) {\n                            if (view instanceof ImageView){\n                                ImageView iv= (ImageView)view;\n                                GlideUtil.GlideWithPlaceHolder(getActivity(), data.toString())\n                                        .placeholder(R.mipmap.itemtrue)\/\/\u52a0\u8f7d\u8fc7\u7a0b\u4e2d\u56fe\u7247\u5c55\u793a\n                                        .error(R.mipmap.itemflase)\/\/\u52a0\u8f7d\u5931\u8d25\u56fe\u7247\u5c55\u793a\n                                        .into(iv);\n\n                                return true;\n                            }else\n                                return false;\n                        }\n\n                    });  \/\/\u5229\u7528glide\u5728\u5217\u8868\u52a0\u8f7d\u56fe\u7247\u7ed3\u675f\n\n                    \/\/\u8981\u60f3\u4e0e\u4e3bUI\u4ea4\u4e92\u9700\u8981\u4f7f\u7528runOnUiThread\u8fd9\u4e2a\u65b9\u6cd5\u5b9e\u73b0Runnable\u7684\u62bd\u8c61\u7c7b\uff0c\u8fdb\u884c\u4ea4\u4e92\n                    getActivity().runOnUiThread(new Runnable() {\n                        @Override\n                        public void run() {\n                            listView.setAdapter(adapter);\n                        }\n                    });\n\n\n                } catch (Exception e) {\n                    e.printStackTrace();\n                }\n            }\n        }\n                .start();\n<\/pre>\n\n\n\n<p>\u518d\u914d\u7f6eJava\u7c7b<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public class GlideUtil {\n    public static RequestBuilder&lt;Drawable> GlideWithPlaceHolder(Context context, Object object) {\n        return Glide.with(context).load(object).apply(new RequestOptions().placeholder(R.mipmap.home).dontAnimate());\n        \/\/\u52a0\u8f7d\u56fe\u7247\n    }\n\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9700\u8981\u914d\u7f6e\u8054\u7f51\u6743\u9650 \u9700\u8981\u914d\u7f6e\u4e24\u4e2axml\u9875\u9762 \u518d\u914d\u7f6eJava\u7c7b<\/p>\n","protected":false},"author":1,"featured_media":172,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-317","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android"],"_links":{"self":[{"href":"https:\/\/blog.nhl520.cn\/index.php\/wp-json\/wp\/v2\/posts\/317","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.nhl520.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.nhl520.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.nhl520.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.nhl520.cn\/index.php\/wp-json\/wp\/v2\/comments?post=317"}],"version-history":[{"count":5,"href":"https:\/\/blog.nhl520.cn\/index.php\/wp-json\/wp\/v2\/posts\/317\/revisions"}],"predecessor-version":[{"id":322,"href":"https:\/\/blog.nhl520.cn\/index.php\/wp-json\/wp\/v2\/posts\/317\/revisions\/322"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.nhl520.cn\/index.php\/wp-json\/wp\/v2\/media\/172"}],"wp:attachment":[{"href":"https:\/\/blog.nhl520.cn\/index.php\/wp-json\/wp\/v2\/media?parent=317"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.nhl520.cn\/index.php\/wp-json\/wp\/v2\/categories?post=317"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.nhl520.cn\/index.php\/wp-json\/wp\/v2\/tags?post=317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}