Android development to obtain Weather Forecasts (weather, temperature, wind power ...) WebService

Source: Internet
Author: User
[Android] View Source

Print?

/**
 * Get Weather Forecasts (weather, temperature, wind power ...)
 */
public
class
MainActivity extends
Activity {
 
    private
static final
String NAMESPACE =
"http://WebXml.com.cn/";
    // What is next to the WebService address? The WSDL can be added or not added based on the address of the website of 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
Button okButton;
    private
SoapObject detail;
 
    @Override
    public
void onCreate(Bundle savedInstanceState) {
 
        StrictMode.setThreadPolicy(new
StrictMode.ThreadPolicy.Builder()
                .detectDiskReads().detectDiskWrites().detectNetwork()
// Replace the value with detectall ()
                                                                        // Including disk read/write and network I/O
                .penaltyLog()
// Print the logcat. You can also locate the Dropbox and save the corresponding log through the file.
                .build());
        StrictMode.setVmPolicy(new
StrictMode.VmPolicy.Builder()
                .detectLeakedSqlLiteObjects()
// Test SQLite database operations
                .penaltyLog()
// Print logcat
                .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) {
                showWeather();
            }
        });
    }
 
    private
void showWeather() {
        String city =
"Zhengzhou";
        getWeather(city);
    }
 
    public
void getWeather(String cityName) {
        try
{
            SoapObject rpc =
new SoapObject(NAMESPACE, METHOD_NAME);
            rpc.addProperty("theCityName", cityName);
 
            HttpTransportSE ht =
new HttpTransportSE(URL);
            ht.debug =
true;
 
            SoapSerializationEnvelope envelope =
new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
 
            envelope.bodyOut = rpc;
            envelope.dotNet =
true;
            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();
            parseWeather(detail);
 
            return;
        }
catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    private
void parseWeather(SoapObject detail)
            throws
UnsupportedEncodingException {
        String date = detail.getProperty(6).toString();
        weatherToday =
"Today :" + date.split(" ")[0];
        weatherToday = weatherToday +
"\ N weather :" + date.split(" ")[1];
        weatherToday = weatherToday +
"\ N temperature :"
                + detail.getProperty(5).toString();
        weatherToday = weatherToday +
"\ N wind power :"
                + detail.getProperty(7).toString() +
"\n";
        System.out.println("weatherToday is "
+ weatherToday);
        Toast.makeText(MainActivity.this, weatherToday, Toast.LENGTH_LONG)
                .show();
 
    }
 
    @Override
    public
boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
 
        return
true;
    }
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.