111,076
社区成员




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout1"
android:layout_marginBottom="5dip"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dip"
android:layout_marginTop="5dip">
<TextView
android:id="@+id/LblCity"
android:text="@string/PressCity"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<EditText
android:id="@+id/TxtWeather"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:lines="1" />
</LinearLayout>
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.IO;
namespace AndroidHelloWorld
{
[Activity(Label = "EasyWeather", MainLauncher = true, Icon = "@drawable/icon")]
public class Activity1 : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
Button button = FindViewById<Button>(Resource.Id.MyButton);
EditText txtWeather = FindViewById<EditText>(Resource.Id.TxtWeather);
// 天气查询结果
TextView lblCity = FindViewById<TextView>(Resource.Id.LblCityRst); // 城市
TextView lblCurTmp = FindViewById<TextView>(Resource.Id.LabCurTempRst); // 当前温度
TextView lblWeather = FindViewById<TextView>(Resource.Id.LabWeatherRst); // 天气
TextView lblRange = FindViewById<TextView>(Resource.Id.LabRangeRst); // 范围
TextView lblUptTime = FindViewById<TextView>(Resource.Id.LabUptTimeRst); // 更新时间
button.Click += (sender, e) => {
HttpHelper helper = new HttpHelper();
string sUrl = String.Format(@"http://cgi.appx.qq.com/cgi/qqweb/weather/wth/weather.do?
retype=1&city={0}&t={1}",
Uri.EscapeDataString(txtWeather.Text),
DateTime.Now.ToFileTime().ToString());
try
{
var v = helper.HttpGetRequest(sUrl, null, 10000, null);
var rst = new StreamReader(v.GetResponseStream(), System.Text.Encoding.GetEncoding(v.CharacterSet));
var vWeather = Newtonsoft.Json.JsonConvert.DeserializeObject<EtWeather>(rst.ReadToEnd());
//var vWeather = jss.Deserialize<EtWeather>(rst.ReadToEnd());
lblCity.Text = vWeather.city;
lblCurTmp.Text = vWeather.now_temperature;
lblWeather.Text = vWeather.now_pic_name;
lblRange.Text = vWeather.temperature_range;
lblUptTime.Text = vWeather.update_time;
}
catch (Exception Err)
{
var builder = new AlertDialog.Builder(this);
builder.SetMessage(Err.Message);
builder.SetCancelable(false);
builder.SetPositiveButton("OK", delegate { });
var dialog = builder.Create();
dialog.Show();
}
};
}
}
}
TextView lblCity = FindViewById<TextView>(Resource.Id.LblCityRst);
public class HttpHelper
{
private readonly string DefaultUserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";
public HttpWebResponse HttpGetRequest(string url, string referer, int? timeout, CookieCollection cookies)
{
HttpWebRequest request = null;
if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
request = WebRequest.Create(url) as HttpWebRequest;
request.ProtocolVersion = HttpVersion.Version10;
}
else
request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "GET";
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = DefaultUserAgent;
request.CookieContainer = new System.Net.CookieContainer();
if (!string.IsNullOrEmpty(referer))
request.Referer = referer;
if (timeout.HasValue)
request.Timeout = timeout.Value;
else
request.Timeout = 25000;
if (cookies != null)
{
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(cookies);
}
return request.GetResponse() as HttpWebResponse;
}
private bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true;
}