收藏 分销(赏)

Android访问phpwebservice简单一例.doc

上传人:二*** 文档编号:4521813 上传时间:2024-09-26 格式:DOC 页数:5 大小:299KB
下载 相关 举报
Android访问phpwebservice简单一例.doc_第1页
第1页 / 共5页
亲,该文档总共5页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

1、.假如是PHP做的效劳端,而咱们要用android去访咨询,如何办?所以能够用REST,但也能够用点笨的办法,比方能够让PHP的效劳端前往JSON或XML数据,而Android端那么能够用APACHE的client去访咨询。上面是一个例子,假定数据表中users表有如下字段(mysql):idusers,UserName,FullName加点数据,而后在效劳端树破一个据,如下:webservice1.php,感化是直截了当前往效劳端数据库的数$post);161718/*json格局*/19if($format=json)20header(Content-type:application/js

2、on);21echojson_encode(array(posts=$posts);2223else24header(Content-type:text/xml);25echo;26foreach($postsas$index=$post)27if(is_array($post)28foreach($postas$key=$value)29echo;30if(is_array($value)31foreach($valueas$tag=$val)32echo,htmlentities($val),;333435echo;36373839echo;404142434445那么能够把数据表输入为J

3、SON或许XML格局了,客户真个Android挪用:try12ParamsParams=newBasicParams();ConnectionParams.setConnectionTimeout(Params,3TIMEOUT_MILLISEC);4ConnectionParams.setSoTimeout(Params,TIMEOUT_MILLISEC);56Paramsp=newBasicParams();p.setParameter(user,1);7Clientclient=newStringurl=DefaultClient(p);8910:/10.0.2.2:8082/myphp

4、/phpWebservice/webservice1.phpuser=1&format=json;Postpost=newtryPost(url);111Log.i(getClass().getSimpleName(),sendtask-start);ListnameValuePairs=newArrayList(2);21nameValuePairs.add(newBasicNameValuePair(user,1);post.setEntity(newUrlEncodedFormEntity(nameValuePairs);314151ResponseHandlerresponseHand

5、ler=newBasicResponseHandler();StringresponseBody=client.execute(post,responseHandler);/剖析JSON前往的JSONObjectjson=newJSONObject(responseBody);JSONArrayjArray=json.getJSONArray(posts);ArrayListHashMapmylist=new6ArrayListHashMap();for(inti=0;ijArray.length();i+)1HashMapmap=newHashMap();7JSONObjecte=jArra

6、y.getJSONObject(i);1Strings=e.getString(post);81JSONObjectjObject=newJSONObject(s);map.put(idusers,jObject.getString(idusers);9map.put(UserName,jObject.getString(UserName);2map.put(FullName,jObject.getString(FullName);0mylist.add(map);212Toast.makeText(this,responseBody,Toast.LENGTH_LONG).show();223

7、24252627282930313233343536373839再搞个webservice2.php,该文件用来承受并保管客户端传递过去的JSON数据。12UserName.,.$obj-FullName.);mysql_close($con);101112$posts=array(1);1314151617header(Content-type:application/json);echojson_encode(array(posts=$posts);而Android客户端,能够结构JSON,发送到webservice2.phptry1JSONObjectjson=newJSONObject

8、();2json.put(UserName,test2);3json.put(FullName,1234567);4ParamsParams=newBasicParams();5ConnectionParams.setConnectionTimeout(Params,6TIMEOUT_MILLISEC);7ConnectionParams.setSoTimeout(Params,TIMEOUT_MILLISEC);8Clientclient=newDefaultClient(Params);910Stringurl=11:/10.0.2.2:8082/myphp/phpWebservice/w

9、ebservice2.php12Postrequest=newPost(url);13request.setEntity(new;14ByteArrayEntity(json.toString().getBytes(UTF8);15request.setHeader(json,json.toString();16Responseresponse=client.execute(request);17Entityentity=response.getEntity();18if(entity!=null)19InputStreaminstream=entity.getContent();20Stringresult=RestClient.convertStreamToString(instream);21Log.i(Readfromserver,result);22Toast.makeText(this,result,23Toast.LENGTH_LONG).show();2425

展开阅读全文
相似文档                                   自信AI助手自信AI助手
猜你喜欢                                   自信AI导航自信AI导航
搜索标签

当前位置:首页 > 教育专区 > 初中其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2024 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服