wenserver gets weather forecast data instances to share

  • 2020-05-19 05:49:08
  • OfStack


package tao.cs;
import java.io.IOException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;
import org.xmlpull.v1.XmlPullParserException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
public class WeatherMain extends Activity {
    Button btn01;   
    TextView tv01,tv02;   
    ImageView imgview01,imgview02;   
    EditText et01;   

    int int_img_1;   
    int int_img_2;   

     public void onCreate(Bundle savedInstanceState) {   
         super.onCreate(savedInstanceState);   
         setContentView(R.layout.main);   

         // component    
       //  final EditText et01=(EditText) findViewById(R.id.et_TextWeather);   
         btn01=(Button) findViewById(R.id.Button01);   
         tv01=(TextView) findViewById(R.id.TextView01);   
         tv02=(TextView) findViewById(R.id.TextView02);   
         imgview01=(ImageView) findViewById(R.id.ImageView01);   
         imgview02=(ImageView) findViewById(R.id.ImageView02);   
         et01=(EditText) findViewById(R.id.EditText01);   

         btn01.setOnClickListener(new OnClickListener() {   

             public void onClick(View v) {   
                 showWeather();   

             }   
         });   

            
     }//onCreate ***************end   
     protected void showWeather(){// Display all the information    

         String str_city=et01.getText().toString();   
         if(str_city.length()==0){   
             str_city=" chongqing ";   

         }   
//         str_city=" jinan ";   
         getWeather(str_city);   
         tv01.setText(getString_WeatherToday());  // The weather today    
         tv02.setText(getString_WeatherCurrent());// The current weather    
         imgview01.setImageResource(getIcon_1());  // The current two ICONS    
         imgview02.setImageResource(getIcon_2());   

     }//showWeather()*****************end   

     private static final String NAME_SPACE="http://WebXml.com.cn/";// The namespace   , The late testing 1 The role of the namespace    
     private static final String METHOD_NAME = "getWeatherbyCityName";   
     private static String URL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx";//  The delivery SOAP The destination address of the data    
     private static String SOAP_ACTION = "http://WebXml.com.cn/getWeatherbyCityName";   

     protected void getWeather(String strcityname){//str  Is the city name    
  /*  
   *  So here's the job   Submit the client's request ,  
   *  You then get the complex object returned by the server , There will be a special one in the back   
   */  
         SoapObject sobject=new SoapObject(NAME_SPACE, METHOD_NAME);   
         sobject.addProperty("theCityName", strcityname);   

         AndroidHttpTransport ht=new AndroidHttpTransport(URL);   
         ht.debug=true;   

         SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);// tell SoapSerializationEnvelope Put together SoapObject Encapsulate:    
         envelope.bodyOut=sobject;   
         envelope.dotNet=true;   
         envelope.setOutputSoapObject(sobject);   

         try {   

             ht.call(SOAP_ACTION, envelope);   

         } catch (IOException e) {   

             e.printStackTrace();   
         } catch (XmlPullParserException e) {   

             e.printStackTrace();   
         }   
         //  
         SoapObject result=(SoapObject) envelope.bodyIn;   
         SoapObject detail=(SoapObject) result.getProperty("getWeatherbyCityNameResult");// Similar to getting a server to return a complex node 1 A contact in    
         parseWeather(detail);    

     }//getweather *****************end   

     String str_weather_today=null;   
     String str_weather_current=null;   
     protected void parseWeather(SoapObject so){   
         /*  
          *  Parse complex node , And assign values to the corresponding parameters , Prepare for the next method   
          */  
         // The first 6 The parameters are overview , Including dates , The weather ....   
         String date=so.getProperty(6).toString();   

         str_weather_today=" Today, : "+date.split(" ")[0];   
         str_weather_today=str_weather_today+"\n The temperature :"+so.getProperty(5).toString();   
         str_weather_today=str_weather_today+"\n wind :"+so.getProperty(7).toString()+"\n";   
         // Two ICONS    
         int_img_1=parseIcon(so.getProperty(8).toString());   
         int_img_2=parseIcon(so.getProperty(9).toString());   

         str_weather_current=so.getProperty(10).toString();   
     }   
     public String getString_WeatherToday(){   
         return str_weather_today;   
     }   
     public String getString_WeatherCurrent(){   
         return str_weather_current;   
     }   

     public int getIcon_1(){   
         return int_img_1;   
     }   
     public int getIcon_2(){   
         return int_img_2;   
     }   
     public int   parseIcon(String strIcon){   
         if (strIcon == null) return -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;   
     }

}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<EditText 

 android:id="@+id/EditText01" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content">
</EditText>
<Button 
 android:text=" The query " 
 android:id="@+id/Button01" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content">
</Button>
<ImageView 
 android:id="@+id/ImageView01" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content">
</ImageView>
<ImageView 
 android:id="@+id/ImageView02" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content">
</ImageView>
<TextView  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:id="@+id/TextView01" 

    />
<TextView 

 android:id="@+id/TextView02" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content">
</TextView>

</LinearLayout>


Related articles: