Android访问WebService

  1. package com.android;   
  2.   
  3. import org.ksoap2.SoapEnvelope;   
  4. import org.ksoap2.serialization.SoapObject;   
  5. import org.ksoap2.serialization.SoapSerializationEnvelope;   
  6. import org.ksoap2.transport.AndroidHttpTransport;   
  7.   
  8. import android.app.Activity;   
  9. import android.app.AlertDialog;   
  10. import android.app.Dialog;   
  11. import android.content.DialogInterface;   
  12. import android.os.Bundle;   
  13. import android.text.method.LinkMovementMethod;   
  14. import android.util.Log;   
  15. import android.view.Menu;   
  16. import android.view.MenuItem;   
  17. import android.view.View;   
  18. import android.widget.Button;   
  19. import android.widget.EditText;   
  20. import android.widget.ImageView;   
  21. import android.widget.TextView;   
  22.   
  23. public class MainActivity extends Activity {   
  24.     private static String LOG_TAG = "Weather";   
  25.     private static boolean DEBUG = false;   
  26.     private static final int SHOW_ABOUT = 0x0001;   
  27.     private static final String NAMESPACE = "http://WebXml.com.cn/";   
  28.     //WebService地址   
  29.     private static String URL = "http://www.webxml.com.cn/webservices/weatherwebservice.asmx";   
  30.     private static final String METHOD_NAME = "getWeatherbyCityName";   
  31.     private static String SOAP_ACTION = "http://WebXml.com.cn/getWeatherbyCityName";   
  32.   
  33.     private String weatherToday;   
  34.     private String weatherTomorrow;   
  35.     private String weatherAfterday;   
  36.     private String weatherCurrent;   
  37.     private int iconToday[] = new int[2];   
  38.     private int iconTomorrow[] = new int[2];   
  39.     private int iconAfterday[] = new int[2];   
  40.   
  41.     private Button okButton;   
  42.     private EditText textInput;   
  43.     private ImageView imageView1;   
  44.     private ImageView imageView2;   
  45.     private TextView textWeatherToday;   
  46.     private ImageView imageView3;   
  47.     private ImageView imageView4;   
  48.     private TextView textWeatherTomorrow;   
  49.     private ImageView imageView5;   
  50.     private ImageView imageView6;   
  51.     private TextView textWeatherAfterday;   
  52.     private TextView textWeatherCurrent;   
  53.     private SoapObject detail;   
  54.     @Override  
  55.     public void onCreate(Bundle savedInstanceState) {   
  56.         super.onCreate(savedInstanceState);   
  57.         setContentView(R.layout.main);   
  58.            
  59.         okButton = (Button)  findViewById(R.id.WeatherSearch);   
  60.         textInput = (EditText) findViewById(R.id.TextWeather);   
  61.         imageView1 = (ImageView) findViewById(R.id.ImageView01);   
  62.         imageView2 = (ImageView) findViewById(R.id.ImageView02);   
  63.         textWeatherToday = (TextView) findViewById(R.id.WeatherToday);   
  64.         imageView3 = (ImageView) findViewById(R.id.ImageView03);   
  65.         imageView4 = (ImageView) findViewById(R.id.ImageView04);   
  66.         textWeatherTomorrow = (TextView) findViewById(R.id.WeatherTomorrow);   
  67.         imageView5 = (ImageView) findViewById(R.id.ImageView05);   
  68.         imageView6 = (ImageView) findViewById(R.id.ImageView06);   
  69.         textWeatherAfterday = (TextView) findViewById(R.id.WeatherAfterday);   
  70.         textWeatherCurrent = (TextView) findViewById(R.id.WeatherCurrent);   
  71.            
  72.         okButton.setOnClickListener(new Button.OnClickListener() {   
  73.             public void onClick(View v) {   
  74.                 showWeather();   
  75.             }   
  76.        });   
  77.     }   
  78.        
  79.     private void showWeather() {   
  80.         String city = textInput.getText().toString();   
  81.         if (city.length() == 0) city = "合肥";   
  82.         getWeather(city);   
  83.            
  84.         textWeatherToday.setText(getWeatherToday());   
  85.         imageView1.setImageResource(getIconToday(0));   
  86.         imageView2.setImageResource(getIconToday(1));   
  87.            
  88.         textWeatherTomorrow.setText(getWeatherTomorrow());         
  89.         imageView3.setImageResource(getIconTomorrow(0));   
  90.         imageView4.setImageResource(getIconTomorrow(1));   
  91.            
  92.         textWeatherAfterday.setText(getWeatherAfterday());         
  93.         imageView5.setImageResource(getIconAfterday(0));   
  94.         imageView6.setImageResource(getIconAfterday(1));   
  95.            
  96.         textWeatherCurrent.setText(getWeatherCurrent());   
  97.     }   
  98.        
  99.     public void getWeather(String cityName) {   
  100.         try {   
  101.             SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);   
  102.             rpc.addProperty("theCityName", cityName);   
  103.   
  104.             AndroidHttpTransport ht = new AndroidHttpTransport(URL);   
  105.             ht.debug = true;   
  106.   
  107.             SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(   
  108.                     SoapEnvelope.VER11);   
  109.                
  110.             envelope.bodyOut = rpc;   
  111.             envelope.dotNet = true;   
  112.             envelope.setOutputSoapObject(rpc);   
  113.   
  114.             ht.call(SOAP_ACTION, envelope);   
  115.                
  116.             debug(LOG_TAG, "DUMP>> " + ht.requestDump);   
  117.             debug(LOG_TAG, "DUMP<< " + ht.responseDump);   
  118.   
  119.             SoapObject result = (SoapObject) envelope.bodyIn;   
  120.              detail = (SoapObject) result.getProperty("getWeatherbyCityNameResult");   
  121.                
  122.             parseWeather(detail);   
  123.             return;   
  124.         } catch (Exception e) {   
  125.             e.printStackTrace();   
  126.         }   
  127.     }   
  128.        
  129.     private void parseWeather(SoapObject detail) {   
  130.         String date = detail.getProperty(6).toString();   
  131.         weatherToday = "今天:" + date.split(" ")[0];   
  132.         weatherToday = weatherToday + "\n天气:" + date.split(" ")[1];   
  133.         weatherToday = weatherToday + "\n气温:" + detail.getProperty(5).toString();   
  134.         weatherToday = weatherToday + "\n风力:" + detail.getProperty(7).toString() + "\n";   
  135.         iconToday[0] = parseIcon(detail.getProperty(8).toString());   
  136.         iconToday[1] = parseIcon(detail.getProperty(9).toString());   
  137.            
  138.         weatherCurrent = detail.getProperty(10).toString();   
  139.            
  140.         date = detail.getProperty(13).toString();   
  141.         weatherTomorrow = "明天:" + date.split(" ")[0];   
  142.         weatherTomorrow = weatherTomorrow + "\n天气:" + date.split(" ")[1];   
  143.         weatherTomorrow = weatherTomorrow + "\n气温:" + detail.getProperty(12).toString();   
  144.         weatherTomorrow = weatherTomorrow + "\n风力:" + detail.getProperty(14).toString() + "\n";   
  145.         iconTomorrow[0] = parseIcon(detail.getProperty(15).toString());   
  146.         iconTomorrow[1] = parseIcon(detail.getProperty(16).toString());   
  147.            
  148.         date = detail.getProperty(18).toString();   
  149.         weatherAfterday = "后天:" + date.split(" ")[0];   
  150.         weatherAfterday = weatherAfterday + "\n天气:" + date.split(" ")[1];   
  151.         weatherAfterday = weatherAfterday + "\n气温:" + detail.getProperty(17).toString();   
  152.         weatherAfterday = weatherAfterday + "\n风力:" + detail.getProperty(19).toString() + "\n";   
  153.         iconAfterday[0] = parseIcon(detail.getProperty(20).toString());   
  154.         iconAfterday[1] = parseIcon(detail.getProperty(21).toString());   
  155.     }   
  156.        
  157.     public String getWeatherToday() {   
  158.         debug(LOG_TAG, "weatherToday: " + weatherToday);   
  159.         return weatherToday;   
  160.     }   
  161.        
  162.     public String getWeatherTomorrow() {   
  163.         debug(LOG_TAG, "weatherTomorrow: " + weatherTomorrow);   
  164.         return weatherTomorrow;   
  165.     }   
  166.        
  167.     public String getWeatherAfterday() {   
  168.         debug(LOG_TAG, "weatherAfterday: " + weatherAfterday);   
  169.         return weatherAfterday;   
  170.     }   
  171.        
  172.     public String getWeatherCurrent() {   
  173.         debug(LOG_TAG, "weatherCurrent: " + weatherCurrent);   
  174.         return weatherCurrent;   
  175.     }   
  176.        
  177.     public int getIconToday (int index) {   
  178.         return iconToday[index];   
  179.     }   
  180.        
  181.     public int getIconTomorrow (int index) {   
  182.         return iconTomorrow[index];   
  183.     }   
  184.        
  185.     public int getIconAfterday (int index) {   
  186.         return iconAfterday[index];   
  187.     }   
  188.        
  189.     private int parseIcon(String strIcon) {   
  190.         if (strIcon == nullreturn -1;   
  191.            
  192.         if ("0.gif".equals(strIcon)) return R.drawable.a_0;   
  193.         if ("1.gif".equals(strIcon)) return R.drawable.a_1;   
  194.         if ("2.gif".equals(strIcon)) return R.drawable.a_2;   
  195.         if ("3.gif".equals(strIcon)) return R.drawable.a_3;   
  196.         if ("4.gif".equals(strIcon)) return R.drawable.a_4;   
  197.         if ("5.gif".equals(strIcon)) return R.drawable.a_5;   
  198.         if ("6.gif".equals(strIcon)) return R.drawable.a_6;   
  199.         if ("7.gif".equals(strIcon)) return R.drawable.a_7;   
  200.         if ("8.gif".equals(strIcon)) return R.drawable.a_8;   
  201.         if ("9.gif".equals(strIcon)) return R.drawable.a_9;   
  202.         if ("10.gif".equals(strIcon)) return R.drawable.a_10;   
  203.         if ("11.gif".equals(strIcon)) return R.drawable.a_11;   
  204.         if ("12.gif".equals(strIcon)) return R.drawable.a_12;   
  205.         if ("13.gif".equals(strIcon)) return R.drawable.a_13;   
  206.         if ("14.gif".equals(strIcon)) return R.drawable.a_14;   
  207.         if ("15.gif".equals(strIcon)) return R.drawable.a_15;   
  208.         if ("16.gif".equals(strIcon)) return R.drawable.a_16;   
  209.         if ("17.gif".equals(strIcon)) return R.drawable.a_17;   
  210.         if ("18.gif".equals(strIcon)) return R.drawable.a_18;   
  211.         if ("19.gif".equals(strIcon)) return R.drawable.a_19;   
  212.         if ("20.gif".equals(strIcon)) return R.drawable.a_20;   
  213.         if ("21.gif".equals(strIcon)) return R.drawable.a_21;   
  214.         if ("22.gif".equals(strIcon)) return R.drawable.a_22;   
  215.         if ("23.gif".equals(strIcon)) return R.drawable.a_23;   
  216.         if ("24.gif".equals(strIcon)) return R.drawable.a_24;   
  217.         if ("25.gif".equals(strIcon)) return R.drawable.a_25;   
  218.         if ("26.gif".equals(strIcon)) return R.drawable.a_26;   
  219.         if ("27.gif".equals(strIcon)) return R.drawable.a_27;   
  220.         if ("28.gif".equals(strIcon)) return R.drawable.a_28;   
  221.         if ("29.gif".equals(strIcon)) return R.drawable.a_29;   
  222.         if ("30.gif".equals(strIcon)) return R.drawable.a_30;   
  223.         if ("31.gif".equals(strIcon)) return R.drawable.a_31;   
  224.            
  225.         return 0;   
  226.     }   
  227.        
  228.     private static void debug(String tag, String msg) {   
  229.         if (DEBUG) Log.d(tag, msg);   
  230.     }   
  231.        
  232.     private void showAbout() {   
  233.         TextView textAbout = new TextView(this);   
  234.         textAbout.setText(R.string.about_text);   
  235.         textAbout.setMovementMethod(LinkMovementMethod.getInstance());   
  236.   
  237.         Dialog dlg = new AlertDialog.Builder(this)   
  238.             .setTitle(R.string.app_about)   
  239.             .setView(textAbout)   
  240.             .setPositiveButton(R.string.about_ok, new DialogInterface.OnClickListener() {   
  241.                 public void onClick(DialogInterface dialog, int whichButton) {   
  242.                 }   
  243.             })   
  244.             .create();   
  245.         dlg.show();   
  246.     }   
  247.     private void about_city(){   
  248.            
  249.         String result_city=detail.getProperty(11).toString();   
  250.         new AlertDialog.Builder(this).setTitle(textInput.getText().toString()).setMessage(result_city).setPositiveButton("OK"null).show();   
  251.            
  252.            
  253.     }   
  254.     public boolean onCreateOptionsMenu(Menu menu) {   
  255.         super.onCreateOptionsMenu(menu);   
  256.            
  257.         menu.add(0, SHOW_ABOUT, 0, R.string.app_about);   
  258.            
  259.         menu.add(0,2,1,R.string.city);   
  260.            
  261. //        menu.addSubMenu("jack").add("tom1")   
  262. //        .add("tom2");   
  263.         //menu.addS   
  264.         this.getMenuInflater().inflate(R.layout.menu, menu);   
  265.         return true;   
  266.   
  267.     }   
  268.   
  269.     public boolean onOptionsItemSelected(MenuItem item) {   
  270.         switch (item.getItemId()) {   
  271.         case SHOW_ABOUT:   
  272.             showAbout();   
  273.             break;   
  274.         case 2:about_city();   
  275.             break;   
  276.         }   
  277.         return true;   
  278.     }   
  279. }  
posted on 2011-03-02 20:44  唐朝  阅读(595)  评论(0编辑  收藏  举报