第一范文网 - 专业文章范例文档资料分享平台

Android访问php webservice简单一例

来源:用户分享 时间:2025/6/1 6:52:32 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

如果是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 ''; 26 foreach ($posts as $index => $post) { 27 if (is_array($post)) { 28 foreach($post as $key => $value) { 29 echo '<',$key,'>'; 30 if (is_array($value)) { 31 foreach($value as $tag => $val) { 32 echo '<',$tag,'>',htmlentities($val),'';

33 } 34 }

35 echo ''; 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 nameValuePairs = new ArrayList(2); 1nameValuePairs.add(new BasicNameValuePair(\

3 httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

1ResponseHandler responseHandler = new BasicResponseHandler(); 4 String responseBody = httpclient.execute(httppost, responseHandler); 1// 解析JSON返回的 JSONObject json = new JSONObject(responseBody); 5 JSONArray jArray = json.getJSONArray(\ 1ArrayList> mylist = new 6 ArrayList>();

1for (int i = 0; i < jArray.length(); i++) {

7 HashMap map = new HashMap(); 1JSONObject e = jArray.getJSONObject(i); 8 String s = e.getString(\

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

搜索更多关于: Android访问php webservice简单一例 的文档
Android访问php webservice简单一例.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c2yue93s9b06cyp27mp7y_1.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top