[android]程式碼程式庫view source
print?
public class MainActivity extends
Activity { |
private
static final String NAMESPACE = "http://WebXml.com.cn/"; |
// WebService地址 後面的?wsdl根據WebService地宮的地址可加可不加 |
private
static String URL =
"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl"; |
private
static final String METHOD_NAME = "getWeatherbyCityName"; |
private
static String SOAP_ACTION = NAMESPACE + METHOD_NAME; |
private
String weatherToday; |
private
SoapObject detail; |
public
void onCreate(Bundle savedInstanceState) { |
StrictMode.setThreadPolicy(new
StrictMode.ThreadPolicy.Builder() |
.detectDiskReads().detectDiskWrites().detectNetwork()
// 這裡可以替換為detectAll() |
.penaltyLog()
// 列印logcat,當然也可以定位到dropbox,通過檔案儲存相應的log |
StrictMode.setVmPolicy(new
StrictMode.VmPolicy.Builder() |
.detectLeakedSqlLiteObjects()
// 探測SQLite資料庫操作 |
.penaltyDeath().build()); |
super.onCreate(savedInstanceState); |
setContentView(R.layout.activity_main); |
okButton = (Button) findViewById(R.id.ok); |
okButton.setOnClickListener(new
Button.OnClickListener() { |
public
void onClick(View v) { |
private
void showWeather() { |
public
void getWeather(String cityName) { |
SoapObject rpc =
new SoapObject(NAMESPACE, METHOD_NAME); |
rpc.addProperty("theCityName", cityName); |
HttpTransportSE ht =
new HttpTransportSE(URL); |
SoapSerializationEnvelope envelope =
new SoapSerializationEnvelope( |
envelope.setOutputSoapObject(rpc); |
ht.call(SOAP_ACTION, envelope); |
// ht.call(null, envelope); |
SoapObject result = (SoapObject) envelope.bodyIn; |
detail = (SoapObject) result |
.getProperty("getWeatherbyCityNameResult"); |
System.out.println("result"
+ result); |
System.out.println("detail"
+ detail); |
Toast.makeText(MainActivity.this, detail.toString(), |
Toast.LENGTH_LONG).show(); |
private
void parseWeather(SoapObject detail) |
throws
UnsupportedEncodingException { |
String date = detail.getProperty(6).toString(); |
weatherToday =
"今天:" + date.split(" ")[0]; |
weatherToday = weatherToday +
"\n天氣:" + date.split(" ")[1]; |
weatherToday = weatherToday +
"\n氣溫:" |
+ detail.getProperty(5).toString(); |
weatherToday = weatherToday +
"\n風力:" |
+ detail.getProperty(7).toString() +
"\n"; |
System.out.println("weatherToday is "
+ weatherToday); |
Toast.makeText(MainActivity.this, weatherToday, Toast.LENGTH_LONG) |
public
boolean onCreateOptionsMenu(Menu menu) { |
getMenuInflater().inflate(R.menu.activity_main, menu); |