当前位置:文档之家› 天气预报代码(WeatherDemo.java)

天气预报代码(WeatherDemo.java)

同学的天气预报代码,供参考!package com.example.baidumap;import java.io.BufferedInputStream;import java.io.IOException;import java.io.InputStream;import .URL;import .URLConnection;import org.apache.http.HttpResponse;import org.apache.http.HttpStatus;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.util.EntityUtils;import org.json.JSONException;import org.json.JSONObject;import android.content.Context;import android.content.res.Resources;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.util.Log;publicclass WeatherDemo{privatestatic Resources resources;private JSONObject cityAndcode;private String weatherPicUrl1;privateboolean update=false;WeatherDemo(Context context){Log.i("Weather","create");System.out.println("create");cityAndcode = new JSONObject();resources = context.getResources();try {cityAndcode = loadLocalJson();} catch (JSONException e) {e.printStackTrace();}}public String getWeather(String city){String result="";try {Log.i("weather","cityAndcode.getString(city)"+cityAndcode.getString(city));String jsonUrl=new String("/data/"+cityAndcode.get(city)+".html");String strResult =getJson(jsonUrl);Log.i("weather",strResult);String message = getInfo(strResult);result = message;update =true;} catch (JSONException e) {result = "璇锋寜鏍煎紡杈撳叆鍩庡競鍚嶇О锛氬 锛堟俯宸烇級";update=false;}return result;}publicboolean weUpdate(){return update;}public JSONObjectloadLocalJson() throws JSONException{cityAndcode=new JSONObject();InputStreaminputStream = null;try {inputStream = resources.openRawResource(R.raw.cityandcode);byte[] reader = newbyte[inputStream.available()];while (inputStream.read(reader) != -1) {String[] cAc = new String(reader,"gb2312").split("\r\n");for(int i=0;i<cAc.length;i++){ String [] sub_cAc = cAc[i].split(":");sub_cAc[1] = sub_cAc[1].substring(0, sub_cAc[1].length()-1);cityAndcode.put(sub_cAc[0],sub_cAc[1]);Log.i("city","cityname:"+sub_cAc[0]+",citycode:"+cityAndcode.getString(sub _cAc[0]));}}} catch (IOException e) {Log.i("city", e.getMessage(), e);}return cityAndcode;}public StringgetJson(String jsonUrl){HttpGethttpRequest = new HttpGet(jsonUrl);String strResult = "";try {HttpClienthttpClient = new DefaultHttpClient();HttpResponsehttpResponse = httpClient.execute(httpRequest);if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { strResult = EntityUtils.toString(httpResponse.getEntity());}} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}Log.i("H", strResult);return strResult;}public StringgetInfo(String strResult){StringBuildersb = new StringBuilder();String str=null;try {JSONObjectjsonObj = new JSONObject(strResult).getJSONObject("weatherinfo");str = jsonObj.getString("city");sb.append("鍩庡競:"+str+'\n');str= jsonObj.getString("date_y");sb.append("鏃ユ湡:"+str+"\n");str=jsonObj.getString("weather1");sb.append("澶╂皵:"+str+"\n");// str= jsonObj.getString("week");// sb.append("鏄熸湡锛�+str+"\n");str = jsonObj.getString("temp1");sb.append("浠婃棩娓╁害:"+str+"\n");// str=jsonObj.getString("tempF1");// sb.append("鍗庢皬娓╁害:"+str+"\n");// str=jsonObj.getString("temp2");// sb.append("鏄庢棩娓╁害:"+str+"\n");str=jsonObj.getString("wind1");sb.append("椋庡姏:"+str+"\n");// str=jsonObj.getString("index");// sb.append("浠婃棩寤鸿 :"+str+"\n");sb.append("浠婃棩寤鸿 :");str=jsonObj.getString("index_d");String[] strs=str.split("");int length=0;for(int i=0;i<strs.length;i++){ length = strs[i].length()+length;if(length>10){sb.append("\n");length=0;}else{sb.append(strs[i]);}}// str=jsonObj.getString("index48");// sb.append("48灏忔椂寤鸿 :"+str+"\t");// str=jsonObj.getString("index48_d");// s b.append(str+"\n");// str=jsonObj.getString("index_xc");// sb.append("娲楄溅鎸囨暟:"+str+"\n");str=jsonObj.getString("index_tr");sb.append("鏃呮父鎸囨暟:"+str+"\n");str=jsonObj.getString("index_co");sb.append("鏁f 鎸囨暟:"+str+"\n");weatherPicUrl1 =jsonObj.getString("img1");// sb.append("鍥剧墖鍦板潃:"+weatherPicUrl1+"\n");setWeatherPicUrl(weatherPicUrl1);} catch (JSONException e) {Log.i("Erorr","Json parse error");e.printStackTrace();}return sb.toString();}privatevoid setWeatherPicUrl(String url){this.weatherPicUrl1=url;}public Bitmap getBimap(){Bitmap bm=null;try{URL iconurl =new URL("/img/b"+weatherPicUrl1+".gif");URLConnection conn = iconurl.openConnection();conn.connect();InputStream is =conn.getInputStream();BufferedInputStreambis = new BufferedInputStream(is, 8192); bm = BitmapFactory.decodeStream(bis);bis.close();is.close();}catch(Exception e){Log.i("img","getBimap error");}return bm;}}。

相关主题