天气预报代码(WeatherDemo.java)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

同学的天气预报代码,供参考!

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

{ 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());

}

相关文档
最新文档