android调用WebService进行天气查询

package android.weather.ui;



import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.weather.domain.CallWebService;
import android.weather.domain.WebServiceUtil;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class MyWeatherActivity extends Activity {


private CallWebService service;
private EditText etCityName;
private TextView tvShowResult;
private Button button;


@Override
protected void onCreate(Bundle savedInstanceState) {
final String namespace = "http://WebXml.com.cn/";
final String url = "http://webservice.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl";

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webservice);

etCityName = (EditText) this.findViewById(R.id.etCityName);
tvShowResult = (TextView) this.findViewById(R.id.textView1);
button = (Button) this.findViewById(R.id.button1);

button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String cityName = etCityName.getText().toString();

service = new WebServiceUtil(namespace,url);
service.callWebservice("getWeatherbyCityName", cityName);
String reString = service.handleWebServiceResult();
tvShowResult.setText(reString);
}
});


}
}






package android.weather.domain;


import org.ksoap2.serialization.SoapObject;


public interface CallWebService {
public SoapObject callWebservice(String methodName,String cityName);
public String handleWebServiceResult();
}






package android.weather.domain;


import java.io.IOException;


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


public class WebServiceUtil implements CallWebService {



String namespace ;
String url ;

public WebServiceUtil(String namespace, String url) {

this.namespace = namespace;
this.url = url;
}


SoapObject ss = null;
public SoapObject callWebservice(String methodName,String cityName) {




SoapObject soapObject = new SoapObject(namespace, methodName);
soapObject.addProperty("theCityName", cityName);

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = soapObject;
envelope.dotNet = true ;

HttpTransportSE httpTransportSE = new HttpTransportSE(url);
httpTransportSE.debug = true ; 
try {
httpTransportSE.call(namespace+methodName, envelope);
if( envelope.getResponse() != null ) {
SoapObject resultObject = (SoapObject) envelope.bodyIn;
ss = (SoapObject) resultObject.getProperty(methodName+"Result");
setSs(ss);
return ss;
}
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
return ss;
}


@Override
public String handleWebServiceResult() {
String resultString = ss.toString(); 
return resultString;
}


public SoapObject getSs() {
return ss;
}


public void setSs(SoapObject ss) {
this.ss = ss;
}
}




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


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/TextView"
        android:textSize="23sp" />


    <EditText
        android:id="@+id/etCityName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/inputCityName"
        android:textSize="23sp" >


        <requestFocus />
    </EditText>


    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />


    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/showResult"
        android:textSize="23sp" />


</LinearLayout>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值