如果是PHP做的服务端,而我们要用android去访问,怎么办?当然可以用REST,但也可以用点笨的方法,比如可以让PHP的服务端返回JSON或XML数据,而Android端则可以用APACHE的httpclient去访问。
下面是一个例子,假设数据表中users表有如下字段(mysql): idusers,UserName,FullName
加点数据,然后在服务端建立一个webservice1.php,作用是直接返回服务端数据库的数据,如下: ?
1
2 if (isset($_GET['user']) && intval($_GET['user'])) {
3 $format = strtolower($_GET['format']) == 'json' ? 'json' : 'xml'; //xml 4 is the default
5 $user_id = intval($_GET['user']); //no default 6 /* 连接数据库 */
7 $link = mysql_connect('localhost','root','xxxxx') or die('Cannot 8 connect to the DB');
9 mysql_select_db('jsonandroid',$link) or die('Cannot select the DB'); 10 $query = \ 11 $result = mysql_query($query,$link); 12 $posts = array();
13 if (mysql_num_rows($result)) {
14 while($post = mysql_fetch_assoc($result)) { 15 $posts[] = array('post'=>$post); 16 } 17 } 18 /* json
格式 */ 19 if($format == 'json') { 20 header('Content-type: application/json'); 21 echo json_encode(array('posts'=>$posts)); 22 } 23 else { 24 header('Content-type: text/xml'); 25 echo '
33 } 34 }
35 echo '',$key,'>'; 36 } 37 } 38 }
39 echo ''; 40 } 41 } 42 ?> 43 44 45
则可以把数据表输出为JSON或者XML格式了,客户端的Android调用: ?
1 try {
2 HttpParams httpParams = new BasicHttpParams();
3 HttpConnectionParams.setConnectionTimeout(httpParams, 4 TIMEOUT_MILLISEC);
5 HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC); 6 HttpParams p = new BasicHttpParams(); 7 p.setParameter(\
8 HttpClient httpclient = new DefaultHttpClient(p); 9 String url =
\http://10.0.2.2:8082/myphp/phpWebservice/webservice1.php?user=1&fo0 rmat=json\
1HttpPost httppost = new HttpPost(url); 1 try {
1Log.i(getClass().getSimpleName(), \
2 List
3 httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
1ResponseHandler
1for (int i = 0; i < jArray.length(); i++) {
7 HashMap
1
JSONObject jObject = new JSONObject(s);
9 map.put(\ 2map.put(\ 0 map.put(\ 2mylist.add(map); 1 }
2Toast.makeText(this, responseBody, Toast.LENGTH_LONG).show();
12 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
再搞个webservice2.php,该文件用来接受并保存客户端传送过来的JSON数据。
?
1 2 3
$json = file_get_contents('php://input'); 4
$obj = json_decode($json); 5
//保存数据库 6
$con = mysql_connect('localhost','root','XXX') or die('Cannot connect 7
to the DB'); 8
mysql_select_db('jsonandroid', $con); 9
mysql_query(\10
('\ 11
mysql_close($con); 12
$posts = array(1); 13
header('Content-type: application/json'); 14
echo json_encode(array('posts'=>$posts)); 15 ?>
16 17
而Android客户端,可以构造JSON,发送到webservice2.php ?
1 try {
2 JSONObject json = new JSONObject(); 3 json.put(\ 4 json.put(\
5 HttpParams httpParams = new BasicHttpParams();
6 HttpConnectionParams.setConnectionTimeout(httpParams, 7 TIMEOUT_MILLISEC);
8 HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC); 9 HttpClient client = new DefaultHttpClient(httpParams); 10 String url =
11 \http://10.0.2.2:8082//myphp/phpWebservice/webservice2.php\ 12 HttpPost request = new HttpPost(url); 13 request.setEntity(new
14 ByteArrayEntity(json.toString().getBytes(\ 15 request.setHeader(\ 16 HttpResponse response = client.execute(request); 17 HttpEntity entity = response.getEntity(); 18 if (entity != null) {
19 InputStream instream = entity.getContent();
20 String result = RestClient.convertStreamToString(instream); 21 Log.i(\ 22 Toast.makeText(this, result,
23 Toast.LENGTH_LONG).show(); 24 } 25
相关推荐: