使用weather.com.cn数据实现的简易天气demo

来源:互联网 发布:python r语言接口 编辑:程序博客网 时间:2024/05/17 08:05

通过网上搜索,找到了中国中央气象局的天气接口网址:http://m.weather.com.cn/data/101010100.html,其中数字部分为城市代码。从网上找到一个sqlite的数据库,存储了省市区的信息和城市代码,一共2556条数据,程序先将数据库copy到程序目录的file下,再读取。连网部分是使用AsyncHttpClient写的。

基本想法是做两个界面,一个界面用于显示从接口返回的数据,一个界面用于选择城市。为了简便,直接将json数据放在textview中显示,不再解析。

main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/container"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="jsj.lgl.weather.MainActivity" >    <Button        android:id="@+id/btn_setting"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_centerHorizontal="true"        android:text="select area" />    <ScrollView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_above="@id/btn_setting" >        <TextView            android:id="@+id/content"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:layout_above="@id/btn_setting" />    </ScrollView></RelativeLayout>


MainActivity.java

package jsj.lgl.weather;import java.io.UnsupportedEncodingException;import org.apache.http.Header;import com.loopj.android.http.AsyncHttpClient;import com.loopj.android.http.AsyncHttpResponseHandler;import com.loopj.android.http.FastjsonHttpResponseHandler;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class MainActivity extends Activity {TextView content;Button btn_setting;AsyncHttpClient httpClient;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);httpClient = new AsyncHttpClient();content = (TextView) findViewById(R.id.content);btn_setting = (Button) findViewById(R.id.btn_setting);btn_setting.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(MainActivity.this, SelectAreaActivity.class);startActivityForResult(intent, 100);}});//getWeatherData("101010100");}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {if(resultCode == Activity.RESULT_OK) {if(requestCode == 100) {String code = data.getExtras().getString("code");getWeatherData(code);}}super.onActivityResult(requestCode, resultCode, data);}private void getWeatherData(String areaId) {String url = "http://m.weather.com.cn/data/"+ areaId +".html";content.setText("loading data");httpClient.get(url, new AsyncHttpResponseHandler() {@Overridepublic void onSuccess(int statusCode, Header[] headers,byte[] responseBody) {super.onSuccess(statusCode, headers, responseBody);String response = null;try {response = responseBody == null ? null : new String(responseBody, getCharset());} catch (UnsupportedEncodingException e) {e.printStackTrace();}content.setText(response);}@Overridepublic void onFailure(int statusCode, Header[] headers,byte[] responseBody, Throwable error) {super.onFailure(statusCode, headers, responseBody, error);String error_text = "error!\nstatusCode=" + statusCode;if(error != null) {error_text += "\nerror_message:" + error.getMessage();}content.setText(error_text);}});}}


select_area.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"     android:gravity="center_horizontal">    <TextView     android:id="@+id/tv_province"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:textSize="30sp"    android:text="select province"    android:background="#f00"    /><TextView     android:id="@+id/tv_city"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:textSize="30sp"    android:layout_marginTop="20dp"    android:text="select city"    android:background="#f00"    /><TextView     android:id="@+id/tv_area"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:textSize="30sp"    android:layout_marginTop="20dp"    android:text="select area"    android:background="#f00"    /><Button     android:id="@+id/btn_ok"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_marginTop="20dp"    android:text="ok"    /></LinearLayout>

SelectAreaActivity.java

package jsj.lgl.weather;import java.util.List;import jsj.lgl.weather.model.Area;import jsj.lgl.weather.model.City;import jsj.lgl.weather.model.Province;import android.app.Activity;import android.app.AlertDialog;import android.app.Dialog;import android.content.DialogInterface;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.TextView;public class SelectAreaActivity extends Activity {TextView tv_province, tv_city, tv_area;List<Province> listProvince;List<City> listCity;List<Area> listArea;Dialog dialogProvince, dialogCity, dialogArea;Button btn_ok;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.select_area);tv_province = (TextView) findViewById(R.id.tv_province);tv_city = (TextView) findViewById(R.id.tv_city);tv_area = (TextView) findViewById(R.id.tv_area);btn_ok = (Button) findViewById(R.id.btn_ok);tv_province.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {showProvinceDialog();}});tv_city.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {showCityDialog();}});tv_area.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {showAreaDialog();}});btn_ok.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {String code = (String) tv_area.getTag();Log.i("code", "code=" + code);setResult(Activity.RESULT_OK, new Intent().putExtra("code", code));SelectAreaActivity.this.finish();}});listProvince = DBUtil.getProvinceWithoutChild(this);}private void showProvinceDialog() {if(dialogProvince == null) {String[] provinces = new String[listProvince.size()];for(int i=0,len=listProvince.size();i<len;i++) {provinces[i] = listProvince.get(i).name;}dialogProvince = new AlertDialog.Builder(this).setTitle("选择省").setIcon(android.R.drawable.ic_dialog_info).setSingleChoiceItems(provinces, -1, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {String id = listProvince.get(which).id;String name = listProvince.get(which).name;tv_province.setText(name);tv_province.setTag(id);dialogProvince.dismiss();listCity = DBUtil.getCityByProvinceId(SelectAreaActivity.this, id);}}).create();}dialogProvince.show();}private void showCityDialog() {if(listCity == null) {return;}String[] citys = new String[listCity.size()];for(int i=0,len=listCity.size();i<len;i++) {citys[i] = listCity.get(i).name;}dialogCity = new AlertDialog.Builder(this).setTitle("选择城市").setIcon(android.R.drawable.ic_dialog_info).setSingleChoiceItems(citys, -1, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {String id = listCity.get(which).id;String name = listCity.get(which).name;tv_city.setText(name);tv_city.setTag(id);dialogCity.dismiss();listArea = DBUtil.getAreaByCityId(SelectAreaActivity.this, id);}}).create();dialogCity.show();}private void showAreaDialog() {if(listArea == null) {return;}String[] areas = new String[listArea.size()];for(int i=0,len=listArea.size();i<len;i++) {areas[i] = listArea.get(i).name;}dialogArea = new AlertDialog.Builder(this).setTitle("选择地区").setIcon(android.R.drawable.ic_dialog_info).setSingleChoiceItems(areas, -1, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {String code = listArea.get(which).code;String name = listArea.get(which).name;tv_area.setText(name);tv_area.setTag(code);dialogArea.dismiss();//listCity = DBUtil.getCityByProvinceId(SelectAreaActivity.this, id);}}).create();dialogArea.show();}}


程序非常简陋,仅为练手。项目下载地址:http://download.csdn.net/detail/lgl1170860350/7482453

程序运行部分截图:




0 0
原创粉丝点击