Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使PHP服务器向Android设备发送数据_Php_Android - Fatal编程技术网

如何使PHP服务器向Android设备发送数据

如何使PHP服务器向Android设备发送数据,php,android,Php,Android,我希望Android设备将数据发送到PHP服务器。PHP服务器接收到数据后,会向Android发送其他数据。第一部分可以使用JSON完成。但是,我不知道如何让PHP服务器向Android发送数据。对不起,我是PHP新手 您要寻找的是某种AJAX调用,允许HTTP GET请求停留并等待服务器返回值: 您要寻找的是某种AJAX调用,允许HTTP GET请求停留并等待服务器返回值: 我目前正在开发一个应用程序,该应用程序使用objective-c[用于iphone]与PHP服务器进行通信(双向通信,服

我希望Android设备将数据发送到PHP服务器。PHP服务器接收到数据后,会向Android发送其他数据。第一部分可以使用JSON完成。但是,我不知道如何让PHP服务器向Android发送数据。对不起,我是PHP新手

您要寻找的是某种AJAX调用,允许HTTP GET请求停留并等待服务器返回值:


您要寻找的是某种AJAX调用,允许HTTP GET请求停留并等待服务器返回值:


我目前正在开发一个应用程序,该应用程序使用objective-c[用于iphone]与PHP服务器进行通信(双向通信,服务器向应用程序发送数据,应用程序向服务器发送数据),但原理是一样的。
我们将REST服务与JSON一起使用。

在您的情况下,它应该是这样工作的:
Mobile 1通过REST调用将数据发送到REST服务器(它调用method1。服务器是使用Zend_REST开发的),它将数据存储在数据库中(例如mySQL)。

mobile2定期向REST服务器发送请求,并发送到一个方法,该方法检查mySQL中的新条目。如果有新的东西,它会用数据发送响应,如果没有,它会发送false。

我目前正在开发一个应用程序,该应用程序使用objective-c(iphone)与PHP服务器通信(双向通信,服务器向应用程序发送数据,应用程序向服务器发送数据),但原理是一样的。
我们将REST服务与JSON一起使用。

在您的情况下,它应该是这样工作的:
Mobile 1通过REST调用将数据发送到REST服务器(它调用method1。服务器是使用Zend_REST开发的),它将数据存储在数据库中(例如mySQL)。
mobile2定期向REST服务器发送请求,并发送到一个方法,该方法检查mySQL中的新条目。如果有新内容,它将发送带有数据的响应,如果没有,则发送false。

PHP脚本“打印”的任何数据都将在对Android设备上的调用的响应中返回

您可以在PHP中执行以下操作:

<?php
// TODO: Handle incoming data

// Send JSON data back to client
header('Cache-Control: no-cache, must-revalidate');
header('Content-type: application/json');

// Compute data
$data = array(1, 2, 3, 4, 5, 6, 7, 8, 9);

// Encode/print data
echo json_encode($data);
?>

您可能希望用代码替换第一条注释,以处理从客户端(Android)提交的数据。然后将响应头设置为
application/json
类型,并
echo
返回编码数据

从技术上讲,您可以
echo
/
print
返回您想要的任何内容,但是使用类似JSON的格式可以更容易地在客户端解码数据。

PHP脚本“打印”的任何数据都将在对Android设备上的调用的响应中返回

您可以在PHP中执行以下操作:

<?php
// TODO: Handle incoming data

// Send JSON data back to client
header('Cache-Control: no-cache, must-revalidate');
header('Content-type: application/json');

// Compute data
$data = array(1, 2, 3, 4, 5, 6, 7, 8, 9);

// Encode/print data
echo json_encode($data);
?>

您可能希望用代码替换第一条注释,以处理从客户端(Android)提交的数据。然后将响应头设置为
application/json
类型,并
echo
返回编码数据


从技术上讲,你可以
echo
/
打印你想要的任何东西,但是使用JSON这样的格式可以更容易地在客户端解码数据。

下面是一个Android代码片段,可以向某个虚假网站发送POST请求,发送电子邮件、密码和数据字符串(你可以将JSON放入数据字符串中)

HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://website.com/yourPageHere.php");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("email", emailString));
nameValuePairs.add(new BasicNameValuePair("password", passwordString));
nameValuePairs.add(new BasicNameValuePair("data", yourDataString));

try {
    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
} catch (UnsupportedEncodingException e) {
    //do stuff
}

HttpResponse response = null;
try {
    response = httpClient.execute(httpPost);
} catch (ClientProtocolException e) {
    //do stuff
} catch (IOException e) {
    //do stuff
}

if(response == null){
    //time out or other problem, fail gracefully

}

HttpEntity responseEntity = response.getEntity();

try {
    String jsonString = EntityUtils.toString(responseEntity);
    //jsonString is the full body of the response from the server
    //if your php script is sending json, thats whats in here
} catch (ParseException e) {
    //do stuff
} catch (IOException e) {
    //do stuff
}
HttpClient-HttpClient=newdefaulthttpclient();
HttpPost HttpPost=新的HttpPost(“http://website.com/yourPageHere.php");
List nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“email”,emailString));
添加(新的BasicNameValuePair(“密码”,passwordString));
添加(新的BasicNameValuePair(“数据”,yourDataString));
试一试{
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
}捕获(不支持的编码异常e){
//做事
}
HttpResponse响应=null;
试一试{
response=httpClient.execute(httpPost);
}捕获(客户端协议例外e){
//做事
}捕获(IOE异常){
//做事
}
如果(响应==null){
//超时或其他问题,请优雅地失败
}
HttpEntity responseEntity=response.getEntity();
试一试{
字符串jsonString=EntityUtils.toString(responseEntity);
//jsonString是服务器响应的完整主体
//如果您的php脚本正在发送json,这里就是这样
}捕获(解析异常){
//做事
}捕获(IOE异常){
//做事
}
您的php脚本,yourPageHere.php 将其视为您可能编写的任何其他php脚本,除了返回html之外,您只返回表示json数据的文本块

<?php

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

/*
here you can use the $_POST['email'], $_POST['password']
and $_POST['data'] indexes to access the data sent to 
you from the phone, then create a json string to return
 to the phone
*/

/* you can convert php objects/arrays to json using
json_encode($object), handle this however you
want just so that $jsonString is the final
representation of the json object */
$jsonString = 'blabla';   


/*
prints the string in the body of the response,
this is the "jsonString" object found in the
above android snippet.
*/
echo $jsonString; // 
?>

您可以使用GET请求而不是POST来执行上述操作。
如果你是一个真正的PHP新手,你可能需要制作几个表单页面示例来掌握读取url参数的诀窍。

下面是一个Android代码片段,用于向某个虚假网站发送POST请求,发送电子邮件、密码和数据字符串(你可以将json放在数据字符串中)

HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://website.com/yourPageHere.php");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("email", emailString));
nameValuePairs.add(new BasicNameValuePair("password", passwordString));
nameValuePairs.add(new BasicNameValuePair("data", yourDataString));

try {
    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
} catch (UnsupportedEncodingException e) {
    //do stuff
}

HttpResponse response = null;
try {
    response = httpClient.execute(httpPost);
} catch (ClientProtocolException e) {
    //do stuff
} catch (IOException e) {
    //do stuff
}

if(response == null){
    //time out or other problem, fail gracefully

}

HttpEntity responseEntity = response.getEntity();

try {
    String jsonString = EntityUtils.toString(responseEntity);
    //jsonString is the full body of the response from the server
    //if your php script is sending json, thats whats in here
} catch (ParseException e) {
    //do stuff
} catch (IOException e) {
    //do stuff
}
HttpClient-HttpClient=newdefaulthttpclient();
HttpPost HttpPost=新的HttpPost(“http://website.com/yourPageHere.php");
List nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“email”,emailString));
添加(新的BasicNameValuePair(“密码”,passwordString));
添加(新的BasicNameValuePair(“数据”,yourDataString));
试一试{
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
}捕获(不支持的编码异常e){
//做事
}
HttpResponse响应=null;
试一试{
response=httpClient.execute(httpPost);
}捕获(客户端协议例外e){
//做事
}捕获(IOE异常){
//做事
}
如果(响应==null){
//超时或其他问题,请优雅地失败
}
HttpEntity responseEntity=response.getEntity();
试一试{
s