资源描述
.
假如是PHP做的效劳端,而咱们要用
android去访咨询,如何办?所以能够用
REST,但也
能够用点笨的办法,比方能够让
PHP的效劳端前往JSON或XML数据,而Android端那么
能够用APACHE的client
去访咨询。
上面是一个例子,假定数据表中
users表有如下字段(mysql):
idusers,UserName,FullName
加点数据,而后在效劳端树破一个
据,如下:
webservice1.php
,感化是直截了当前往效劳端数据库的数
<php
1
2if(isset($_GET['user'])&&intval($_GET['user'])){
$format=strtolower($_GET['format'])=='json''json':'xml';//xml
3
4isthedefault
$user_id=intval($_GET['user']);//nodefault
5
/*衔接数据库*/
6
$link=mysql_connect('localhost','root','xxxxx')or
die('Cannot
7
connecttotheDB');
8
9mysql_select_db('jsonandroid',$link)or
10$query="SELECT*FROM'users'";
11$result=mysql_query($query,$link);
12$posts=array();
die('CannotselecttheDB');
13if(mysql_num_rows($result)){
14while($post=mysql_fetch_assoc($result)){
15$posts[]=array('post'=>$post);
16}
17}
18/*json格局*/
19if($format=='json'){
20header('Content-type:application/json');
21echojson_encode(array('posts'=>$posts));
22}
23else{
24header('Content-type:text/xml');
25echo'<posts>';
26foreach($postsas$index=>$post){
27if(is_array($post)){
28foreach($postas$key=>$value){
29echo'<',$key,'>';
30if(is_array($value)){
31foreach($valueas$tag=>$val){
32echo'<',$tag,'>',htmlentities($val),'</',$tag,'>';
33}
34}
35echo'</',$key,'>';
36}
37}
38}
39echo'</posts>';
40}
41}
42>
43
44
45
那么能够把数据表输入为
JSON或许XML格局了,客户真个Android挪用:
try{
1
2ParamsParams=newBasicParams();
ConnectionParams.setConnectionTimeout(Params,
3
TIMEOUT_MILLISEC);
4
ConnectionParams.setSoTimeout(Params,TIMEOUT_MILLISEC);
5
6Paramsp=newBasicParams();
p.setParameter("user","1");
7
Clientclient=new
Stringurl=
DefaultClient(p);
8
9
1
0
"://10.0.2.2:8082/myphp/phpWebservice/webservice1.phpuser=1&fo
rmat=json";
Postpost=new
try{
Post(url);
1
1
1
Log.i(getClass().getSimpleName(),"sendtask-start");
List<NameValuePair>nameValuePairs=newArrayList<NameValuePair>(2);
2
1nameValuePairs.add(newBasicNameValuePair("user","1"));
post.setEntity(new
UrlEncodedFormEntity(nameValuePairs));
3
1
4
1
5
1
ResponseHandler<String>responseHandler=newBasicResponseHandler();
StringresponseBody=client.execute(post,responseHandler);
//剖析JSON前往的JSONObjectjson=newJSONObject(responseBody);
JSONArrayjArray=json.getJSONArray("posts");
ArrayList<HashMap<String,String>>mylist=new
6ArrayList<HashMap<String,String>>();
for(inti=0;i<jArray.length();i++){
1
HashMap<String,String>map=newHashMap<String,String>();
7
JSONObjecte=jArray.getJSONObject(i);
1
Strings=e.getString("post");
8
1JSONObjectjObject=newJSONObject(s);
map.put("idusers",jObject.getString("idusers"));
9
map.put("UserName",jObject.getString("UserName"));
2
map.put("FullName",jObject.getString("FullName"));
0
mylist.add(map);
2
1}
2Toast.makeText(this,responseBody,Toast.LENGTH_LONG).show();
2
2
3
2
4
2
5
2
6
2
7
2
8
2
9
3
0
3
1
3
2
3
3
3
4
3
5
3
6
3
7
3
8
3
9
再搞个webservice2.php,
该文件用来承受并保管客户端传递过去的
JSON数据。
1
2
<php
3
$json=file_get_contents('
$obj=json_decode($json);
//保管数据库
php://input');
4
5
6
7
8
9
$con=mysql_connect('localhost','root','XXX')or
totheDB');
mysql_select_db('jsonandroid',$con);
die('Cannotconnect
mysql_query("INSERTINTO'users'(UserName,FullName)VALUES
('".$obj->{'UserName'}."','".$obj->{'FullName'}."')");
mysql_close($con);
10
11
12
$posts=array(1);
13
14
15
16
17
header('Content-type:application/json');
echojson_encode(array('posts'=>$posts));
>
而Android客户端,能够结构JSON,发送到webservice2.php
try{
1
JSONObjectjson=newJSONObject();
2
json.put("UserName","test2");
3
json.put("FullName","1234567");
4
ParamsParams=newBasicParams();
5
ConnectionParams.setConnectionTimeout(Params,
6
TIMEOUT_MILLISEC);
7
ConnectionParams.setSoTimeout(Params,TIMEOUT_MILLISEC);
8
Clientclient=new
DefaultClient(Params);
9
10Stringurl=
11"://10.0.2.2:8082//myphp/phpWebservice/webservice2.php
12Postrequest=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();
24}
25
展开阅读全文