代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
访问webservice
作者:
千如
/ 发布于
2015/5/25
/
590
package com.android; import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.AndroidHttpTransport; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.os.Bundle; import android.text.method.LinkMovementMethod; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; public class MainActivity extends Activity { private static String LOG_TAG = "Weather"; private static boolean DEBUG = false; private static final int SHOW_ABOUT = 0x0001; private static final String NAMESPACE = "http://WebXml.com.cn/"; //WebService地址 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 String weatherTomorrow; private String weatherAfterday; private String weatherCurrent; private int iconToday[] = new int[2]; private int iconTomorrow[] = new int[2]; private int iconAfterday[] = new int[2]; private Button okButton; private EditText textInput; private ImageView imageView1; private ImageView imageView2; private TextView textWeatherToday; private ImageView imageView3; private ImageView imageView4; private TextView textWeatherTomorrow; private ImageView imageView5; private ImageView imageView6; private TextView textWeatherAfterday; private TextView textWeatherCurrent; private SoapObject detail; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); okButton = (Button) findViewById(R.id.WeatherSearch); textInput = (EditText) findViewById(R.id.TextWeather); imageView1 = (ImageView) findViewById(R.id.ImageView01); imageView2 = (ImageView) findViewById(R.id.ImageView02); textWeatherToday = (TextView) findViewById(R.id.WeatherToday); imageView3 = (ImageView) findViewById(R.id.ImageView03); imageView4 = (ImageView) findViewById(R.id.ImageView04); textWeatherTomorrow = (TextView) findViewById(R.id.WeatherTomorrow); imageView5 = (ImageView) findViewById(R.id.ImageView05); imageView6 = (ImageView) findViewById(R.id.ImageView06); textWeatherAfterday = (TextView) findViewById(R.id.WeatherAfterday); textWeatherCurrent = (TextView) findViewById(R.id.WeatherCurrent); okButton.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { showWeather(); } }); } private void showWeather() { String city = textInput.getText().toString(); if (city.length() == 0) city = "合肥"; getWeather(city); textWeatherToday.setText(getWeatherToday()); imageView1.setImageResource(getIconToday(0)); imageView2.setImageResource(getIconToday(1)); textWeatherTomorrow.setText(getWeatherTomorrow()); imageView3.setImageResource(getIconTomorrow(0)); imageView4.setImageResource(getIconTomorrow(1)); textWeatherAfterday.setText(getWeatherAfterday()); imageView5.setImageResource(getIconAfterday(0)); imageView6.setImageResource(getIconAfterday(1)); textWeatherCurrent.setText(getWeatherCurrent()); } public void getWeather(String cityName) { try { SoapObject rpc = new SoapObject(NAMESPACE, METHOD_NAME); rpc.addProperty("theCityName", cityName); AndroidHttpTransport ht = new AndroidHttpTransport(URL); ht.debug = true; SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11); envelope.bodyOut = rpc; envelope.dotNet = true; envelope.setOutputSoapObject(rpc); ht.call(SOAP_ACTION, envelope); debug(LOG_TAG, "DUMP>> " + ht.requestDump); debug(LOG_TAG, "DUMP<< " + ht.responseDump); SoapObject result = (SoapObject) envelope.bodyIn; detail = (SoapObject) result.getProperty("getWeatherbyCityNameResult"); parseWeather(detail); return; } catch (Exception e) { e.printStackTrace(); } } private void parseWeather(SoapObject detail) { 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"; iconToday[0] = parseIcon(detail.getProperty(8).toString()); iconToday[1] = parseIcon(detail.getProperty(9).toString()); weatherCurrent = detail.getProperty(10).toString(); date = detail.getProperty(13).toString(); weatherTomorrow = "明天:" + date.split(" ")[0]; weatherTomorrow = weatherTomorrow + "/n天气:" + date.split(" ")[1]; weatherTomorrow = weatherTomorrow + "/n气温:" + detail.getProperty(12).toString(); weatherTomorrow = weatherTomorrow + "/n风力:" + detail.getProperty(14).toString() + "/n"; iconTomorrow[0] = parseIcon(detail.getProperty(15).toString()); iconTomorrow[1] = parseIcon(detail.getProperty(16).toString()); date = detail.getProperty(18).toString(); weatherAfterday = "后天:" + date.split(" ")[0]; weatherAfterday = weatherAfterday + "/n天气:" + date.split(" ")[1]; weatherAfterday = weatherAfterday + "/n气温:" + detail.getProperty(17).toString(); weatherAfterday = weatherAfterday + "/n风力:" + detail.getProperty(19).toString() + "/n"; iconAfterday[0] = parseIcon(detail.getProperty(20).toString()); iconAfterday[1] = parseIcon(detail.getProperty(21).toString()); } public String getWeatherToday() { debug(LOG_TAG, "weatherToday: " + weatherToday); return weatherToday; } public String getWeatherTomorrow() { debug(LOG_TAG, "weatherTomorrow: " + weatherTomorrow); return weatherTomorrow; } public String getWeatherAfterday() { debug(LOG_TAG, "weatherAfterday: " + weatherAfterday); return weatherAfterday; } public String getWeatherCurrent() { debug(LOG_TAG, "weatherCurrent: " + weatherCurrent); return weatherCurrent; } public int getIconToday (int index) { return iconToday[index]; } public int getIconTomorrow (int index) { return iconTomorrow[index]; } public int getIconAfterday (int index) { return iconAfterday[index]; } private 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; } private static void debug(String tag, String msg) { if (DEBUG) Log.d(tag, msg); } private void showAbout() { TextView textAbout = new TextView(this); textAbout.setText(R.string.about_text); textAbout.setMovementMethod(LinkMovementMethod.getInstance()); Dialog dlg = new AlertDialog.Builder(this) .setTitle(R.string.app_about) .setView(textAbout) .setPositiveButton(R.string.about_ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { } }) .create(); dlg.show(); } private void about_city(){ String result_city=detail.getProperty(11).toString(); new AlertDialog.Builder(this).setTitle(textInput.getText().toString()).setMessage(result_city).setPositiveButton("OK", null).show(); } public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); menu.add(0, SHOW_ABOUT, 0, R.string.app_about); menu.add(0,2,1,R.string.city); // menu.addSubMenu("jack").add("tom1") // .add("tom2"); //menu.addS this.getMenuInflater().inflate(R.layout.menu, menu); return true; } public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case SHOW_ABOUT: showAbout(); break; case 2:about_city(); break; } return true; } }
试试其它关键字
访问
webservice
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
千如
贡献的其它代码
(
9
)
.
Android发送接收短信
.
Android打电话
.
用户注册的Activity
.
动态添加删除Spinner
.
记事本程序
.
取得已安装程序列表
.
访问webservice
.
html5中页面拨打电话的方式
.
如何获wifi路由器的BSSID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3