課程
/后端開發(fā)
/Java
/Java入門第二季 升級版
老師啊,能不能把url里面的訪問網(wǎng)址也改成參數(shù)傳入后更新個jar包呀?這個API的訪問參數(shù)有變化了:?
2021-01-24
源自:Java入門第二季 升級版 5-2
正在回答
購買的免費的api和視頻上的一致就不會有這個問題,如果一定要變url的話就把jar包里用到的文件復制出來,再修改url.
朱智琳 提問者
小樓倚月聽微風 回復 朱智琳 提問者
可以新創(chuàng)建一個temp_WeatherUtilsImpl.java文件,來重寫WeatherUtilsImpl類,此處僅以24h查詢?yōu)槔? package?com.imooc.weather; import?com.google.gson.FieldNamingPolicy; import?com.google.gson.Gson; import?com.google.gson.GsonBuilder; import?com.google.gson.reflect.TypeToken; import?com.imooc.weather.impl.WeatherUtilsImpl; import?ognl.Ognl; import?ognl.OgnlContext; import?ognl.OgnlException; import?okhttp3.Call; import?okhttp3.OkHttpClient; import?okhttp3.Request; import?okhttp3.Response; import?java.io.IOException; import?java.util.ArrayList; import?java.util.List; import?java.util.Map; public?class?temp_WeatherUtilsImpl?extends?WeatherUtilsImpl?{ ????//重載或重寫WeatherUtilsImpl.java ????private?<T>?T?getValue(Object?map,?String?path,?Class<T>?clazz)?{ ????????try?{ ????????????OgnlContext?context?=?new?OgnlContext(); ????????????context.setRoot(map); ????????????T?value?=?(T)?Ognl.getValue(path,?context,?context.getRoot()); ????????????return?value; ????????}?catch?(Exception?e)?{ ????????????throw?new?RuntimeException(e); ????????} ????} ????public?List<HourWeather>?w24h(String?appCode,?String?area)?{ ????????List<HourWeather>?resultList?=?new?ArrayList<HourWeather>(); ????????try?{ ????????????//新建查詢請求 ????????????OkHttpClient?client?=?new?OkHttpClient(); ????????????Request?request?=?new?Request.Builder() ????????????????????.get() ????????????????????.url("https://ali-weather.showapi.com/hour24?area="?+?area) ????????????????????.header("Authorization",?"APPCODE?"?+?appCode) ????????????????????.build(); ????????????Call?call?=?client.newCall(request); ????????????Response?response?=?call.execute(); ????????????Gson?gson?=?new?GsonBuilder() ????????????????????.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) ????????????????????.create(); ????????????//獲取服務器返回數(shù)據(jù) ????????????String?resBody?=?response.body().string(); ????????????Map?result?=?gson.fromJson(resBody,?new?TypeToken<Map>()?{ ????????????}.getType()); ????????????//包裝為對象集合 ????????????List<Map<String,?String>>?hourList?=?this.getValue(result,?"showapi_res_body.hourList",?ArrayList.class); ????????????//如果沒有找到對應地區(qū)天氣數(shù)據(jù),返回空的List列表 ????????????if(hourList?==?null?||?hourList.size()?==?0){ ????????????????return?new?ArrayList<HourWeather>(); ????????????} ????????????for?(Map<String,?String>?hour?:?hourList)?{ ????????????????HourWeather?hourWeather?=?new?HourWeather(); ????????????????hourWeather.setYear(hour.get("time").substring(0,?4)); ????????????????hourWeather.setMonth(hour.get("time").substring(4,?6)); ????????????????hourWeather.setDay(hour.get("time").substring(6,?8)); ????????????????hourWeather.setHour(hour.get("time").substring(8,?10)); ????????????????hourWeather.setWindDirection(hour.get("wind_direction")); ????????????????hourWeather.setWindPower(hour.get("wind_power")); ????????????????hourWeather.setWeather(hour.get("weather")); ????????????????hourWeather.setTemperature(hour.get("temperature")); ????????????????resultList.add(hourWeather); ????????????} ????????}?catch?(Exception?e)?{ ????????????throw?new?RuntimeException(e); ????????} ????????return?resultList; ????} ????public?static?void?main(String[]?args)?throws?IOException,?OgnlException?{ ????????List<DayWeather>?list2?=?new?WeatherUtilsImpl().w3d("28a8e0cdea484fdd9cba138a72bc1778",?"DASDSADSA"); ????????System.out.println(list2); ????} } 這是Application.java: package?com.imooc.weather; import?com.imooc.weather.impl.WeatherUtilsImpl; import?java.util.List; import?java.util.Scanner; public?class?Application?{ ????public?static?void?main(String[]?args)?{ ????????System.out.println("查詢最近天氣預報"); ????????System.out.println("輸入1:?查詢未來24小時天氣預報"); ????????System.out.println("輸入2:?查詢未來3天天氣預報"); ????????System.out.println("輸入3:?查詢未來7天天氣預報"); ????????System.out.print("請輸入您的選擇:?"); ????????Scanner?scanner?=?new?Scanner(System.in); ????????int?i?=?scanner.nextInt(); ????????System.out.println("用戶輸入數(shù)字:?"?+?i); ????????if(i?==?1){ ????????????System.out.print("請輸入城市名稱查詢未來24小時天氣預報:?"); ????????????String?city?=?scanner.next(); ????????????temp_WeatherUtilsImpl?weatherUtils?=?new?temp_WeatherUtilsImpl(); ????????????List<HourWeather>?weatherList1?=?weatherUtils.w24h("5f94543d72c44591bf4897e0f3d622ea",?city); ????????????System.out.println(weatherList1); ????????} ????} }
舉報
課程升級!以終為始告別枯燥,在開發(fā)和重構中體會Java面向對象編程的奧妙
1 回答jar包
2 回答新建包,缺不能引用?
1 回答jar包在哪里
1 回答jar包怎么下載
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學習伙伴
掃描二維碼關注慕課網(wǎng)微信公眾號
2021-01-25
購買的免費的api和視頻上的一致就不會有這個問題,如果一定要變url的話就把jar包里用到的文件復制出來,再修改url.
2022-05-08