安卓编程设计很多方面,非常复杂,需要系统的学习才可以,这里以一个简单的天气预报app编程为例:
public class WebServiceUtil
{
// 定义Web Service的命名空间
static final String SERVICE_NS = "";
// 定义Web Service提供服务的URL
static final String SERVICE_URL = "";
public static List getProvinceList()
{
// 需要调用的 *** 名(获得本天气预报Web Services支持的洲、国内外省份和城市信息)
String methodName = "getRegionProvince";
// 创建HttpTransportSE传输对象
HttpTransportSE httpTranstation = new HttpTransportSE(SERVICE_URL);
httpTranstation.debug = true;
// 使用SOAP1.1协议创建Envelop对象
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
// 实例化SoapObject对象
SoapObject soapObject = new SoapObject(SERVICE_NS, methodName);
envelope.bodyOut = soapObject;
// 设置与.Net提供的Web Service保持较好的兼容性
envelope.dotNet = true;
try
{
// 调用Web Service
httpTranstation.call(SERVICE_NS + methodName, envelope);
if (envelope.getResponse() != null)
{
// 获取服务器响应返回的SOAP消息
SoapObject result = (SoapObject) envelope.bodyIn;
SoapObject detail = (SoapObject) result.getProperty(methodName
+ "Result");
// 解析服务器响应的SOAP消息。
return parseProvinceOrCity(detail);
}
} catch (Exception e)
{
e.printStackTrace();
}
return null;
}
public static List getCityListByProvince(String province)
{
// 需要调用的 *** 名(获得本天气预报Web Services支持的城市信息,根据省份查询城市 *** :带参数)
String methodName = "getSupportCityString";
HttpTransportSE httpTranstation = new HttpTransportSE(SERVICE_URL);
httpTranstation.debug = true;
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
SoapObject soapObject = new SoapObject(SERVICE_NS, methodName);
soapObject.addProperty("theRegionCode", province);
envelope.bodyOut = soapObject;
envelope.dotNet = true;
try
{
// 调用Web Service
httpTranstation.call(SERVICE_NS + methodName, envelope);
if (envelope.getResponse() != null)
{
// 获取服务器响应返回的SOAP消息
SoapObject result = (SoapObject) envelope.bodyIn;
SoapObject detail = (SoapObject) result.getProperty(methodName
+ "Result");
// 解析服务器响应的SOAP消息。
return parseProvinceOrCity(detail);
}
} catch (Exception e)
{
e.printStackTrace();
}
return null;
}
private static List parseProvinceOrCity(SoapObject detail)
{
ArrayList result = new ArrayList();
for (int i = 0; i detail.getPropertyCount(); i++)
{
String str = detail.getProperty(i).toString();
// 解析出每个省份
result.add(str.split(",")[0]);
}
return result;
}
public static SoapObject getWeatherByCity(String cityName)
{
// 根据城市或地区名称查询获得未来三天内天气情况、现在的天气实况、天气和生活指数
String methodName = "getWeather";
HttpTransportSE httpTranstation = new HttpTransportSE(SERVICE_URL);
httpTranstation.debug = true;
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
SoapObject soapObject = new SoapObject(SERVICE_NS, methodName);
soapObject.addProperty("theCityCode", cityName);
envelope.bodyOut = soapObject;
envelope.dotNet = true;
try
{
// 调用Web Service
httpTranstation.call(SERVICE_NS + methodName, envelope);
if (envelope.getResponse() != null)
{
// 获取服务器响应返回的SOAP消息
SoapObject result = (SoapObject) envelope.bodyIn;
SoapObject detail = (SoapObject) result.getProperty(methodName
+ "Result");
// 解析服务器响应的SOAP消息。
return detail;
}
} catch (Exception e)
{
e.printStackTrace();
}
return null;
}
}
本经验将介绍Android如何获取天气预报主要使用了中国天气网的接口,使用webView显示。
工具/原料
Android Studio
*** /步骤
首先我们打开下载安装好的Android Studio然后新建一个项目,我这里为了方便就直接添加一个Activity了
然后我们添加界面布局代码,布局如下:
?xml version="1.0" encoding="utf-8"?
LinearLayout xmlns:android=""
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Button
android:id="@+id/bj"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/bj" /
Button
android:id="@+id/sh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sh" /
Button
android:id="@+id/heb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/heb" /
Button
android:id="@+id/cc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cc" /
Button
android:id="@+id/sy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sy" /
Button
android:id="@+id/gz"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/gz" /
/LinearLayout
WebView android:id="@+id/webView1"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:focusable="false"
android:layout_weight="1"
/
/LinearLayout
然后我们添加后台代码:
package com.basillee.asus.demo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
public class MainActivity7 extends Activity implements OnClickListener {
private WebView webView; //声明WebView组件的对象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity7);
webView=(WebView)findViewById(R.id.webView1); //获取WebView组件
webView.getSettings().setJavaScriptEnabled(true); //设置JavaScript可用
webView.setWebChromeClient(new WebChromeClient()); //处理JavaScript对话框
webView.setWebViewClient(new WebViewClient()); //处理各种通知和请求事件,如果不使用该句代码,将使用内置浏览器访问网页
webView.loadUrl(" "); //设置默认显示的天气预报信息
webView.setInitialScale(57*4); //放网页内容放大4倍
Button bj=(Button)findViewById(R.id.bj); //获取布局管理器中添加的“北京”按钮
bj.setOnClickListener(this);
Button sh=(Button)findViewById(R.id.sh); //获取布局管理器中添加的“上海”按钮
sh.setOnClickListener(this);
Button heb=(Button)findViewById(R.id.heb); //获取布局管理器中添加的“哈尔滨”按钮
heb.setOnClickListener(this);
Button cc=(Button)findViewById(R.id.cc); //获取布局管理器中添加的“长春”按钮
cc.setOnClickListener(this);
Button sy=(Button)findViewById(R.id.sy); //获取布局管理器中添加的“沈阳”按钮
sy.setOnClickListener(this);
Button gz=(Button)findViewById(R.id.gz); //获取布局管理器中添加的“广州”按钮
gz.setOnClickListener(this);
}
@Override
public void onClick(View view){
switch(view.getId()){
case R.id.bj: //单击的是“北京”按钮
openUrl("101010100T");
break;
case R.id.sh: //单击的是“上海”按钮
openUrl("101020100T");
break;
case R.id.heb: //单击的是“哈尔滨”按钮
openUrl("101050101T");
break;
case R.id.cc: //单击的是“长春”按钮
openUrl("101060101T");
break;
case R.id.sy: //单击的是“沈阳”按钮
openUrl("101070101T");
break;
case R.id.gz: //单击的是“广州”按钮
openUrl("101280101T");
break;
}
}
//打开网页的 ***
private void openUrl(String id){
webView.loadUrl(""+id+" "); //获取并显示天气预报信息
}
}
然后我们点击Android Studio上面的运行按钮:
这里要访问 *** 我们要添加权限:
uses-permission android:name="android.permission.INTERNET" /
6
我们然后可以在模拟器上面可以看到获取的天气情况
package com.nrzc.weatherstation;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import java.util.Timer;
import java.util.TimerTask;
/**
* 环境传感器
* 气象站
*/
public class MainActivity extends AppCompatActivity {
private SensorManager sensorManager;
private TextView temperatureTextView;
private TextView pressureTextView;
private TextView lightTextView;
private float currentTemperature=Float.NaN;
private float currentPressure=Float.NaN;
private float currentLight=Float.NaN;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
Timer updateTimer=new Timer("weatherUpdate");
updateTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
updateGUI();
}
},0,1000);
}
private void init(){
temperatureTextView=(TextView)findViewById(R.id.temperature);
pressureTextView=(TextView)findViewById(R.id.pressure);
lightTextView=(TextView)findViewById(R.id.light);
sensorManager=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
}
private final SensorEventListener tempSensorEventListener=new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
currentTemperature=event.values[0];
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
private final SensorEventListener pressureSensorEventListener=new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
currentPressure=event.values[0];
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
private final SensorEventListener lightSensorEventListener=new SensorEventListener() {
@Override
public void onSensorChanged(SensorEvent event) {
currentLight=event.values[0];
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
};
@Override
protected void onResume() {
super.onResume();
Sensor lightSensor=sensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
if (lightSensor!=null)
sensorManager.registerListener(lightSensorEventListener,
lightSensor,
SensorManager.SENSOR_DELAY_NORMAL);
else
lightTextView.setText("Light Sensor Unavailable");
Sensor pressureSensor=sensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE);
if (pressureSensor!=null)
sensorManager.registerListener(pressureSensorEventListener,
pressureSensor,SensorManager.SENSOR_DELAY_NORMAL);
else
pressureTextView.setText("Barometer Unavailable");
Sensor temperatureSensor=sensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);
if (temperatureSensor!=null)
sensorManager.registerListener(tempSensorEventListener,
temperatureSensor,
SensorManager.SENSOR_DELAY_NORMAL);
else
temperatureTextView.setText("Thermometer Unavailable");
}
@Override
protected void onPause() {
sensorManager.unregisterListener(pressureSensorEventListener);
sensorManager.unregisterListener(tempSensorEventListener);
sensorManager.unregisterListener(lightSensorEventListener);
super.onPause();
}
private void updateGUI(){
runOnUiThread(new Runnable() {
@Override
public void run() {
if(!Float.isNaN(currentPressure)){
pressureTextView.setText(currentPressure+"hPa");
pressureTextView.invalidate();
}
if (!Float.isNaN(currentLight)){
String lightStr="Sunny";
if (currentLight=SensorManager.LIGHT_CLOUDY)
lightStr="night";
else if (currentLight=SensorManager.LIGHT_OVERCAST)
lightStr="Cloudy";
else if (currentLight=SensorManager.LIGHT_SUNLIGHT)
lightStr="Overcast";
lightTextView.setText(lightStr);
lightTextView.invalidate();
}
if (!Float.isNaN(currentTemperature)){
temperatureTextView.setText(currentTemperature+"C");
temperatureTextView.invalidate();
}
}
});
}
}
以色列似乎正在从一场大规模的网络攻击中恢复过来。据Haaretz和Kan的Amichai Stein报道,攻击者在周一晚上攻陷了几个以色列政府网站,其中包括卫生部、内政部、司法部和福利部网站。总理办公室的网站也受到了影响。以色列国家网络管理局在一份声明中称,现在所有的网站都已重新上线。 虽然以色列政...
由周四发布的“网络垃圾”(Webspam)报告可知,谷歌搜索在 2021 年过滤的垃圾网站数量、竟是 2020 年的六倍。据悉,作为 Alphabet 旗下子公司,Google 有一套名为 SpamBrian 的人工智能垃圾过滤系统,并且可在超过 99% 情况下实现“不受垃圾所困扰”(spam-fr...
2018年,英特尔、AMD、ARM曝出CPU安全事件,引起广泛关注,舆论一片哗然。虽然英特尔公司表示此次事件不仅仅是英特尔,还涉及AMD/ARM等厂商,且CPU 漏洞补丁基本不会给普通用户造成任何影响,但这次bug依旧被定为成行业大事件。 时隔几年,CPU又再次曝出一个大bug,有意思的...
Hackernews 编译,转载请注明出处: 一个P2P的Golang僵尸网络在一年多后重新浮出水面,在一个月内侵入了医疗、教育和政府部门实体的服务器,感染了总共1500台主机。 Akamai 的研究人员在与 The Hacker News 共享的一份报告中称,这种名为 FritzFrog 的“分...
5月5日,SentinelLabs 发布报告,显示他们曾在知名防病毒产品Avast 和 AVG (2016 年被 Avast 收购)中发现了两个存在时间长达近10年之久的严重漏洞。 这两个漏洞被跟踪为 CVE-2022-26522 和 CVE-2022-26523,存在于名为 aswArPot.s...
申请一份新工作通常意味着向潜在的雇主交出一连串的个人信息,这就是为什么联邦调查局警告人们要警惕招聘网站上的假招聘广告,这些广告会窃取和出售你的详细资料。联邦调查局的互联网犯罪中心(IC3)公共服务公告警告说,自2019年以来,受害者因虚假招聘广告而损失的平均金额约为3000美元。 骗子经常仿冒正常...