阳光灿烂的日子

导航

 

从 Grand Valley State University 的课程修改而来

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
final int HTTP_STATUS_OK = 200;
final byte[] buff = new byte[1024];

// -----------------------------------------------------------------------------------------
// 4个编辑框        
final EditText editCity = (EditText) findViewById(R.id.editTextCity);
final EditText editDate = (EditText) findViewById(R.id.editTextDate);
final EditText editTemp = (EditText) findViewById(R.id.editTextTemp);
final EditText editWeather = (EditText) findViewById(R.id.editTextWeather);

// -----------------------------------------------------------------------------------------
// 响应按键
Button button = (Button) findViewById(R.id.buttonShow);
button.setOnClickListener(new OnClickListener() 
{
    public void onClick(View t)
    {
        String result = null;
        String url = "http://m.weather.com.cn/data/101010100.html";
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(url);
        try 
        {
            HttpResponse response = client.execute(request);
            StatusLine status = response.getStatusLine();
            if (status.getStatusCode() != HTTP_STATUS_OK)
                throw new Exception("Invalid response from m.weather.com.cn" + status.toString());
            HttpEntity entity = response.getEntity();
            InputStream ist = entity.getContent();
            ByteArrayOutputStream content = new ByteArrayOutputStream();
            int readCount = 0;
            while ((readCount = ist.read(buff)) != -1)
                content.write(buff, 0, readCount);
            result = new String(content.toByteArray());
        } 
        catch(Exception e) 
        {
            throw new RuntimeException(e);
        }                
        
        try
        {
            JSONObject weatherObject = new JSONObject(result);
            JSONObject weatherInfo = weatherObject.getJSONObject("weatherinfo");
            // -----------------------------------------------------------------------------------------
            // 取得 城市字段
            String city = weatherInfo.getString("city");
            editCity.setText(city);

            // -----------------------------------------------------------------------------------------
            // 取得日期字段
            String date = weatherInfo.getString("date_y");
            editDate.setText(date);

            // -----------------------------------------------------------------------------------------
            // 取得温度字段
            String temp = weatherInfo.getString("temp1");
            editTemp.setText(temp);

            // -----------------------------------------------------------------------------------------
            // 取得天气字段
            String weather = weatherInfo.getString("weather1");
            editWeather.setText(weather);                    

        } 
        catch(JSONException e) 
        {
            throw new RuntimeException(e);
        }
    }
});
// end Listener        

 

 

posted on 2013-01-02 18:34  阳光灿烂的日子  阅读(454)  评论(0)    收藏  举报