您好,登錄后才能下訂單哦!
這篇文章主要講解了“如何用聚合數據技術獲取數據”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“如何用聚合數據技術獲取數據”吧!
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(); }
感謝各位的閱讀,以上就是“如何用聚合數據技術獲取數據”的內容了,經過本文的學習后,相信大家對如何用聚合數據技術獲取數據這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。