php客户端安卓调用webservice,[操作系统]Android访问php webservice

'Content-type: text/);

echo '';

foreach ($posts as $index =>$post) {

if (is_array($post)) {

foreach($post as $key =>$value) {

echo '';

if (is_array($value)) {

foreach($value as $tag =>$val) {

echo '',htmlentities($val),'',$tag,'>';

}

}

echo '',$key,'>';

}

}

}

echo '';

}

}

?>

则可以把数据表输出为JSON或者try {

HttpParams httpParams =new BasicHttpParams();

HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);

HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);

HttpParams p =new BasicHttpParams();

p.setParameter("user","1");

HttpClient httpclient =new DefaultHttpClient(p);

String url ="http://10.0.2.2:8082/myphp/phpWebservice/webservice1.php?user=1&format=json";

HttpPost httppost =new HttpPost(url);

try {

Log.i(getClass().getSimpleName(),"send task - start");

List nameValuePairs =new ArrayList(2);

nameValuePairs.add(new BasicNameValuePair("user","1"));

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

ResponseHandler responseHandler =new BasicResponseHandler();

String responseBody = httpclient.execute(httppost, responseHandler);

// 解析JSON返回的 JSONObject json = new JSONObject(responseBody);

JSONArray jArray = json.getJSONArray("posts");

ArrayList> mylist =new ArrayList>();

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

HashMap map =new HashMap();

JSONObject e = jArray.getJSONObject(i);

String s = e.getString("post");

JSONObject jObject =new JSONObject(s);

map.put("idusers", jObject.getString("idusers"));

map.put("UserName", jObject.getString("UserName"));

map.put("FullName", jObject.getString("FullName"));

mylist.add(map);

}

Toast.makeText(this, responseBody, Toast.LENGTH_LONG).show();

再搞个webservice2.php,该文件用来接受并保存客户端传送过来的JSON数据。<?php

$json =file_get_contents('php://input');

$obj = json_decode($json);

//保存数据库

$con = mysql_connect('localhost','root','XXX')or die('Cannot connect to the DB');

mysql_select_db('jsonandroid',$con);

mysql_query("INSERT INTO 'users' (UserName, FullName) VALUES ('".$obj->{'UserName'}."','".$obj->{'FullName'}."')");

mysql_close($con);

$posts =array(1);

header('Content-type: application/json');

echo json_encode(array('posts'=>$posts));

?>

而Android客户端,可以构造JSON,发送到webservice2.phptry {

JSONObject json =new JSONObject();

json.put("UserName","test2");

json.put("FullName","1234567");

HttpParams httpParams =new BasicHttpParams();

HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);

HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);

HttpClient client =new DefaultHttpClient(httpParams);

String url ="http://10.0.2.2:8082//myphp/phpWebservice/webservice2.php";

HttpPost request =new HttpPost(url);

request.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF8")));

request.setHeader("json", json.toString());

HttpResponse response = client.execute(request);

HttpEntity entity = response.getEntity();

if (entity !=null) {

InputStream instream = entity.getContent();

String result = RestClient.convertStreamToString(instream);

Log.i("Read from server", result);

Toast.makeText(this, result,

Toast.LENGTH_LONG).show();

}

这样就可以把Android端发送的数据保存到服务端了。

转载请保留本文网址:http://www.shaoqun.com/a/88595.html

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们:admin@shaoqun.com。

Android

0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值