天气预报app简易版

天气预报

public class MainActivity extends Activity {



private EditText ed_city;
private TextView city_result1;
private TextView city_result2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ed_city = (EditText) findViewById(R.id.ed_city);
city_result1 = (TextView) findViewById(R.id.city_result1);
city_result2 = (TextView) findViewById(R.id.city_result2);
}

private final static String PATH = "http://wthrcdn.etouch.cn/weather_mini?city=";

protected static final int SUCCESS = 0;
protected static final int INVALID_CITY = 1;
protected static final int ERROR = 2;


private String city;
String ul;

private Handler mHandler = new Handler(){

public void handleMessage(android.os.Message msg) {

switch (msg.what) {
case SUCCESS:

try {
JSONArray data = (JSONArray) msg.obj;
String day01 = data.getString(0);
city_result1.setText(day01);

String day02 = data.getString(1);
city_result2.setText(day02);
} catch (Exception e) {
e.printStackTrace();
}

break;
case INVALID_CITY:
Toast.makeText(MainActivity.this, "城市无效 ...", 0).show();
break;
case ERROR:

Toast.makeText(MainActivity.this, "网络 问题 .... ...", 0).show();
break;


default:
break;
}
};
};

public void searchCityWeather(View v){

city = ed_city.getText().toString().trim();
if(TextUtils.isEmpty(city)){
Toast.makeText(this, "路径错误...", 0).show();
return;
}

//http://wthrcdn.etouch.cn/weather_mini?city=%E6%B7%B1%E5%9C%B3

// 发起请求 给 那个 网站了 
new Thread(){
public void run() {

try {

ul = PATH + URLEncoder.encode(city, "UTF-8");
URL url = new URL(ul);

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

// 设置 必要的参数信息
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");

//判断 响应码 
int code = conn.getResponseCode();

if(code==200){

InputStream in = conn.getInputStream();
String data = StreamTool.decodeStream(in);

System.out.println(data);

//解析json 格式的数据 
JSONObject jsonObj = new JSONObject(data);

// 获得 desc 的值 
String result = jsonObj.getString("desc");
if("OK".equals(result)){

//城市 有效, 返回了需要的数据
JSONObject dataObj = jsonObj.getJSONObject("data");

JSONArray jsonArray = dataObj.getJSONArray("forecast");

//通知 更新 ui 
Message msg = Message.obtain();
msg.obj = jsonArray;
msg.what=SUCCESS;
mHandler.sendMessage(msg);

/* String value1 = jsonArray.getString(0);
String value2 = jsonArray.getString(2);
System.out.println(value1);
System.out.println(value2);*/

}else{

//城市 无效
Message msg = Message.obtain();
msg.what=INVALID_CITY;
mHandler.sendMessage(msg);
}
}

} catch (Exception e) {
e.printStackTrace();

Message msg = Message.obtain();
msg.what=ERROR;
mHandler.sendMessage(msg);
}

};
}.start();

}

}





public class StreamTool {


public static String decodeStream(InputStream in) throws IOException {

// 底层流
ByteArrayOutputStream baos = new ByteArrayOutputStream();

int len =0;
byte[] buf = new byte[1024];

while((len=in.read(buf))>0){
baos.write(buf, 0, len);
}

String data = baos.toString();

return data;
}


}





<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请输入要查询的城市" />
    <EditText 
        android:id="@+id/ed_city"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
    <Button 
        android:onClick="searchCityWeather"
        android:text="查询"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
    
    <TextView
        android:id="@+id/city_result1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />
    <TextView
        android:id="@+id/city_result2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />


</LinearLayout>









<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.itheima.json.weather"
    android:versionCode="1"
    android:versionName="1.0" >


    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.INTERNET"/>


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.itheima.json.weather.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>


</manifest>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值