php向安卓发送数据类型 php如何发送数据给websocket

Android 怎样和以PHP为服务端的后台通信这样理解吧,,,PHP作为服务端的时候,它就是做为一个o数据处理读取的工作 。
比如安桌端需要某位用户的登录信息正确不正确 ,
通过你制定好的类型,指定的方式向服务器的PHP传递参数,PHP进行处理 , 读取数据库比对,返回指定类型的数据给安卓做为数据交互 。。。
常用几种类型JSON,XML,JSONP等等 。。。
安卓和PHP你可以分开理解 , 它们交互的只是数据而已,,所以别把他们放一起来理解 。。。
项目指定用JSON来做为数据类型,那你安卓就向PHP,,POST也好,什么方式也好,项目需求来指定传送方式,把数据传送到指定的PHP处理控制器,PHP处理好,返回值 。。。
这样理解可以会更好理解:就跟AJAX PHP差不多的道理 。。。
android怎么得到php发来的json数据使用守则
首先,我们要创建Web服务,从MySQL数据库中读取数据 。
?php
pre/* require the user as the parameter */
pre//
if(isset($_GET['user'])intval($_GET['user'])) {
/* soak in the passed variable or set our own */
$number_of_posts = isset($_GET['num']) ? intval($_GET['num']) : 10; //10 is the default
$format = strtolower($_GET['format']) == 'json' ? 'json' : 'xml'; //xml is the default
$user_id = intval($_GET['user']); //no default
/* connect to the db */
$link = mysql_connect('localhost','root','123456') or die('Cannot connect to the DB');
mysql_select_db('TEST',$link) or die('Cannot select the DB');
/* grab the posts from the db */
//$query = "SELECT post_title, guid FROM wp_posts WHERE post_author =
//$user_id AND post_status = 'publish' ORDER BY ID DESC LIMIT $number_of_posts";
$query = "SELECT * FROM `test`.`users`;";
$result = mysql_query($query,$link) or die('Errant query:'.$query);
/* create one master array of the records */
$posts = array();
if(mysql_num_rows($result)) {
while($post = mysql_fetch_assoc($result)) {
$posts[] = array('post'=$post);
}
}
/* output in necessary format */
if($format == 'json') {
header('Content-type: application/json');
echo json_encode(array('posts'=$posts));
}
else {
header('Content-type: text/xml');
echo '';
foreach($posts as $index = $post) {
if(is_array($post)) {
foreach($post as $key = $value) {
echo '',$key,'';
if(is_array($value)) {
foreach($value as $tag = $val) {
echo '',$tag,'',htmlentities($val),'/',$tag,'';
}
}
echo '/',$key,'';
}
}
}
echo '';
}
/* disconnect from the db */
@mysql_close($link);
}
?
下面是代码为Android活动读取Web服务和解析JSON对象:
public void clickbutton(View v) {
try {
//
// Log.i(getClass().getSimpleName(), "sendtask - start");
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams,
TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
//
HttpParams p = new BasicHttpParams();
// p.setParameter("name", pvo.getName());
p.setParameter("user", "1");
// Instantiate an HttpClient
HttpClient httpclient = new DefaultHttpClient(p);
String url = ""
"webservice1.php?user=1format=json";
HttpPost httppost = new HttpPost(url);
// Instantiate a GET HTTP method
try {
Log.i(getClass().getSimpleName(), "sendtask - start");
//
ListNameValuePair nameValuePairs = new ArrayListNameValuePair(
2);
nameValuePairs.add(new BasicNameValuePair("user", "1"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
ResponseHandlerString responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httppost,
responseHandler);
// Parse
JSONObject json = new JSONObject(responseBody);
JSONArray jArray = json.getJSONArray("posts");
ArrayListHashMapString, String mylist =
new ArrayListHashMapString, String();
for (int i = 0; ijArray.length(); i) {
HashMapString, String map = new HashMapString, String();
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();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Log.i(getClass().getSimpleName(), "sendtask - end");
} catch (Throwable t) {
Toast.makeText(this, "Request failed: "t.toString(),
Toast.LENGTH_LONG).show();
}
}
这里是PHP代码,将数据发送到Web服务,并将其保存:
?php
//$json=$_GET ['json'];
$json = file_get_contents('php://input');
$obj = json_decode($json);
//echo $json;
//Save
$con = mysql_connect('localhost','root','123456')
or die('Cannot connect to the DB');
mysql_select_db('TEST',$con);
/* grab the posts from the db */
//$query = "SELECT post_title, guid FROM wp_posts WHERE
//post_author = $user_id AND post_status = 'publish'
// ORDER BY ID DESC LIMIT $number_of_posts";
mysql_query("INSERT INTO `test`.`users` (UserName, FullName)
VALUES ('".$obj-{'UserName'}."', '".$obj-{'FullName'}."')");
mysql_close($con);
//
//$posts = array($json);
$posts = array(1);
header('Content-type: application/json');
echo json_encode(array('posts'=$posts));
?
Android的活动 , 将数据发送到Web服务作为一个JSON对象保存在MySQL数据库中
public void clickbuttonRecieve(View v) {
try {
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 = "?"
//"json={\"UserName\":1,\"FullName\":2}";
String url = "";
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 the response does not enclose an entity, there is no need
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();
}
} catch (Throwable t) {
Toast.makeText(this, "Request failed: "t.toString(),
Toast.LENGTH_LONG).show();
}
}
知识点
要连接到你的模拟器 , 你可以使用此链接: 。
要读取JSON对象在Web服务中 , 您可以使用下面这行代码:
$json = file_get_contents('php://input');
$obj = json_decode($json);
PHP怎么写一个登录的接口,安卓端调用 。其实只要写一个数据转换php向安卓发送数据类型的函数,然后把安卓端php向安卓发送数据类型的数据转换成pc端php向安卓发送数据类型的数据类型,然后调用Pc端的登录函数就好php向安卓发送数据类型了,代码应该很简单,
这个函数就是接口:
接口功能就是
接收安卓的数据,
处理数据,
调用pc端登录函数,
返回登录结果给安卓端
php后台如何接收安卓传来的json数据,json名为object将接受过来的json转换成php数组格式 。
$json=接收来的json字符串;
$json=json_decode($json,true);//这里转换成数组然后就是操作数组的方士去读取了 。
echo $json['id'];
PHP如何模拟安卓设备,请求API接口的数据?模拟的时候,在头信息中携带这些参数 。可以用postman这个chrome插件,调试API接口很方便 。方法很多,看的POST请求还是GET请求 , CURL扩展可以实现 。
【php向安卓发送数据类型 php如何发送数据给websocket】php向安卓发送数据类型的介绍就聊到这里吧,感谢你花时间阅读本站内容 , 更多关于php如何发送数据给websocket、php向安卓发送数据类型的信息别忘了在本站进行查找喔 。

    推荐阅读