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

  • packagecom.android;
  • importorg.ksoap2.SoapEnvelope;
  • importorg.ksoap2.serialization.SoapObject;
  • importorg.ksoap2.serialization.SoapSerializationEnvelope;
  • importorg.ksoap2.transport.AndroidHttpTransport;
  • importandroid.app.Activity;
  • importandroid.app.AlertDialog;
  • importandroid.app.Dialog;
  • importandroid.content.DialogInterface;
  • importandroid.os.Bundle;
  • importandroid.text.method.LinkMovementMethod;
  • importandroid.util.Log;
  • importandroid.view.Menu;
  • importandroid.view.MenuItem;
  • importandroid.view.View;
  • importandroid.widget.Button;
  • importandroid.widget.EditText;
  • importandroid.widget.ImageView;
  • importandroid.widget.TextView;
  • publicclassMainActivityextendsActivity{
  • privatestaticStringLOG_TAG="Weather";
  • privatestaticbooleanDEBUG=false;
  • privatestaticfinalintSHOW_ABOUT=0x0001;
  • privatestaticfinalStringNAMESPACE="http://WebXml.com.cn/";
  • //WebService地址
  • privatestaticStringURL="http://www.webxml.com.cn/webservices/weatherwebservice.asmx";
  • privatestaticfinalStringMETHOD_NAME="getWeatherbyCityName";
  • privatestaticStringSOAP_ACTION="http://WebXml.com.cn/getWeatherbyCityName";
  • privateStringweatherToday;
  • privateStringweatherTomorrow;
  • privateStringweatherAfterday;
  • privateStringweatherCurrent;
  • privateinticonToday[]=newint[2];
  • privateinticonTomorrow[]=newint[2];
  • privateinticonAfterday[]=newint[2];
  • privateButtonokButton;
  • privateEditTexttextInput;
  • privateImageViewimageView1;
  • privateImageViewimageView2;
  • privateTextViewtextWeatherToday;
  • privateImageViewimageView3;
  • privateImageViewimageView4;
  • privateTextViewtextWeatherTomorrow;
  • privateImageViewimageView5;
  • privateImageViewimageView6;
  • privateTextViewtextWeatherAfterday;
  • privateTextViewtextWeatherCurrent;
  • privateSoapObjectdetail;
  • @Override
  • publicvoidonCreate(BundlesavedInstanceState){
  • 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(newButton.OnClickListener(){
  • publicvoidonClick(Viewv){
  • showWeather();
  • }
  • });
  • }
  • privatevoidshowWeather(){
  • Stringcity=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());
  • }
  • publicvoidgetWeather(StringcityName){
  • try{
  • SoapObjectrpc=newSoapObject(NAMESPACE,METHOD_NAME);
  • rpc.addProperty("theCityName",cityName);
  • AndroidHttpTransportht=newAndroidHttpTransport(URL);
  • ht.debug=true;
  • SoapSerializationEnvelopeenvelope=newSoapSerializationEnvelope(
  • 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);
  • SoapObjectresult=(SoapObject)envelope.bodyIn;
  • detail=(SoapObject)result.getProperty("getWeatherbyCityNameResult");
  • parseWeather(detail);
  • return;
  • }catch(Exceptione){
  • e.printStackTrace();
  • }
  • }
  • privatevoidparseWeather(SoapObjectdetail){
  • Stringdate=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());
  • }
  • publicStringgetWeatherToday(){
  • debug(LOG_TAG,"weatherToday:"+weatherToday);
  • returnweatherToday;
  • }
  • publicStringgetWeatherTomorrow(){
  • debug(LOG_TAG,"weatherTomorrow:"+weatherTomorrow);
  • returnweatherTomorrow;
  • }
  • publicStringgetWeatherAfterday(){
  • debug(LOG_TAG,"weatherAfterday:"+weatherAfterday);
  • returnweatherAfterday;
  • }
  • publicStringgetWeatherCurrent(){
  • debug(LOG_TAG,"weatherCurrent:"+weatherCurrent);
  • returnweatherCurrent;
  • }
  • publicintgetIconToday(intindex){
  • returniconToday[index];
  • }
  • publicintgetIconTomorrow(intindex){
  • returniconTomorrow[index];
  • }
  • publicintgetIconAfterday(intindex){
  • returniconAfterday[index];
  • }
  • privateintparseIcon(StringstrIcon){
  • if(strIcon==null)return-1;
  • if("0.gif".equals(strIcon))returnR.drawable.a_0;
  • if("1.gif".equals(strIcon))returnR.drawable.a_1;
  • if("2.gif".equals(strIcon))returnR.drawable.a_2;
  • if("3.gif".equals(strIcon))returnR.drawable.a_3;
  • if("4.gif".equals(strIcon))returnR.drawable.a_4;
  • if("5.gif".equals(strIcon))returnR.drawable.a_5;
  • if("6.gif".equals(strIcon))returnR.drawable.a_6;
  • if("7.gif".equals(strIcon))returnR.drawable.a_7;
  • if("8.gif".equals(strIcon))returnR.drawable.a_8;
  • if("9.gif".equals(strIcon))returnR.drawable.a_9;
  • if("10.gif".equals(strIcon))returnR.drawable.a_10;
  • if("11.gif".equals(strIcon))returnR.drawable.a_11;
  • if("12.gif".equals(strIcon))returnR.drawable.a_12;
  • if("13.gif".equals(strIcon))returnR.drawable.a_13;
  • if("14.gif".equals(strIcon))returnR.drawable.a_14;
  • if("15.gif".equals(strIcon))returnR.drawable.a_15;
  • if("16.gif".equals(strIcon))returnR.drawable.a_16;
  • if("17.gif".equals(strIcon))returnR.drawable.a_17;
  • if("18.gif".equals(strIcon))returnR.drawable.a_18;
  • if("19.gif".equals(strIcon))returnR.drawable.a_19;
  • if("20.gif".equals(strIcon))returnR.drawable.a_20;
  • if("21.gif".equals(strIcon))returnR.drawable.a_21;
  • if("22.gif".equals(strIcon))returnR.drawable.a_22;
  • if("23.gif".equals(strIcon))returnR.drawable.a_23;
  • if("24.gif".equals(strIcon))returnR.drawable.a_24;
  • if("25.gif".equals(strIcon))returnR.drawable.a_25;
  • if("26.gif".equals(strIcon))returnR.drawable.a_26;
  • if("27.gif".equals(strIcon))returnR.drawable.a_27;
  • if("28.gif".equals(strIcon))returnR.drawable.a_28;
  • if("29.gif".equals(strIcon))returnR.drawable.a_29;
  • if("30.gif".equals(strIcon))returnR.drawable.a_30;
  • if("31.gif".equals(strIcon))returnR.drawable.a_31;
  • return0;
  • }
  • privatestaticvoiddebug(Stringtag,Stringmsg){
  • if(DEBUG)Log.d(tag,msg);
  • }
  • privatevoidshowAbout(){
  • TextViewtextAbout=newTextView(this);
  • textAbout.setText(R.string.about_text);
  • textAbout.setMovementMethod(LinkMovementMethod.getInstance());
  • Dialogdlg=newAlertDialog.Builder(this)
  • .setTitle(R.string.app_about)
  • .setView(textAbout)
  • .setPositiveButton(R.string.about_ok,newDialogInterface.OnClickListener(){
  • publicvoidonClick(DialogInterfacedialog,intwhichButton){
  • }
  • })
  • .create();
  • dlg.show();
  • }
  • privatevoidabout_city(){
  • Stringresult_city=detail.getProperty(11).toString();
  • newAlertDialog.Builder(this).setTitle(textInput.getText().toString()).setMessage(result_city).setPositiveButton("OK",null).show();
  • }
  • publicbooleanonCreateOptionsMenu(Menumenu){
  • 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);
  • returntrue;
  • }
  • publicbooleanonOptionsItemSelected(MenuItemitem){
  • switch(item.getItemId()){
  • caseSHOW_ABOUT:
  • showAbout();
  • break;
  • case2:about_city();
  • break;
  • }
  • returntrue;
  • }
  • }
  • 更多相关文章

    1. Android应用实例之----天气预报程序
    2. Android解析中国天气接口JSon数据,应用于天气查询!
    3. Android3.0自带天气例子
    4. 关于android 天气预报的源码
    5. Android调用天气预报的WebService简单例子
    6. Android[项目] Android天气预报
    7. Android访问中央气象台的天气预报API得到天气数据
    8. 使用java获取未来7天天气信息,可用于android
    9. Android通过调用Webservice实现天气预报

    随机推荐

    1. 适合Material Dsign的新抽屉---Navigatio
    2. 使用Android手机APP查看ROS中RGB摄像头数
    3. listview每一条中间有空隙问题解决
    4. 2017-06-09-LayoutTransition 容器布局动
    5. RK3399 Android7.1 编译
    6. Android(安卓)RSA 公钥加密 遇到坑
    7. Android中AsyncTask的源码分析以及实例
    8. LayoutAnimationController
    9. retrofit2+okhttp3+rxjava网络封装
    10. Android(安卓)Studio导入项目的几个问题