我的Android进阶之旅

编程入门 行业动态 更新时间:2024-10-13 00:38:24

我的Android<a href=https://www.elefans.com/category/jswz/34/1769503.html style=进阶之旅"/>

我的Android进阶之旅

此app的实现功能如图所示:

注:.asmx是本文webservice的提供商

具体的用法见:.asmx?op=getMobileCodeInfo

以下是 SOAP 1.2 请求和响应示例。所显示的占位符需替换为实际值。

POST /WebServices/MobileCodeWS.asmx HTTP/1.1
Host: webservice.webxml
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="" xmlns:xsd="" xmlns:soap12=""><soap12:Body><getMobileCodeInfo xmlns="/"><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="" xmlns:xsd="" xmlns:soap12=""><soap12:Body><getMobileCodeInfoResponse xmlns="/"><getMobileCodeInfoResult>string</getMobileCodeInfoResult></getMobileCodeInfoResponse></soap12:Body>
</soap12:Envelope>

step1:创建android工程MobileAddressQuery,并设置网络访问权限。



step2:设计应用的UI界面和使用的字符串值

/res/values/string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources><string name="hello">Hello World, MainActivity!</string><string name="app_name">手机号码归属地查询</string><string name="mobile">手机号码</string><string name="button">归属地查询</string><string name="error">查询失败</string>
</resources>



布局 /res/layout/main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><TextView android:layout_width="fill_parent"android:layout_height="wrap_content" android:text="@string/mobile" /><EditText android:layout_width="fill_parent"android:layout_height="wrap_content" android:id="@+id/mobile" /><Button android:layout_width="fill_parent"android:layout_height="wrap_content" android:text="@string/button"android:onClick="query" /><!-- 指定button的响应方法为query --><TextView android:layout_width="fill_parent"android:layout_height="wrap_content" android:id="@+id/textView" />
</LinearLayout>


step3:在src目录下创建sop12.xml,并将网址文档中提供的代码复制其中,如下

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="" xmlns:xsd="" xmlns:soap12=""><soap12:Body><getMobileCodeInfo xmlns="/"><!-- $mobile只是展示如此,在程序调用此xml文件后会将用户输入的手机号码代替 --><mobileCode>$mobile</mobileCode><userID></userID></getMobileCodeInfo></soap12:Body>
</soap12:Envelope>

step4:业务类:cn.roco.address.AddressService

注意访问目标地址是:

.asmx

可以有协议中得到。

package cn.roco.address.service;import java.io.InputStream;
import java.HttpURLConnection;
import java.URL;import org.xmlpull.v1.XmlPullParser;import android.util.Xml;import cn.roco.utils.StreamTools;public class AddressService {/*** 用于获取手机号归属地  调用webservice* @param mobile  手机号* @return* @throws Exception*/public static String getAddress(String mobile) throws Exception {String soap = readSoap();/*** 正则表达式$为特殊正则中的特殊符号须转义,即\$mobile  *  而\为字符串中的特殊符号,所以用两个反斜杠,即"\\{1}quot;  */soap = soap.replaceAll("\\$mobile", mobile); //将用户输入的手机号码代替sop12.xml中的$mobilebyte[] entity = soap.getBytes();String path = ".asmx";  //webservice提供源HttpURLConnection connection = (HttpURLConnection) new URL(path).openConnection();connection.setConnectTimeout(5000);connection.setRequestMethod("POST");connection.setRequestProperty("Content-Type","application/soap+xml; charset=utf-8");connection.setRequestProperty("Content-Length",String.valueOf(entity.length));connection.setDoOutput(true);connection.getOutputStream().write(entity);if (connection.getResponseCode() == 200) {return parseSOAP(connection.getInputStream());}return null;}/*** 解析webservice返回的数据* <?xml version="1.0" encoding="utf-8"?><soap12:Envelope xmlns:xsi="" xmlns:xsd="" xmlns:soap12=""><soap12:Body><getMobileCodeInfoResponse xmlns="/"><getMobileCodeInfoResult>string</getMobileCodeInfoResult></getMobileCodeInfoResponse></soap12:Body></soap12:Envelope>* @param inputStream* @return * @throws Exception */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;}/*** 读取sop12.xml的内容* @return* @throws Exception*/private static String readSoap() throws Exception {InputStream inputStream = AddressService.class.getClassLoader().getResourceAsStream("sop12.xml");byte[] data = StreamTools.read(inputStream);return new String(data);}
}


step5:工具类cn.roco.utils.StreamTool

package cn.roco.utils;import java.io.ByteArrayOutputStream;
import java.io.InputStream;public class StreamTools {/*** 读取输入流中的数据* @param inputStream  输入流* @return   二进制的流数据* @throws Exception*/public static byte[] read(InputStream inputStream) throws Exception {ByteArrayOutputStream outputStream=new ByteArrayOutputStream();byte[] buffer=new byte[1024];int length=0;while((length=inputStream.read(buffer))!=-1){outputStream.write(buffer,0,length);}inputStream.close();return outputStream.toByteArray();}}

step6:cn.roco.address.MainActivity

package cn.roco.address;import cn.roco.address.service.AddressService;
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 mobileText;private TextView textView;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);mobileText = (EditText) this.findViewById(R.id.mobile);textView = (TextView) this.findViewById(R.id.textView);}public void query(View v) {String mobile=mobileText.getText().toString();try {String address=AddressService.getAddress(mobile);textView.setText(address);} catch (Exception e) {e.printStackTrace();Toast.makeText(getApplicationContext(), R.string.error, 1).show();}}
}


step7:AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""package="cn.roco.address" android:versionCode="1" android:versionName="1.0"><uses-sdk android:minSdkVersion="8" /><!-- 访问Internet权限 --><uses-permission android:name="android.permission.INTERNET"/><application android:icon="@drawable/icon" android:label="@string/app_name"><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><uses-library android:name="android.test.runner" /></application><instrumentation android:targetPackage="cn.roco.address"android:name="android.test.InstrumentationTestRunner" />
</manifest>



==================================================================================================

作者:欧阳鹏 欢迎转载,与人分享是进步的源泉!

转载请保留原文地址:

==================================================================================================


更多推荐

我的Android进阶之旅

本文发布于:2024-02-26 13:49:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1702729.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:进阶   之旅   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!