码迷,mamicode.com
首页 > 移动开发 > 详细

android webservice 成功版(帅哥)

时间:2014-07-27 09:42:32      阅读:303      评论:0      收藏:0      [点我收藏+]

标签:

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 + "\n天气:" + date.split(" ")[1];
        weatherToday = weatherToday + "\n气温:"
                + detail.getProperty(5).toString();
        weatherToday = weatherToday + "\n风力:"
                + detail.getProperty(7).toString() + "\n";
        System.out.println("weatherToday is " + weatherToday);
        Toast.makeText(TSMainActivity.this, weatherToday, Toast.LENGTH_LONG).show();

            }
}

android webservice 成功版(帅哥)

标签:

原文地址:http://www.cnblogs.com/xrtd/p/3870335.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!