Android访问WebService

Android调用Webservice实现手机与PCSERVER的交互,废话不多说,直接贴代码,下面是一段通过WEBSERVICE获取天气的代码

 

package com.android;       import  org.ksoap2.SoapEnvelope;    import  org.ksoap2.serialization.SoapObject;    import  org.ksoap2.serialization.SoapSerializationEnvelope;    import  org.ksoap2.transport.AndroidHttpTransport;       import  android.app.Activity;    import  android.app.AlertDialog;    import  android.app.Dialog;    import  android.content.DialogInterface;    import  android.os.Bundle;    import  android.text.method.LinkMovementMethod;    import  android.util.Log;    import  android.view.Menu;    import  android.view.MenuItem;    import  android.view.View;    import  android.widget.Button;    import  android.widget.EditText;    import  android.widget.ImageView;    import  android.widget.TextView;       public   class  MainActivity  extends  Activity {         private   static  String LOG_TAG =  "Weather" ;         private   static   boolean  DEBUG =  false ;         private   static   final   int  SHOW_ABOUT =  0x0001 ;         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  String weatherTomorrow;         private  String weatherAfterday;         private  String weatherCurrent;         private   int  iconToday[] =  new   int [ 2 ];         private   int  iconTomorrow[] =  new   int [ 2 ];         private   int  iconAfterday[] =  new   int [ 2 ];            private  Button okButton;         private  EditText textInput;         private  ImageView imageView1;         private  ImageView imageView2;         private  TextView textWeatherToday;         private  ImageView imageView3;         private  ImageView imageView4;         private  TextView textWeatherTomorrow;         private  ImageView imageView5;         private  ImageView imageView6;         private  TextView textWeatherAfterday;         private  TextView textWeatherCurrent;         private  SoapObject detail;         @Override         public   void  onCreate(Bundle savedInstanceState) {             super .onCreate(savedInstanceState);            setContentView(R.layout.main);                        okButton = (Button)  findViewById(R.id.WeatherSearch);            textInput = (EditText) findViewById(R.id.TextWeather);            imageView1 = (ImageView) findViewById(R.id.ImageView01);            imageView2 = (ImageView) findViewById(R.id.ImageView02);            textWeatherToday = (TextView) findViewById(R.id.WeatherToday);            imageView3 = (ImageView) findViewById(R.id.ImageView03);            imageView4 = (ImageView) findViewById(R.id.ImageView04);            textWeatherTomorrow = (TextView) findViewById(R.id.WeatherTomorrow);            imageView5 = (ImageView) findViewById(R.id.ImageView05);            imageView6 = (ImageView) findViewById(R.id.ImageView06);            textWeatherAfterday = (TextView) findViewById(R.id.WeatherAfterday);            textWeatherCurrent = (TextView) findViewById(R.id.WeatherCurrent);                        okButton.setOnClickListener( new  Button.OnClickListener() {                 public   void  onClick(View v) {                    showWeather();                }           });        }                 private   void  showWeather() {            String city = textInput.getText().toString();             if  (city.length() ==  0 ) city =  "合肥" ;            getWeather(city);                        textWeatherToday.setText(getWeatherToday());            imageView1.setImageResource(getIconToday( 0 ));            imageView2.setImageResource(getIconToday( 1 ));                        textWeatherTomorrow.setText(getWeatherTomorrow());                  imageView3.setImageResource(getIconTomorrow( 0 ));            imageView4.setImageResource(getIconTomorrow( 1 ));                        textWeatherAfterday.setText(getWeatherAfterday());                  imageView5.setImageResource(getIconAfterday( 0 ));            imageView6.setImageResource(getIconAfterday( 1 ));                        textWeatherCurrent.setText(getWeatherCurrent());        }                 public   void  getWeather(String cityName) {             try  {                SoapObject rpc =  new  SoapObject(NAMESPACE, METHOD_NAME);                rpc.addProperty( "theCityName" , cityName);                   AndroidHttpTransport ht =  new  AndroidHttpTransport(URL);                ht.debug =  true ;                   SoapSerializationEnvelope envelope =  new  SoapSerializationEnvelope(                        SoapEnvelope.VER11);                                envelope.bodyOut = rpc;                envelope.dotNet =  true ;                envelope.setOutputSoapObject(rpc);                   ht.call(SOAP_ACTION, envelope);                                debug(LOG_TAG,  "DUMP>> "  + ht.requestDump);                debug(LOG_TAG,  "DUMP<< "  + ht.responseDump);                   SoapObject result = (SoapObject) envelope.bodyIn;                 detail = (SoapObject) result.getProperty( "getWeatherbyCityNameResult" );                                parseWeather(detail);                 return ;            }  catch  (Exception e) {                e.printStackTrace();            }        }                 private   void  parseWeather(SoapObject detail) {            String date = detail.getProperty( 6 ).toString();            weatherToday =  "今天:"  + date.split( " " )[ 0 ];            weatherToday = weatherToday +  "\n天气:"  + date.split( " " )[ 1 ];            weatherToday = weatherToday +  "\n气温:"  + detail.getProperty( 5 ).toString();            weatherToday = weatherToday +  "\n风力:"  + detail.getProperty( 7 ).toString() +  "\n" ;            iconToday[ 0 ] = parseIcon(detail.getProperty( 8 ).toString());            iconToday[ 1 ] = parseIcon(detail.getProperty( 9 ).toString());                        weatherCurrent = detail.getProperty( 10 ).toString();                        date = detail.getProperty( 13 ).toString();            weatherTomorrow =  "明天:"  + date.split( " " )[ 0 ];            weatherTomorrow = weatherTomorrow +  "\n天气:"  + date.split( " " )[ 1 ];            weatherTomorrow = weatherTomorrow +  "\n气温:"  + detail.getProperty( 12 ).toString();            weatherTomorrow = weatherTomorrow +  "\n风力:"  + detail.getProperty( 14 ).toString() +  "\n" ;            iconTomorrow[ 0 ] = parseIcon(detail.getProperty( 15 ).toString());            iconTomorrow[ 1 ] = parseIcon(detail.getProperty( 16 ).toString());                        date = detail.getProperty( 18 ).toString();            weatherAfterday =  "后天:"  + date.split( " " )[ 0 ];            weatherAfterday = weatherAfterday +  "\n天气:"  + date.split( " " )[ 1 ];            weatherAfterday = weatherAfterday +  "\n气温:"  + detail.getProperty( 17 ).toString();            weatherAfterday = weatherAfterday +  "\n风力:"  + detail.getProperty( 19 ).toString() +  "\n" ;            iconAfterday[ 0 ] = parseIcon(detail.getProperty( 20 ).toString());            iconAfterday[ 1 ] = parseIcon(detail.getProperty( 21 ).toString());        }                 public  String getWeatherToday() {            debug(LOG_TAG,  "weatherToday: "  + weatherToday);             return  weatherToday;        }                 public  String getWeatherTomorrow() {            debug(LOG_TAG,  "weatherTomorrow: "  + weatherTomorrow);             return  weatherTomorrow;        }                 public  String getWeatherAfterday() {            debug(LOG_TAG,  "weatherAfterday: "  + weatherAfterday);             return  weatherAfterday;        }                 public  String getWeatherCurrent() {            debug(LOG_TAG,  "weatherCurrent: "  + weatherCurrent);             return  weatherCurrent;        }                 public   int  getIconToday ( int  index) {             return  iconToday[index];        }                 public   int  getIconTomorrow ( int  index) {             return  iconTomorrow[index];        }                 public   int  getIconAfterday ( int  index) {             return  iconAfterday[index];        }                 private   int  parseIcon(String strIcon) {             if  (strIcon ==  nullreturn  - 1 ;                         if  ( "0.gif" .equals(strIcon))  return  R.drawable.a_0;             if  ( "1.gif" .equals(strIcon))  return  R.drawable.a_1;             if  ( "2.gif" .equals(strIcon))  return  R.drawable.a_2;             if  ( "3.gif" .equals(strIcon))  return  R.drawable.a_3;             if  ( "4.gif" .equals(strIcon))  return  R.drawable.a_4;             if  ( "5.gif" .equals(strIcon))  return  R.drawable.a_5;             if  ( "6.gif" .equals(strIcon))  return  R.drawable.a_6;             if  ( "7.gif" .equals(strIcon))  return  R.drawable.a_7;             if  ( "8.gif" .equals(strIcon))  return  R.drawable.a_8;             if  ( "9.gif" .equals(strIcon))  return  R.drawable.a_9;             if  ( "10.gif" .equals(strIcon))  return  R.drawable.a_10;             if  ( "11.gif" .equals(strIcon))  return  R.drawable.a_11;             if  ( "12.gif" .equals(strIcon))  return  R.drawable.a_12;             if  ( "13.gif" .equals(strIcon))  return  R.drawable.a_13;             if  ( "14.gif" .equals(strIcon))  return  R.drawable.a_14;             if  ( "15.gif" .equals(strIcon))  return  R.drawable.a_15;             if  ( "16.gif" .equals(strIcon))  return  R.drawable.a_16;             if  ( "17.gif" .equals(strIcon))  return  R.drawable.a_17;             if  ( "18.gif" .equals(strIcon))  return  R.drawable.a_18;             if  ( "19.gif" .equals(strIcon))  return  R.drawable.a_19;             if  ( "20.gif" .equals(strIcon))  return  R.drawable.a_20;             if  ( "21.gif" .equals(strIcon))  return  R.drawable.a_21;             if  ( "22.gif" .equals(strIcon))  return  R.drawable.a_22;             if  ( "23.gif" .equals(strIcon))  return  R.drawable.a_23;             if  ( "24.gif" .equals(strIcon))  return  R.drawable.a_24;             if  ( "25.gif" .equals(strIcon))  return  R.drawable.a_25;             if  ( "26.gif" .equals(strIcon))  return  R.drawable.a_26;             if  ( "27.gif" .equals(strIcon))  return  R.drawable.a_27;             if  ( "28.gif" .equals(strIcon))  return  R.drawable.a_28;             if  ( "29.gif" .equals(strIcon))  return  R.drawable.a_29;             if  ( "30.gif" .equals(strIcon))  return  R.drawable.a_30;             if  ( "31.gif" .equals(strIcon))  return  R.drawable.a_31;                         return   0 ;        }                 private   static   void  debug(String tag, String msg) {             if  (DEBUG) Log.d(tag, msg);        }                 private   void  showAbout() {            TextView textAbout =  new  TextView( this );            textAbout.setText(R.string.about_text);            textAbout.setMovementMethod(LinkMovementMethod.getInstance());               Dialog dlg =  new  AlertDialog.Builder( this )                .setTitle(R.string.app_about)                .setView(textAbout)                .setPositiveButton(R.string.about_ok,  new  DialogInterface.OnClickListener() {                     public   void  onClick(DialogInterface dialog,  int  whichButton) {                    }                })                .create();            dlg.show();        }         private   void  about_city(){                        String result_city=detail.getProperty( 11 ).toString();             new  AlertDialog.Builder( this ).setTitle(textInput.getText().toString()).setMessage(result_city).setPositiveButton( "OK"null ).show();                                }         public   boolean  onCreateOptionsMenu(Menu menu) {             super .onCreateOptionsMenu(menu);                        menu.add( 0 , SHOW_ABOUT,  0 , R.string.app_about);                        menu.add( 0 , 2 , 1 ,R.string.city);                //        menu.addSubMenu("jack").add("tom1")    //        .add("tom2");             //menu.addS             this .getMenuInflater().inflate(R.layout.menu, menu);             return   true ;           }            public   boolean  onOptionsItemSelected(MenuItem item) {             switch  (item.getItemId()) {             case  SHOW_ABOUT:                showAbout();                 break ;             case   2 :about_city();                 break ;            }             return   true ;        }    } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值