WebService(手机号码归属地查询和天气查询)
2013-11-14 12:33
423 查看
简介
Web ServiceWeb service是一个平台独立的,松耦合的,自包含的、基于可编程的web的应用程序,可使用开放的XML标准来描述、发布、发现、协调和配置这些应用程序,用于开发分布式的互操作的应用程序
1.例子:使用WebService电话号码归属地查询
http://www.webxml.com.cn/zh_cn/index.aspx 网站有电话号码归属地查询
WebService API ,只需要在实现这API就可以了
(1)界面配置文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/phone" /> <EditText android:id="@+id/phonenum" android:layout_width="fill_parent" android:layout_height="wrap_content" android:inputType="phone" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/search" /> <TextView android:id="@+id/result" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
(2)AndroidInteractWithWebService.xml
<?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>$mobile</mobileCode> <userID></userID> </getMobileCodeInfo> </soap12:Body> </soap12:Envelope>
(3)StreamTool.java
package com.webservice.tool; import java.io.ByteArrayOutputStream; import java.io.InputStream; public class StreamTool { /** * 从流中读取数据 * * @param inStream * @return */ public static byte[] read(InputStream inStream) throws Exception { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = inStream.read(buffer)) != -1) { outputStream.write(buffer, 0, len); } inStream.close(); return outputStream.toByteArray(); } }
(5)WebServiceRequestFromAndroid.java
package com.webservice.service; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import org.xmlpull.v1.XmlPullParser; import com.webservice.tool.StreamTool; import android.util.Xml; public class WebServiceRequestFromAndroid { /** * 获取手机号归属地 * * @param mobile * 手机号 * @return * @throws Exception */ public static String getAddress(String mobile) throws Exception { String soap = readSoap(); soap = soap.replaceAll("\\$mobile", mobile); byte[] entity = soap.getBytes(); String path = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx"; HttpURLConnection conn = (HttpURLConnection) new URL(path) .openConnection(); conn.setConnectTimeout(50000); conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8"); conn.setRequestProperty("Content-Length", String.valueOf(entity.length)); conn.getOutputStream().write(entity); if (conn.getResponseCode() == 200) { return parseSOAP(conn.getInputStream()); } return null; } /* * <?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> */ private static String parseSOAP(InputStream xml) throws Exception { XmlPullParser pullParser = Xml.newPullParser(); pullParser.setInput(xml, "UTF-8"); int event = pullParser.getEventType(); while (event != XmlPullParser.END_DOCUMENT) { switch (event) { case XmlPullParser.START_TAG: if ("getMobileCodeInfoResult".equals(pullParser.getName())) { return pullParser.nextText(); } break; } event = pullParser.next(); } return null; } private static String readSoap() throws Exception { InputStream inStream = WebServiceRequestFromAndroid.class .getClassLoader().getResourceAsStream( "AndroidInteractWithWebService.xml"); byte[] data = StreamTool.read(inStream); return new String(data); } }
(6)MainActivity.java
package com.example.webservice; import com.webservice.service.WebServiceRequestFromAndroid; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private Button button = null; private EditText phone = null; private static TextView resultText = null; private final int SUCCESS = 1; private final int ERROR = 1; private String result; private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == SUCCESS) { String r = (String) msg.obj; resultText.setText(r); } else if (msg.what == ERROR) { Toast.makeText(MainActivity.this, "服务器繁忙", 2).show(); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.button); phone = (EditText) findViewById(R.id.phonenum); resultText = (TextView) findViewById(R.id.result); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { final String phoneNum = phone.getText().toString(); if (!phoneNum.equals("")) { new Thread() { public void run() { try { result = WebServiceRequestFromAndroid .getAddress(phoneNum); Message msg = new Message(); msg.what = SUCCESS; msg.obj = result; handler.sendMessage(msg); } catch (Exception e) { Message msg = new Message(); msg.what = ERROR; handler.sendMessage(msg); } } }.start(); } else { Toast.makeText(MainActivity.this, "请输入手机号", 2).show(); } } }); } }
2.天气查询
(1)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/address" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <EditText android:id="@+id/phonenum" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="200" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/search" /> </LinearLayout> <ScrollView android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/result" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </ScrollView> </LinearLayout>
(2)AndroidInteractWithWebService.xml
<?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> <getWeather xmlns="http://WebXml.com.cn/"> <theCityCode>$city</theCityCode> <theUserID></theUserID> </getWeather> </soap12:Body> </soap12:Envelope>
(3)StreamTool.java
package com.webservice.tool; import java.io.ByteArrayOutputStream; import java.io.InputStream; public class StreamTool { /** * 从流中读取数据 * * @param inStream * @return */ public static byte[] read(InputStream inStream) throws Exception { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = inStream.read(buffer)) != -1) { outputStream.write(buffer, 0, len); } inStream.close(); return outputStream.toByteArray(); } }
(4)WebServiceRequestFromAndroid.java
package com.webservice.service; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; import java.util.List; import org.xmlpull.v1.XmlPullParser; import com.webservice.tool.StreamTool; import android.util.Xml; import android.widget.AbsoluteLayout; public class WebServiceRequestFromAndroid { /** * 获取手机号归属地 * * @param mobile * 手机号 * @return * @throws Exception */ public static String getAddress(String city) throws Exception { String soap = readSoap(); soap = soap.replaceAll("\\$city", city); byte[] entity = soap.getBytes(); String path = "http://webservice.webxml.com.cn//WebServices/WeatherWS.asmx"; HttpURLConnection conn = (HttpURLConnection) new URL(path) .openConnection(); conn.setConnectTimeout(50000); conn.setRequestMethod("POST"); conn.setDoOutput(true); conn.setRequestProperty("Content-Type", "application/soap+xml; charset=utf-8"); conn.setRequestProperty("Content-Length", String.valueOf(entity.length)); conn.getOutputStream().write(entity); if (conn.getResponseCode() == 200) { String result = parseSOAP(conn.getInputStream()); String weather = result.replace("1.gif", ""); return weather; } return null; } private static String parseSOAP(InputStream xml) throws Exception { String result = ""; XmlPullParser pullParser = Xml.newPullParser(); pullParser.setInput(xml, "UTF-8"); int event = pullParser.getEventType(); while (event != XmlPullParser.END_DOCUMENT) { switch (event) { case XmlPullParser.START_TAG: if ("string".equals(pullParser.getName())) { result += pullParser.nextText() + "\n"; } break; } event = pullParser.next(); } return result; } private static String readSoap() throws Exception { InputStream inStream = WebServiceRequestFromAndroid.class .getClassLoader().getResourceAsStream( "AndroidInteractWithWebService.xml"); byte[] data = StreamTool.read(inStream); return new String(data); } }
(5)MainActivity.java
package com.example.webservice; import com.webservice.service.WebServiceRequestFromAndroid; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private Button button = null; private EditText phone = null; private static TextView resultText = null; private final int SUCCESS = 1; private final int ERROR = 1; private String result; private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == SUCCESS) { String r = (String) msg.obj; resultText.setText(r); } else if (msg.what == ERROR) { Toast.makeText(MainActivity.this, "服务器繁忙", 2).show(); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.button); phone = (EditText) findViewById(R.id.phonenum); resultText = (TextView) findViewById(R.id.result); phone.setText("清远"); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { final String phoneNum = phone.getText().toString(); if (!phoneNum.equals("")) { new Thread() { public void run() { try { result = WebServiceRequestFromAndroid .getAddress(phoneNum); Message msg = new Message(); msg.what = SUCCESS; msg.obj = result; handler.sendMessage(msg); } catch (Exception e) { Message msg = new Message(); msg.what = ERROR; handler.sendMessage(msg); } } }.start(); } else { Toast.makeText(MainActivity.this, "请输入城市", 2).show(); } } }); } }
相关文章推荐
- WebService开发--手机号码归属地查询和天气预报查询
- C#添加WebService服务实现天气预报(查询)
- [Webservice]关于天气预报查询软件的查询原理(一)
- Android通过WebService进行网络编程------查询天气
- 转汇集各种 webservice工厂,快递,ip,天气,身份证,手机,翻译,火车时刻,股票,邮编,二维码,公交,ISBN,ICP 查询接口 API
- Java调用免费的WebService示例【天气预报】【国内手机号码归属地查询】
- 调用天气查询webservice服务的方法
- webservices系列(三)——调用线上webservice(天气预报和号码查询)
- 汇集各种 webservice工厂,快递,ip,天气,身份证,手机,翻译,火车时刻,股票,邮编,二维码,公交,ISBN,ICP 查询接口 API
- 利用WebService制作天气预报查询
- WebService查询天气
- 调用天气查询webservice服务的两种基本方法
- 利用国家气象局的webservice查询天气预报(转载)
- axis2通过城市名称调用.net写的asmx WebService查询天气实例
- android调用Webservice——天气情况查询(更新,显示自己想显示的信息)
- axis2通过城市名称调用.net写的asmx WebService查询天气实例 .
- WebService调用网络查询天气
- webservice查询天气
- webService调用 查询手机号码和天气
- jquery这个webservice根据选择城市查询天气