通过网上搜索,找到了中国中央气象局的天气接口网址: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>
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