Android下get/post访问网络及webService的调用

本文介绍如何使用xutils库在Android应用中实现GET和POST请求,同时演示如何调用WebService获取手机号码归属地信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

       对于刚做开发或者是刚学android的兄弟(j2SE -> Android)来说访问网络还是比较痛苦的,因为需要考虑在子线程访问网络然后在主线程更新,正式开发还要考虑如何去维护创建的各个线程来确保整个app的性能稳定,还有WebService是啥呀,一问三不知,百度一下,什么soap协议 ,xml以及一堆的专业术语,当你研究完这些个名词之后,发现一个头两个大,更让人郁闷的是还没搞懂到底webService是个啥,到底咋个调用法?这里和大家分享下最直接的方法,使用xutils完成get/post请求以及webService的调用,快速达到开发要求。

这里以国内免费的webService为例,用于获取手机号码归属地

http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getMobileCodeInfo

        get方式获取手机归属地,打开方法调用页(上面的url),找到下面部分


从第一行:

      GET /WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode=string&userID=string HTTP/1.1
我们可以发现:方法名为getMobileCodeInfo,参数为mobileCode,&userID
因此我们现在就去调用它。
	public void get(View view) {
		
		HttpUtils http = new HttpUtils(10 * 1000);
		http.configCurrentHttpCacheExpiry(0);
		String url = String.format("http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode=%s&userID=" , "183xxxxxxxx" );
		http.send(HttpMethod.GET, url, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {//访问失败回调
				Log.d("get:" + arg1);
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {//访问成功回调
				Log.d("get:" + arg0.result);
			}
		});

	}
	打印结果为xml:	
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://WebXml.com.cn/">183xxxxxxxx:江苏 xx 江苏移动全球通卡</string>

          Post方式:在方法调用页找到下面部分:

	老规矩,上面是调用 , 下面是返回的数据,直接调用:
public void post(View view) {

		
		HttpUtils http = new HttpUtils(10 * 1000);
		http.configCurrentHttpCacheExpiry(0);
		String url = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";
		RequestParams params = new RequestParams();
		params.setContentType("application/x-www-form-urlencoded");//指定ContentType,这里不是一定要要,安全起见还是带上吧。
		params.addBodyParameter("mobileCode", "18352566805");
		params.addBodyParameter("userID", "");
		http.send(HttpMethod.POST, url, params, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				Log.d("post:" + arg1);
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				Log.d("post:" + arg0.result);
			}
		});
	}
返回数据:
 <?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://WebXml.com.cn/">183xxxxxxxx:江苏 xx 江苏移动全球通卡</string>

访问webService,老套路,方法调用页,找到以下部分:

上面部分是发送部分,下面是返回结果,不说废话了,webService调用如下:
public String getXml() throws IOException {
		InputStream in = this.getClassLoader().getResourceAsStream("mobileInfo.xml");
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		int len = 0;
		byte[] buffer = new byte[1024];
		while ((len = in.read(buffer)) != -1) {
			out.write(buffer, 0, len);
		}
		in.close();
		String result = new String(out.toByteArray(), "utf-8");
		out.close();
		return result;
	}

	public void login(View v) {

		HttpUtils http = new HttpUtils(10 * 1000);
		http.configCurrentHttpCacheExpiry(0);
		String xml = null;
		try {
			xml = getXml();
		} catch (IOException e) {
			e.printStackTrace();
		}

		xml = xml.replaceAll("\\$mobileCode", "18352566805");
		Log.d(TAG, xml);

		String url = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?op=getMobileCodeInfo";
		RequestParams params = new RequestParams();
		params.setContentType("application/soap+xml; charset=utf-8");
		try {
			params.setBodyEntity(new StringEntity(xml, "utf-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		http.send(HttpMethod.POST, url, params, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException e, String arg1) {
				e.printStackTrace();
				Log.d("失败" + arg1);
			}

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				Log.d(responseInfo.result);
			}
		});

	}

返回数据如下:
 <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><getMobileCodeInfoResponse xmlns="http://WebXml.com.cn/"><getMobileCodeInfoResult>183xxxxxxxx:江苏 xx 江苏移动全球通卡</getMobileCodeInfoResult></getMobileCodeInfoResponse></soap:Body></soap:Envelope>
好了,自己解析吧。
     细心的小伙伴肯定会问,为毛和get/post不一样,不要担心,且听我慢慢道来:
	get/post,是必须遵循http协议的,而webService在遵循http协议的同时,还需要遵循SOAP协议,因此调用方式略有不同,这里与网上其他的小伙伴使用ksoap2.jar调用webService的方式也不同,先说下ksoap2.jar的调用方式吧,这个工具老实来说已经听方便的了,但是在配置的时候比较麻烦,为什么呢?因为要配置namespace,methodname,bodyOut.......一步都不能错,比较烦人,我所采用的方法是保存要发送的数据格式为xml文件,然后直接读取这个文件替换其中的占位符,这样只要指定url就好。
	对了,使用xutils调用Webservice的时候要指定下contentType,不然是没办法访问到的。
        







评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值