android --天气预报

今天参考了别人的关于android平台的天气预报,自己也修改后实现自己要实现的格式,特地拿出来分享:

首先要导入第三方包:ksoap2-android-assembly-2.4-jar-with-dependencies.jar

  主程序的代码:

  

package com.sebservice;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class WebsActivity extends Activity {

private Button okButton;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

okButton = (Button) this.findViewById(R.id.bt1);
okButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
String city = "北京";
getWeather(city);
}

});
}

private static final String NAMESPACE = "http://WebXml.com.cn/";

// WebService地址
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;

public void getWeather(String cityName) {
try {
 SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);
 rpc.addProperty("theCityName", cityName);

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = rpc;
envelope.dotNet = true;
envelope.setOutputSoapObject(rpc);
 HttpTransportSE ht = new HttpTransportSE(URL);

ht.debug = true;

ht.call(SOAP_ACTION, envelope);
detail =(SoapObject) envelope.getResponse();

 getw(detail,city);
return;

} catch (Exception e) {
e.printStackTrace();
}
}
private void getw(SoapObject detail,String local)throws UnsupportedEncodingException
{

String str=detail.getProperty(6).toString();
String show="所查城市:"+local;
show=show+"\n今天是:"+str.split(" ")[0];
show=show+"\n天气:"+str.split(" ")[1];
show=show+"\n风级:"+detail.getProperty(7).toString().split(" ")[0];
show=show+"\n"+detail.getProperty(10).toString().split(" ")[0];
show=show+"\n温馨提示:\n"+detail.getProperty(11).toString().split(" ")[0];
Builder al=new AlertDialog.Builder(this);
al.setTitle("天气预报");
al.setPositiveButton("确定", null);
al.setMessage(show.toString());
al.create().show();

}

}

 

 

布局文件的内容是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/bt1"
android:text="查看北京天气"

/>

</LinearLayout>

   

3、还要在androidmanifest.xml加入权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sebservice"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="10" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".WebsActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

</manifest>

你可能感兴趣的:(android)