- 浏览: 321342 次
最新评论
-
ax003d:
你好,我使用这种方法,在gridview里显示图片,当图片数量 ...
ScrollView嵌套GridView、ListView的解决办法 -
yunzhu:
Firefox下有Xmarks书签同步插件,很好用,可以方便地 ...
开发人员应该具备的工具----欲善其事,先利其器 -
mjlixm:
Android入门:ContentProvider 学习了! ...
Android入门:ContentProvider -
bluesky329:
很好收益匪浅!
一个软件工程师的经验之谈 -
qinglongyun:
每天工作4小时的程序员
发送XML
通过URL封装路径打开一个HttpURLConnection
设置请求方式,Content-Type和Content-Length
XML文件的Content-Type为:text/xml;charset=UTF-8
使用HttpURLConnection获取输出流输出数据
WebService
WebService是发布在网络上的API,可以通过发送XML调用,WebService返回结果也是XML数据
WebService没有语言限制,只要可以发送XML数据和接收XML数据即可
http://www.webxml.com.cn网站上提供了一些WebService服务,我们可以对其进行调用
http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getMobileCodeInfo中提供了电话归属地查询的使用说明
内容如下:
SOAP 1.2
以下是 SOAP 1.2 请求和响应示例。所显示的占位符需替换为实际值。
POST /WebServices/MobileCodeWS.asmx HTTP/1.1 Host: webservice.webxml.com.cn Content-Type: application/soap+xml; charset=utf-8 Content-Length: length <?xml version="1.0" encoding="utf-8"?> <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Body> <getMobileCodeInfo xmlns="http://WebXml.com.cn/"> <mobileCode>string</mobileCode> <userID>string</userID> </getMobileCodeInfo> </soap12:Body> </soap12:Envelope>
HTTP/1.1 200 OK Content-Type: application/soap+xml; charset=utf-8 Content-Length: length <?xml version="1.0" encoding="utf-8"?> <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Body> <getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/"> <getMobileCodeInfoResult>string</getMobileCodeInfoResult> </getMobileCodeInfoResponse> </soap12:Body> </soap12:Envelope>
HTTP GET
以下是 HTTP GET 请求和响应示例。所显示的占位符需替换为实际值。
GET /WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode=string&userID=string HTTP/1.1 Host: webservice.webxml.com.cn
HTTP/1.1 200 OK Content-Type: text/xml; charset=utf-8 Content-Length: length <?xml version="1.0" encoding="utf-8"?> <string xmlns="http://WebXml.com.cn/">string</string>
HTTP POST
以下是 HTTP POST 请求和响应示例。所显示的占位符需替换为实际值。
POST /WebServices/MobileCodeWS.asmx/getMobileCodeInfo HTTP/1.1 Host: webservice.webxml.com.cn Content-Type: application/x-www-form-urlencoded Content-Length: length mobileCode=string&userID=string
HTTP/1.1 200 OK Content-Type: text/xml; charset=utf-8 Content-Length: length <?xml version="1.0" encoding="utf-8"?> <string xmlns="http://WebXml.com.cn/">string</string>
下面为具体实例及代码:
界面显示:
向WebService发送的XML文件: send.xml(放置在SRC路径下)
Java代码
- <?xml version="1.0" encoding="utf-8"?>
- <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
- <soap12:Body>
- <getMobileCodeInfo xmlns="http://WebXml.com.cn/">
- <mobileCode>$number</mobileCode>
- <userID></userID>
- </getMobileCodeInfo>
- </soap12:Body>
- </soap12:Envelope>
布局文件man.xml
Java代码
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <EditText
- android:id="@+id/phoneET"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:inputType="phone" >
- <requestFocus />
- </EditText>
- <Button
- android:onClick="onClick"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="归属地查询" />
- <TextView
- android:id="@+id/locationTV"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="25sp"
- />
- </LinearLayout>
功能清单文件AndroidManifest.xml
Java代码
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.itheima.webservice"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk android:minSdkVersion="10" />
- <uses-permission android:name="android.permission.INTERNET"/>
- <instrumentation
- android:name="android.test.InstrumentationTestRunner"
- android:targetPackage="com.itheima.webservice"
- />
- <application
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name" >
- <uses-library android:name="android.test.runner" />
- <activity
- android:name=".MainActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
MainActivity:
Java代码
- package com.itheima.webservice;
- import com.itheima.webservice.service.NumberService;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- private EditText phoneET;
- private TextView locationTV;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- phoneET = (EditText) findViewById(R.id.phoneET);
- locationTV = (TextView) findViewById(R.id.locationTV);
- }
- public void onClick(View view){
- try {
- NumberService service = new NumberService();
- String num = phoneET.getText().toString();
- String loacation = service.getLocation(num);
- locationTV.setText(loacation);
- } catch (Exception e) {
- e.printStackTrace();
- Toast.makeText(getApplicationContext(), "查无此号", 1).show();
- }
- }
- }
NumberService:
Java代码
- package com.itheima.webservice.service;
- import java.io.InputStream;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import org.xmlpull.v1.XmlPullParser;
- import android.util.Xml;
- import com.itheima.webservice.util.StreamUtil;
- public class NumberService {
- public String getLocation(String number) throws Exception {
- // 读取本地准备好的文件, 用输入的号码替换原来的占位符
- InputStream in = NumberService.class.getClassLoader().getResourceAsStream("send.xml");
- byte[] data = StreamUtil.load(in);
- String content = new String(data);
- content = content.replace("$number", number);
- // 创建连接对象, 设置请求头, 按照Webservice服务端提供的要求来设置
- URL url = new URL("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx");
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setConnectTimeout(5000);
- conn.setRequestProperty("Host", "webservice.webxml.com.cn");
- conn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8");
- conn.setRequestProperty("Content-Length", content.getBytes().length + "");
- conn.setRequestMethod("POST");
- // 输出数据
- conn.setDoOutput(true);
- conn.getOutputStream().write(content.getBytes());
- // // 获取服务端传回的数据, 解析XML, 得到结果
- XmlPullParser parser = Xml.newPullParser();
- parser.setInput(conn.getInputStream(), "UTF-8");
- for (int type = parser.getEventType();type!=XmlPullParser.END_DOCUMENT;type=parser.next())
- if(type==XmlPullParser.START_TAG&&parser.getName().equals("getMobileCodeInfoResult")){
- return parser.nextText();
- }
- return "没有找到此号码";
- }
- }
读取输入流工具类StreamUtil:
Java代码
- package com.itheima.webservice.util;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- public class StreamUtil {
- public static byte[] load(InputStream in ) throws IOException{
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- byte b [] = new byte[1024];
- int len = -1;
- while((len=in.read(b))!=-1){
- baos.write(b,0,len);
- }
- baos.close();
- return baos.toByteArray();
- }
- }
发表评论
-
Android入门:ContentProvider
2012-07-02 00:06 1884一、ContentProvider介绍 Cont ... -
Android入门:监听ContentProvider数据改变
2012-07-02 00:06 1394一、监听ContentProvider主要步骤 1 ... -
android 使用广播接收者监听短信和拦截外拨电话
2012-06-27 23:31 2181如果你想监听自己或者别人接收到的短信,设置黑名单等功能, ... -
android 广播接收者--BroadcastReceiver
2012-06-27 23:30 3504BroadcastReceiver初识: ... -
一个项目只使用一个Activity初探
2012-06-27 23:29 936提供一个简单案例: 1,新建一个项目 修改和添加 ... -
Android开发从入门到精通(8) _9
2012-04-24 21:45 2423Spinner 第八章(9) 在本节中将为Spinne ... -
Android开发从入门到精通(8) _8
2012-04-24 21:44 933RadioGroup 第八章(8) 在本章中将为Rad ... -
Android开发从入门到精通(8) _7
2012-04-24 21:43 909EditText 第八章(7) 在本节中,和上一节很类 ... -
Android开发从入门到精通(8) _6
2012-04-24 21:43 837CheckBox 第八章(6) 在本节中,将为Chec ... -
Android开发从入门到精通(8) _5
2012-04-24 21:42 869按钮 第八章(5) 看看下面的代码。这段代码代表了四个 ... -
Android开发从入门到精通(8) _4
2012-04-23 22:18 1003为AutoComplete创建一个 ... -
Android开发从入门到精通(8) _3
2012-04-23 22:17 1123使用菜单 第八章(3) 在本节中,你将建造一个应用 ... -
Android开发从入门到精通(8) _2
2012-04-23 22:17 1006修改AndroidManifest.xml文件 第八章( ... -
Android开发从入门到精通(8) _1
2012-04-23 22:16 827列表,菜单和其它Views 第八章(1) 关键技能 & ... -
Android开发从入门到精通(7) _8
2012-04-23 22:16 1061试试这个:修改AndoridPhoneDialer项目 ... -
Android开发从入门到精通(7) _7
2012-04-22 15:14 930执行一个EditText View 第七章(7) ... -
Android开发从入门到精通(7) _6
2012-04-22 15:13 1118修改AndroidPhoneDialer 第七章(6) ... -
Android开发从入门到精通(7) _5
2012-04-22 15:13 896编辑活动许可 第七章(5) 大多数的Activit ... -
Android开发从入门到精通(7) _4
2012-04-22 15:12 974在本节中你将会 ... -
Android开发从入门到精通
2012-04-21 12:46 872从你的活动中打出电话 第七章(4) 在本节中你将会 ...
相关推荐
Android中调用Webservice实现号码归属地查询案例!
android基于webservice的手机号码查询功能,自己学习的时候写的小例子,简单易懂!android基于webservice的手机号码查询功能,自己学习的时候写的小例子,简单易懂!
在android端使用了soap协议来调用网上的webservice接口,来获得手机号码的归属地信息
C# 调用WebService查询手机号码归属地
CSharp调用WebService实现手机号码归属地查询
我第一次使用webservice传输数据的时候用来参考的例子,里面代码很清晰,适合做参考,而且实现了号码归属地的查询,代码简洁。
Android调用webservice查询手机归属地信息,可以直接运行,非常的简单好用
Android访问webservice
Android平台之Webservice详解(超详细),Android平台之Webservice详解(超详细),Android平台之Webservice详解(超详细)
Android通过IIS搭建WebService访问 SQLServer,实现增、删、查
在Android中访问WebService接口
android webserviceandroid webserviceandroid webservice
本程序代码是利用HTTP协议发送XML数据并调用webservice(soap),来实现手机号码的查询,是一个简单的应用程序,适合Android开发程序员。
android调用.net的webservice的例子获取手机的归属地
自动查询手机号码归属地程序,用C#编写,程序通过soap调用Webservice查询,数据库中已经有130-189号段的归属看信息。运行时记得还原数据库哦
Android开发之WebService介绍
qt5.12.0 和 webservice 的demo,通过http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl 查询手机号码归属, 简单有效
android 调用.net webservice
delphi XE5 ANDROID平台 调用 webservice并访问操作MSSQL数据库
比较适合android 刚入门的小白,需要调用webService,此demo能迅速带你入门