webservice

布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />


</RelativeLayout>

=================================================================================

代码

package com.example.day2_webservice;


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;


import android.os.Bundle;
import android.app.Activity;


public class MainActivity extends Activity {
// String key = "5ae00c9c22fa435ba56161ef0d263b7c";
//要访问的方法
String name = "GetWeather";
//命名空间
String namespace = "http://www.36wu.com/";


String url = "http://web.36wu.com/WeatherService.asmx?WSDL";

String soapAction = "http://www.36wu.com/GetWeather";



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//android4.0之后,主线程不可以做联网操作
new Thread() {


public void run() {
getWeather();
};


}.start();


}


/**
* 联网获得天气情况
*/
private void getWeather() {
// 得到HttpTransportSE对象,设置请求地址
HttpTransportSE httpTransportSE = new HttpTransportSE(url);
//得到SoapSerializationEnvelope,携带与服务器通信的信息
         SoapSerializationEnvelope serializationEnvelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
       //得到SoapObject对象,主要携带要传递给服务器的请求信息(键值对的形式),
         SoapObject soapObject_out=new SoapObject(namespace, name);
//         (键值对的形式携带信息),
         soapObject_out.addProperty("district", "太原");
         soapObject_out.addProperty("authkey", "5ae00c9c22fa435ba56161ef0d263b7c");
         //设置传递给服务器的信息
         serializationEnvelope.bodyOut=soapObject_out;
         //设置对.NET语言更好的支持
  serializationEnvelope.dotNet = true;


try {
// 开始访问
httpTransportSE.call(soapAction, serializationEnvelope);
// 得到服务器返回的数据
SoapObject soapObject_In = (SoapObject) serializationEnvelope.bodyIn;


System.out.println("整体信息==" + soapObject_In.toString());


// 获得Key是GetWeatherResult的信息
SoapObject getWeatherResult = (SoapObject) soapObject_In
.getProperty("GetWeatherResult");
System.out.println("getWeatherResult=="
+ getWeatherResult.toString());


// 获得Key是data的信息
SoapObject data = (SoapObject) getWeatherResult.getProperty("data");


System.out.println("data==" + data.toString());

/*
* int propertyCount = data.getPropertyCount();

* System.out.println(propertyCount+"条数据");
*/

// 获得Key是minTemp的信息
Object minTemp = data.getProperty("minTemp");
System.out.println(minTemp + "度");


} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值