Android imports External Database parsing json to get weather forecast

Source: Internet
Author: User

Running interface: 1. after modifying the previous code, you do not need to import the database to the/data/package name folder, directly put in the/res/raw folder without binary compression [java] public static SQLiteDatabase openDatabase (Context context) {try {// Context context Context = new TestActivity (); string databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME; File dir = new File (DATABASE_PATH); if (! Dir. exists () // if the folder does not exist, create the folder {dir. mkdir (); System. out. println ("File build success");} if (! (New File (databaseFilename )). exists () {// if the file does not exist, create the file InputStream is = context. getResources (). openRawResource (R. raw. chinacity); FileOutputStream fos = new FileOutputStream (databaseFilename); byte [] buffer = new byte [8192]; int count = 0; while (count = is. read (buffer)> 0) {fos. write (buffer, 0, count);} fos. close (); is. close ();} db = SQLiteDatabase. openOrCreateDatabase (databaseFilename, nul L);} catch (Exception e) {e. printStackTrace ();} if (db! = Null) {System. out. println ("db build success! ");} Else {System. out. println (" db build failed! ");} Return db;} Get the database: [java] JSON mJson = new JSON (MainActivity. this); [java] db = DBUtils. openDatabase (mContext); 2. connection Network: [java] private static String getConnection (String path) throws MalformedURLException, IOException, ProtocolException {URL url = new URL (path); try {HttpURLConnection cn = (HttpURLConnection) url. openConnection (); cn. setConnectTimeout (5*1000); cn. setRequestMethod ("GET "); // System. out. println ("stream =======" + cn. getInputStream (); InputStreamReader in = new InputStreamReader (cn. getInputStream (); // System. out. println ("in ======" + in); // stream application and read BufferedReader bu = new BufferedReader (in); String line = bu. readLine (). toString (); System. out. println ("stream data line =======" + line); bu. close (); in. close (); return line;} catch (Exception e) {// TODO Auto-generated catch Block e. printStackTrace (); System. out. println ("query failed. Check the network... "); return null;} Note: I have tried to connect all Huawei mobile phones, but other htl and ZTE mobile phones can. I don't know why. 3. obtain the data in json format and use the watermark (formatted) for apt access from the Central Meteorological Observatory: {"weatherinfo": {"city": "Beijing", "cityid": "101010100 ", "temp": "-1", "WD": "North Wind", "WS": "Level 3", "SD": "18%", "WSE ": "3", "time": "15:10", "isRadar": "1", "Radar": "JC_RADAR_AZ9010_JB "}} This is a JSON data format and will be parsed to obtain the information. The required fields are shown in Table 1.1.1 1: field name code meaning city name cityid city id temp real-time temperature WD wind direction WS wind level SD humidity time weather forecast release time table 1.1.1 region 1 keyword segment and its meaning 1 the most key field is cityid, this field will be used as a unique identifier to parse the weather, and the api: Weather (formatted): {"weatherinfo": {"city": "Beijing", "city_en ": "beijing", "date_y": "January 8, 2013", "date": "", "week": "Tuesday", "fchh": "11", "cityid ": 10101 0100 "," temp1 ":" 0 ℃ ~ -11 ℃ "," temp2 ":" 0 ℃ ~ -10 ℃ "," temp3 ":" 0 ℃ ~ -7 ℃ "," temp4 ":" 2 ℃ ~ -7 ℃ "," temp5 ":" 1 ℃ ~ -6 ℃ "," temp6 ":" 0 ℃ ~ -6 ℃ "," tempF1 ":" 32 Gbit/s ~ 12.2 bytes "," tempF2 ":" 32 bytes ~ 14 bytes "," tempF3 ":" 32 bytes ~ 19.4 bytes "," tempF4 ":" 35.6 bytes ~ 19.4 bytes "," tempF5 ":" 33.8 bytes ~ 21.2 bytes "," tempF6 ":" 32 bytes ~ 21.2 Tib "," weather1 ":" clear "," Weather ":" clear "," weather3 ":" Cloudy to overcast "," weather4 ":" Cloudy to sunny ", "weather5": "clear to cloudy", "weather6": "Cloudy to clear", "img1": "0", "img2": "99", "img3 ": "0", "img4": "99", "img5": "1", "img6": "2", "img7": "1", "img8 ": "0", "img9": "0", "img10": "1", "img11": "1", "img12": "0", "img_single ": "0", "img_title1": "clear", "img_title2": "clear", "img_title3": "clear", "img_title4": "clear", "img_title5 ": "Cloudy", "img_title6": "Overcast", "img_titl E7 ":" Multi-cloud "," img_title8 ":" clear "," img_title9 ":" clear "," img_title10 ":" Multi-cloud "," img_title11 ":" Multi-cloud ", "img_title12": "clear", "img_title_single": "clear", "wind1": "Breeze", "wind2": "Breeze", "wind3": "Breeze ", "wind4": "Breeze", "wind5": "Breeze", "wind6": "Breeze", "fx1": "Breeze", "fx2": "Breeze ", "fl1": "less than 3", "fl2": "less than 3", "fl3": "less than 3", "fl4": "less than 3 ", "fl5": "less than 3", "fl6": "less than 3", "index": "cold", "index_d": "cold weather, we recommend that you wear thick down jackets, fur coats, thick sweaters, and other winter clothes. .", "Index48": "cold", "index48_d": "The weather is cold. We recommend that you wear thick down jackets, fur coats, thick sweaters, and other winter clothes. .", "Index_uv": "weak", "index48_uv": "weak", "index_xc": "suitable", "index_tr": "More suitable", "index_co ": "Not Comfortable", "st1": "0", "st2": "-10", "st3": "0", "st4": "-7 ", "st5": "0", "st6": "-7", "index_cl": "applicable", "index_ls": "applicable", "index_ag ": "Not easy to use"} The following table lists the key fields used: 1.1.1-2: field name code meaning city name cityid city id day_y current time temp1 today's minimum temperature the highest temperature temp5 the fifth day the lowest temperature the highest temperature temp6 the sixth day the lowest temperature the highest temperature weather1 today day Gas description img1 today daytime temperature icon img3 tomorrow daytime temperature icon img5 third day daytime temperature icon img7 fourth day daytime temperature icon img9 fifth day daytime temperature icon img11 sixth day daytime temperature icon table 1.1.1 limit 2 keyword field and corresponding meaning 2 Note the temperature coordinate field (img1, img3, img5, img7, img9, img11) only gives the icon number. You Need To concatenate a string to obtain the network image. For example, if img1 is obtained, the image address is the signature code: [java] public static List <Map <String, String> gettodd AyWeather (String path) {List <Map <String, String> list = new ArrayList <Map <String, String> (); String json = null; Map <String, string> map; try {String line = getConnection (path); // convert the character array to a String if (line! = Null) {json = new String (line); // JSONObject item1 = new JSONObject (json) in the form of an object ); // get the object JSONObject item = item1.getJSONObject ("weatherinfo"); // city name, cloudy day, 3-4 °, North Wind Level 3, humidity, date String city = item. getString ("city"); String temp = item. getString ("temp"); String wind = item. getString ("WD") + item. getString ("WS"); String wet = item. getString ("SD"); String time = item. getString ("time"); // Add to MAP Map = new HashMap <String, String> (); map. put ("city", city); map. put ("temp", temp); map. put ("wind", wind); map. put ("wet", wet); map. put ("time", time); list. add (map);} else {System. out. println ("failed to get the reserved data! ") ;}} Catch (MalformedURLException e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (ProtocolException e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (JSONException e) {// TODO Auto-generated catch block e. printStackTrace ();} return list ;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.