zoukankan      html  css  js  c++  java
  • android webservice 成功版(帅哥)

    package com.ts.xtweatherreport01;
    import java.io.UnsupportedEncodingException;

    import android.annotation.SuppressLint;
    import android.annotation.TargetApi;
    import android.app.Activity;
    import android.os.Build;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.HandlerThread;
    import android.os.Message;
    import android.os.StrictMode;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;

    import org.ksoap2.SoapEnvelope;
    import org.ksoap2.serialization.SoapObject;
    import org.ksoap2.serialization.SoapSerializationEnvelope;
    //import org.ksoap2.transport.AndroidHttpTransport;
    import org.ksoap2.transport.HttpTransportSE;





    public class TSMainActivity extends Activity 
    {
        
        private Handler handler = null;

        
        
        private Button okButton;
        private TextView v;
        

        
        private static final String NAMESPACE = "http://WebXml.com.cn/";
        
        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 SoapObject detail;
        
        @Override

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

        
            
            v=(TextView)this.findViewById(R.id.textView2);
            okButton = (Button) this.findViewById(R.id.button1);
            okButton.setOnClickListener(new Button.OnClickListener() {
                @Override
                public void onClick(View v) {
                    
                        HandlerThread handlerThread = new HandlerThread("myHandlerThread");  
                        handlerThread.start();  
                        handler = new Handler(handlerThread.getLooper());  
                        handler.post(new MyRunnable());  
      
                }

            });
        }

        private class MyRunnable implements Runnable {  
            public void run() {  
            
                try {  
                    Thread.sleep(0);  
                    
                    String city = "北京";
                    getWeather(city); 
                    
                } catch (InterruptedException e) {             
                    e.printStackTrace();  
                }  
            }  
        }  

        public void getWeather(String cityName)
        {

                System.out.println("rpc------");
                SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME);
                System.out.println("rpc" + rpc);
                System.out.println("cityName is " + cityName);
                rpc.addProperty("theCityName", cityName);
                
                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);
                envelope.bodyOut = rpc;
                envelope.dotNet = true;
                envelope.setOutputSoapObject(rpc);
                
                HttpTransportSE ht = new HttpTransportSE(URL);

            
                ht.debug = true;
                try
                {
                ht.call(SOAP_ACTION, envelope);


                detail =(SoapObject) envelope.getResponse();
                
        
                System.out.println("detail" + detail);
                Toast.makeText(this, detail.toString(), Toast.LENGTH_LONG).show();
                v.setText(detail.toString());
                parseWeather(detail);

                return;
            } 
                catch (Exception e) 
                {
                e.printStackTrace();
                }
        }

        private void parseWeather(SoapObject detail)
                throws UnsupportedEncodingException 
                {
            String date = detail.getProperty(6).toString();
            weatherToday = "今天:" + date.split(" ")[0];
            weatherToday = weatherToday + " 天气:" + date.split(" ")[1];
            weatherToday = weatherToday + " 气温:"
                    + detail.getProperty(5).toString();
            weatherToday = weatherToday + " 风力:"
                    + detail.getProperty(7).toString() + " ";
            System.out.println("weatherToday is " + weatherToday);
            Toast.makeText(TSMainActivity.this, weatherToday, Toast.LENGTH_LONG).show();

                }
    }

  • 相关阅读:
    Alwayson常用脚本
    SQL Server 编译缓存相关的知识点
    Alwayson 常用视图
    Analysis Services PowerShell
    visible:hidden和dispaly:none的区别
    问题:做EsayUI分页报错 $(...).pagination is not a function之后我把<jsp:include page="top.jsp"/>去掉就好了,有大神知道为什么吗?另外分页按键放在那里好些,我放到form表单下,就开始显示,点一下后就没有了
    MyBatis:统计数量(查询所有)
    attr与prop的区别
    JAVA_OPTS
    JVM参数设置
  • 原文地址:https://www.cnblogs.com/xrtd/p/3870335.html
Copyright © 2011-2022 走看看