WebService

来源:互联网 发布:网页编程用什么语言 编辑:程序博客网 时间:2024/06/02 06:39
package com.example.tianqi;


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.util.Log;
import android.widget.TextView;


public class MainActivity extends Activity {


// 名称空间
private static final String namespace = "http://www.36wu.com/";
// 方法名
private static final String name = "GetWeather";
// 请求的地址
private static final String url = "http://web.36wu.com/WeatherService.asmx?WSDL";
// action获取天气调用的地址
private static final String soapAction = "http://www.36wu.com/GetWeather";


private TextView textView;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


textView = (TextView) findViewById(R.id.tv);


// 建立方法,在线程里面写
new Thread() {
public void run() {
setWeather();
};
}.start();
}


protected void setWeather() {


SoapObject sb = new SoapObject(namespace, name);
sb.addProperty("district", "邯郸");
sb.addProperty("authkey", "5e431e2f00544a73b048b00458fb187d");


SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
// 输出写出来
envelope.bodyOut = sb;
envelope.dotNet = true;


// 访问网络
HttpTransportSE SE = new HttpTransportSE(url);
try {
SE.call(soapAction, envelope);


final SoapObject bodyIn = (SoapObject) envelope.bodyIn;
runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText(bodyIn.toString());
}
});


} catch (Exception e) {
e.printStackTrace();
Log.e("", e != null ? e.getMessage() : "出错了");
}
}

}





1 0
原创粉丝点击