package com.ts.xtweatherreport01;
import java.io.UnsupportedEncodingException;import android.annotation.SuppressLint;import android.annotation.TargetApi;import android.app.Activity;import android.os.Build;import android.os.Bundle;import android.os.Handler;import android.os.HandlerThread;import android.os.Message;import android.os.StrictMode;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;import org.ksoap2.SoapEnvelope;import org.ksoap2.serialization.SoapObject;import org.ksoap2.serialization.SoapSerializationEnvelope;//import org.ksoap2.transport.AndroidHttpTransport;import org.ksoap2.transport.HttpTransportSE;public class TSMainActivity extends Activity { private Handler handler = null; private Button okButton; private TextView v; private static final String NAMESPACE = "http://WebXml.com.cn/"; private static String URL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx"; private static final String METHOD_NAME = "getWeatherbyCityName"; private static String SOAP_ACTION = "http://WebXml.com.cn/getWeatherbyCityName"; private String weatherToday; private SoapObject detail; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_tsmain); v=(TextView)this.findViewById(R.id.textView2); okButton = (Button) this.findViewById(R.id.button1); okButton.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { HandlerThread handlerThread = new HandlerThread("myHandlerThread"); handlerThread.start(); handler = new Handler(handlerThread.getLooper()); handler.post(new MyRunnable()); } }); } private class MyRunnable implements Runnable { public void run() { try { Thread.sleep(0); String city = "北京"; getWeather(city); } catch (InterruptedException e) { e.printStackTrace(); } } } public void getWeather(String cityName) { System.out.println("rpc------"); SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME); System.out.println("rpc" + rpc); System.out.println("cityName is " + cityName); rpc.addProperty("theCityName", cityName); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10); envelope.bodyOut = rpc; envelope.dotNet = true; envelope.setOutputSoapObject(rpc); HttpTransportSE ht = new HttpTransportSE(URL); ht.debug = true; try { ht.call(SOAP_ACTION, envelope); detail =(SoapObject) envelope.getResponse(); System.out.println("detail" + detail); Toast.makeText(this, detail.toString(), Toast.LENGTH_LONG).show(); v.setText(detail.toString()); parseWeather(detail); return; } catch (Exception e) { e.printStackTrace(); } } 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(TSMainActivity.this, weatherToday, Toast.LENGTH_LONG).show(); }}