android webservice获取城市天气情况
Weather.java
[java]
package com.example;
import java.io.UnsupportedEncodingException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
public class Weather extends Activity {
private Button search_but;
private TextView textview1;
private EditText et;
private Button city1_1;
private Button city1_2;
private Button city1_3;
private Button city1_4;
private ImageView image1;
private ImageView image2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et=(EditText)findViewById(R.id.et);
image1=(ImageView)findViewById(R.id.image1);
image2=(ImageView)findViewById(R.id.image2);
search_but=(Button)findViewById(R.id.search_but);
city1_1=(Button)findViewById(R.id.city1_1);
city1_1.setOnClickListener(listener1);
city1_2=(Button)findViewById(R.id.city1_2);
city1_2.setOnClickListener(listener2);
city1_3=(Button)findViewById(R.id.city1_3);
city1_3.setOnClickListener(listener3);
city1_4=(Button)findViewById(R.id.city1_4);
city1_4.setOnClickListener(listener4);
search_but.setOnClickListener(listener);
}
private OnClickListener listener=new OnClickListener() {
@Override
public void onClick(View v) {
String city=et.getText().toString();
getWeather(city);
}
};
private OnClickListener listener1=new OnClickListener() {
@Override
public void onClick(View v) {
et.setText("北京");
}
};
private OnClickListener listener2=new OnClickListener() {
@Override
public void onClick(View v) {
et.setText("上海");
}
};
private OnClickListener listener3=new OnClickListener() {
@Override
public void onClick(View v) {
et.setText("深圳");
}
};
private OnClickListener listener4=new OnClickListener() {
@Override
public void onClick(View v) {
et.setText("北京");
}
};
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;
private String weatherNow;
private String weatherWillBe;
private void setIcon(String weather, ImageView imageview) {
if(weather.equalsIgnoreCase("nothing.gif"))
imageview.setBackgroundResource(R.drawable.a_nothing);
if(weather.equalsIgnoreCase("0.gif"))
imageview.setBackgroundResource(R.drawable.a_0);
if(weather.equalsIgnoreCase("1.gif"))
imageview.setBackgroundResource(R.drawable.a_1);
if(weather.equalsIgnoreCase("2.gif"))
imageview.setBackgroundResource(R.drawable.a_2);
if(weather.equalsIgnoreCase("3.gif"))
imageview.setBackgroundResource(R.dr
补充:移动开发 , Android ,