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