温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何用聚合数据技术获取数据

发布时间:2021-10-14 09:42:35 来源:亿速云 阅读:116 作者:iii 栏目:编程语言

这篇文章主要讲解了“如何用聚合数据技术获取数据”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“如何用聚合数据技术获取数据”吧!

1、首先你需要注册聚合数据,获取相应的KEY,http://www.juhe.cn

2、得到相应的key后,我们就可以通过代码来获取数据了,具体代码如下:

/**
 * 获取菜谱数据
 */
public class CaiPumMenueActivity extends Activity {
    //配置您申请的KEY
    public static final String APPKEY = "82ca66***********49a93e5";
    String apiAddress = "http://apis.juhe.cn/cook/query?key=" + APPKEY +
            "&menu=午餐&rn=10&pn=3";

    /**
     * 请求菜谱数据
     * @param url:请求 用AsyncTask异步请求
     */
    public void readNet(String url)  //请求菜谱数据
    {
        new AsyncTask<String, String, String>() {
            StringBuilder sb;
            @Override
            protected String doInBackground(String... params) {
  try {
                        URL url = new URL(params[0]);
                        URLConnection connection = url.openConnection();      //建立连接
                        InputStream inputStream = connection.getInputStream();
                        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                        BufferedReader br = new BufferedReader(inputStreamReader);
                        String line;
                        sb = new StringBuilder();
                        while ((line = br.readLine()) != null) {
                            sb.append(line);
                        }
                        br.close();
                        inputStream.close();
                        return sb.toString();
                    } catch (MalformedURLException e) {
                        Toast.makeText(CaiPumMenueActivity.this, "获取数据异常", Toast.LENGTH_LONG).show();
                        e.printStackTrace();
                    } catch (IOException e) {
                        Toast.makeText(CaiPumMenueActivity.this, "数据流异常", Toast.LENGTH_LONG).show();
                        e.printStackTrace();
                    }
                }

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }

            @Override
            protected void onPostExecute(String s) {
                try {
                    if(s.equals(null))
                    {
                        //readjson("zaocan.json");    //从本地读取json数据
                        Toast.makeText(CaiPumMenueActivity.this,"获取资源失败",Toast.LENGTH_SHORT).show();
                    }}

            /**
             * 开始解析json数据
             * @param sb
             * @throws JSONException
             */
            public void startJson(String sb) throws JSONException {
                JSONObject root = new JSONObject(sb);
                if ("Success".equals(root.getString("reason"))) {
                    List<Map<String, Object>> list = null;
                    list = new ArrayList<Map<String, Object>>();
                    Map<String, Object> map = null;
                    result = root.getString("result");
                    JSONObject root1 = new JSONObject(result);
                    //int totalNum = root1.getInt("totalNum");
                    JSONArray jsonArray = root1.getJSONArray("data");//处理json数组
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject tree = jsonArray.getJSONObject(i);
                        id = tree.getInt("id");
                        title = tree.getString("title");
                        tags = tree.getString("tags");
                        imtro = tree.getString("imtro");
                        ingredients = tree.getString("ingredients");
                        burden = tree.getString("burden");

                        JSONArray album = tree.optJSONArray("albums");
                        albums = album.getString(0);                   //图片地址

                        map = new HashMap<String, Object>();
                        map.put("id", id);
                        map.put("title", title); //菜名
                        map.put("imtro", imtro); //简介
                        map.put("albums", albums);//菜谱地址

                        JSONArray steps = tree.getJSONArray("steps");//菜谱步骤
                        for (int j = 0; j < steps.length(); j++) {
                            JSONObject ste = steps.getJSONObject(j);
                            img = ste.getString("img");
                            step = ste.getString("step");
                            map.put("img", img);
                            map.put("step", step);
                            //list.add(map);
                        }
                        list.add(map);
                    }
                    Toast.makeText(CaiPumMenueActivity.this, "获取菜谱失败:" + root.getString("reason")+"}}+"+list.size(), Toast.LENGTH_LONG).show();
                    /**
                     * 设置适配器并将 list集合传过去
                     */
                    listView = (ListView) findViewById(R.id.caipu_lv);
                    listView.setAdapter(new JsonAdapter(CaiPumMenueActivity.this, list,listView));
                } else {
                    Toast.makeText(CaiPumMenueActivity.this, "获取菜谱失败" + root.getString("reason"), Toast.LENGTH_LONG).show();
                }
            }
        }.execute(url);
    }

}

JSON返回示例:

{
    "resultcode": "200",
    "reason": "Success",
    "result": {
        "data": [{
            "id": "45",
            "title": "秘制红烧肉",
            "tags": "家常菜;热菜;烧;煎;炖;红烧;炒锅",
            "imtro": "做红烧肉的豆亲们很多,大家对红烧肉的热爱更不用我说,从名字上就能反映出来。一些高手们对红烧肉的认识更是令我佩服,单单就红烧肉的做法、菜谱都看得我是眼花缭乱,口水横流。单纯的红烧肉我平时还真没做过,再不抓紧时间做一回解解馋,不是对不起别人,而是太对不起我自己了! 这道菜的菜名用了秘制二字来形容,当然是自己根据自己多年吃货的经验想象出来的,我不介意把自己的做法与大家共享,只为大家能同我一样,吃到不同口味的红烧肉。不同的人们根据自己的习惯都有不同的做法,味道也不尽相同。我的秘制的关键就是必须用玫瑰腐乳、冰糖和米醋这三种食材,腐乳和冰糖可以使烧出来的肉色泽红亮,米醋能解腻,令肥肉肥而不腻,此法烧制的红烧肉软糯中略带咸甜,的确回味无穷!",
            "ingredients": "五花肉,500g",
            "burden": "玫瑰腐乳,适量;盐,适量;八角,适量;草果,适量;香叶,适量;料酒,适量;米醋,适量;生姜,适量",
            "albums": ["http:\/\/img.juhe.cn\/cookbook\/t\/0\/45_854851.jpg"],
            "steps": [{
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/45_0824e37faf00b71e.jpg",
                "step": "1.将五花肉煮至断生状态"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/45_b6d7329b703f6e85.jpg",
                "step": "2.切成大小一致的块"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/45_6ee9e8dab0516333.jpg",
                "step": "3.放在锅内煎"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/45_b9afd6d4dd81f55c.jpg",
                "step": "4.入生姜"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/45_d0170fbe236421f9.jpg",
                "step": "5.放八角草果各一个,香叶一片"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/45_639b12210745fa41.jpg",
                "step": "6.放冰糖"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/45_c25e0cedd2012f45.jpg",
                "step": "7.加料酒"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/45_eb68327980f022dd.jpg",
                "step": "8.加玫瑰腐乳和腐乳汁及适量盐"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/45_ac17263a11507a41.jpg",
                "step": "9.加米醋"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/45_f5489af5d12b4930.jpg",
                "step": "10.加水继续炖"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/45_8e0cf83cb7306281.jpg",
                "step": "11.直至肉变软糯汤汁收干即可"
            }]
        },
        {
            "id": "52",
            "title": "经典红烧肉",
            "tags": "家常菜;咸;半小时-1小时;孕妇;青少年;老人;白领;晚餐;红烧;营养;增强抵抗力;全菜系;1-2人;待客菜;锅子",
            "imtro": "红烧肉是热菜菜谱之一,以五花肉为制作主料,红烧肉的做法各地也会稍有不同。南方习惯用酱油(老抽)调色,而北方则偏爱炒糖色儿,红烧肉也是我拿手菜之一,不管是逢年过节还是亲朋聚会都不会少了诱人的红烧肉。红烧肉的特点;浓油赤酱,肥而不腻,入口酥软即化。",
            "ingredients": "五花肉,1000g",
            "burden": "葱,适量;姜,适量;蒜,适量;八角,2粒;桂皮,1块;干辣椒,1个;酱油,适量;冰糖,适量;盐,适量;料酒,适量",
            "albums": ["http:\/\/img.juhe.cn\/cookbook\/t\/1\/52_759155.jpg"],
            "steps": [{
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/52_b02a6aa276e3352f.jpg",
                "step": "1.五花肉洗净切成大块。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/52_0cfde36e8061dd9c.jpg",
                "step": "2.冷水把肉下锅烧开,把肉焯水。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/52_4bbe9a903c8b144e.jpg",
                "step": "3.准备好 八角 桂皮 葱姜蒜 辣椒。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/52_887688f6d7716106.jpg",
                "step": "4.把锅烧热,下入焯水的五花肉,小火煸炒至五花肉出油。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/52_72ddd29b5fba09c3.jpg",
                "step": "5.下入葱姜蒜 八角 桂皮 辣椒继续煸炒至肉色微黄。盛出。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/52_90765e3bf8a33c53.jpg",
                "step": "6.锅中留底油放入冰糖小火熬至起沫成棕红色。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/52_95ddaa107b65158b.jpg",
                "step": "7.倒入煸炒的五花肉翻炒均匀,使每块肉均匀沾满糖色,"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/52_68a1aa355f2c62e9.jpg",
                "step": "8.加入料酒、酱油"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/52_b4391bab5b4c70b7.jpg",
                "step": "9.加入开水没过肉,盖上锅盖开火煮沸,调小火炖40分钟,"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/52_22e562a234ed1891.jpg",
                "step": "10.烧至肉熟烂、把汤汁收浓即可。"
            }]
        },
        {
            "id": "92",
            "title": "红烧肉",
            "tags": "家常菜;快手菜",
            "imtro": "红烧肉,各地各家的做法稍有不同味道即大不一样,中餐的精妙在于食材随意性而产生的变化,南方习惯用酱油(老抽)调色,而北方则偏爱炒糖色。 女人不要为了保持好身材,刻意与美味的肉类食品绝缘哦。其实,很多科学证明,适当的吃肉并不会增加额外的脂肪。 猪肉经过小火煸炒出油后,炖出来的红炒肉是肥而不腻,软烂入味,非常的好吃美味,也非常适合咱们的健康理念,即少油,又解馋,又解腻,下面,为你推荐这种红烧肉的经典做法,不用一滴油就可以做出美味健康的红烧肉。",
            "ingredients": "猪后臀尖,1000g",
            "burden": "葱段,适量;八角,2个;干辣椒,4个;香叶,4片;桂皮,1块;鲜姜,1块;干山楂片,4片;黄油,适量;老抽,适量;生抽,适量;白糖,适量;开水,适量",
            "albums": ["http:\/\/img.juhe.cn\/cookbook\/t\/1\/92_512827.jpg"],
            "steps": [{
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_472370e29b980b31.jpg",
                "step": "1.猪肉清洗干净切成方块,冷水下锅,水开后撇去上面的浮沫。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_04f90c14513aa139.jpg",
                "step": "2.焯好的肉块捞出,用温水清洗干净。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_50af1fae3911794d.jpg",
                "step": "3.准备好调料:干辣椒,桂皮,香叶,八角,(大蒜也可以不放)。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_12f839ee0920c03b.jpg",
                "step": "4.做锅开小火,不放油,把肉块放入反复煸炒,煸炒至肉块有点焦香,有油渗出,关火把肉块捞出。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_bfdc35cde7da929b.jpg",
                "step": "5.做锅,用肉块煸出的猪油烧热,放入八角,干辣椒,桂皮,香叶煸炒出香味。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_e21dddc1ffcad36b.jpg",
                "step": "6.放入肉块煸炒1-2分钟后捞出备用。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_89ba401801a762fe.jpg",
                "step": "7.用锅中剩下的余油放入2勺白糖(这个量可以根据自己的喜好添加,喜欢吃甜一点的就多放点糖),开小火煸炒白糖。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_33f84ea85dcd11c9.jpg",
                "step": "8.这是糖色变化的过程"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_bbf69240b5f7211d.jpg",
                "step": "9.当糖起小泡,颜色变红就可以了。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_114bbdd5ef3d1255.jpg",
                "step": "10.这时快速烹上1勺热水,加入肉块煸炒至肉块颜色变红。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_831b33673ac8c507.jpg",
                "step": "11.煸炒至猪肉块上色后,加入适量生抽,老抽(不可多放,放多了颜色就发黑了),黄酒煸炒。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_a377c03180cb6b26.jpg",
                "step": "12.加入开水(多放点水,最好一次加足,肉炖熟了可以大火收汤,水少了,中途加水就影响肉质的口感了),再把姜块,葱段,山楂片放入。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_127b81298ad6cf93.jpg",
                "step": "13.盖上锅盖,大火烧开,小火炖煮1个小时。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_59ebcf1a60e54619.jpg",
                "step": "14.炖煮了一个小时了,汤汁也差不多剩一半了,这时加入6克盐(盐可以根据自己的口味添加,在还有汤的情况下,不要尝出咸味正好,等汤汁收浓就会咸了),小火继续炖煮。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_f4cbb0c04220428e.jpg",
                "step": "15.当品尝到肉质软烂,就可以大火收汁,一锅肥而不腻,肉质酥烂,诱人食欲的红烧肉就炖好了,赶快就着小酒,大快朵颐的享受吧"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_372112ff28389c98.jpg",
                "step": "16.炎热的夏季,吃一顿少油,解腻,又美味的红烧肉大餐。"
            },
            {
                "img": "http:\/\/img.juhe.cn\/cookbook\/s\/1\/92_1630281ff0350105.jpg",
                "step": "17.再来一张,想吃吗,那就赶紧动手吧,呵呵。"
            }]
        }],
        "totalNum": "306",
        "pn": "1",
        "rn": "3"
    },
    "error_code": 0
}

4、通过网络获取图片

 /**
     * //创建新的子线程
     * 获取图片
     */
    public void ImgThread(final String img) {
        new Thread() {
            private HttpURLConnection conn;
            private Bitmap bitmap;

            @Override
            public void run() {
                super.run();
                try {
                    URL url = new URL(img);
                    conn = (HttpURLConnection) url.openConnection();
                    conn.setConnectTimeout(5000);
                    conn.setRequestMethod("GET");
                    int code = conn.getResponseCode();
                    if (code == 200) {
                        InputStream is = conn.getInputStream();
                        bitmap = BitmapFactory.decodeStream(is);
                        Message msg = new Message();
                        msg.what = 1;
                        msg.obj = bitmap;
                        handler.sendMessage(msg);
                    } else {
                        Message msg = new Message();
                        msg.what = 2;
                        handler.sendMessage(msg);
                    }

                } catch (MalformedURLException e) {
                    Message msg = new Message();
                    msg.what = 2;
                    handler.sendMessage(msg);
                    e.printStackTrace();
                } catch (IOException e) {
                    Message msg = new Message();
                    msg.what = 2;
                    handler.sendMessage(msg);
                    e.printStackTrace();
                }
            }
        }.start();
    }

感谢各位的阅读,以上就是“如何用聚合数据技术获取数据”的内容了,经过本文的学习后,相信大家对如何用聚合数据技术获取数据这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI