`
he91_com
  • 浏览: 373787 次
文章分类
社区版块
存档分类
最新评论

android调用Webservice——天气情况查询

 
阅读更多

利用Webservice连接网络进行实时查询,可以根据手机号码查询归属地也可根据城市名称查询天气情况。两者相差不多,这里先做下天气情况的查询:

首先,对展示的页面进行布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="@drawable/a"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="vertical">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="@dimen/padding_medium"
        android:text="@string/cityname"
        tools:context=".MainActivity" />
    <EditText 
        android:id="@+id/cityname"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    <Button 
        android:id="@+id/search"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/search"/>
    <TextView 
        android:id="@+id/result"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>


其次,对方法进行封装(很重要哦,一个标点都不能出错)

public class SOAPUtil {
	public static Object doTransport(final String wsdUrl,final String webMethod,String city){
	         String nameSpace="http://WebXml.com.cn/";
		SoapObject soapObject=new SoapObject(nameSpace, webMethod);
		soapObject.addProperty("theCityName", city);
		System.out.println(city);
		SoapSerializationEnvelope soapSerializationEnvelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapSerializationEnvelope.bodyOut=soapObject;
		soapSerializationEnvelope.dotNet=true;
		soapSerializationEnvelope.setOutputSoapObject(soapObject);
		HttpTransportSE httpTransportSE=new HttpTransportSE(wsdUrl);
		
		String SOAP_ACTION="http://WebXml.com.cn/"+webMethod;
		System.out.println(SOAP_ACTION);
		try{
			httpTransportSE.call(SOAP_ACTION, soapSerializationEnvelope);
			System.out.println("调用结束");
			System.out.println(soapSerializationEnvelope.getResponse());
			if(soapSerializationEnvelope.getResponse()!=null){
				Object result=soapSerializationEnvelope.getResponse();
				System.out.println(result);
				return result;
			}
		}catch(IOException e){
			System.out.println("IOException");
			e.printStackTrace();
		}catch(XmlPullParserException e){
			e.printStackTrace();
		}		
		
		
		
		
		return null;
		
	}

}

主要方法的调用了,很简单,除了点击事件作出相应的改变,其他的都和最初学习的一样:

public class MainActivity extends Activity {
	private EditText city;
	private Button searchs;
	private TextView results;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        city=(EditText) findViewById(R.id.cityname);
        searchs=(Button) findViewById(R.id.search);
        results= (TextView) findViewById(R.id.result);
        searchs.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String cityname=city.getText().toString();
				String wsdUrl="http://www.webxml.com.cn/webservices/weatherwebservice.asmx";
				String method="getWeatherbyCityName";
				Object result=SOAPUtil.doTransport(wsdUrl, method, cityname);
				results.setText(result.toString());
				
				
			}
		});
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    
}


最后别忘了,进行网络访问许可:

<!-- 允许访问网络 -->
    <uses-permission  android:name="android.permission.INTERNET"/>


效果如图显示:

如上图,结果显示中有许多我们并不希望显示出来的信息如anyType,String等都显示出来了,在原先的基础上修改一点代码即可实现自己希望的结果哦!

请看下一篇吐舌头奋斗

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics