博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android webservice 成功版(帅哥)
阅读量:5161 次
发布时间:2019-06-13

本文共 3277 字,大约阅读时间需要 10 分钟。

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();
            }
}

转载于:https://www.cnblogs.com/xrtd/p/3870335.html

你可能感兴趣的文章
解析Ceph: RBDCache 背后的世界
查看>>
qt安装遇到的错误
查看>>
Linux下which、whereis、locate、find 区别
查看>>
h.264加权预测
查看>>
as4 通过yum自动升级实现
查看>>
学习springMVC实例1——配置和跳转到HelloWorld
查看>>
寻找完美平方数
查看>>
初学反编译-.-
查看>>
防御式编程
查看>>
单线程并发的server端
查看>>
View可以设置tag携带数据
查看>>
individual reading task ---12061183 叶露婷
查看>>
delphi的消息对话框
查看>>
java:Apache Shiro 权限管理
查看>>
38.输出1到最大的N位数[Print 1 to max number of N bits]
查看>>
ZOJ - 2165 Red and Black
查看>>
objective c的注释规范
查看>>
FreeNas安装配置使用
查看>>
机器学习中的F1-score
查看>>
编译安装php5.5.38
查看>>