是否可以从Android应用程序调用此php函数?
<?PHP
namespace TEST
{
class NameOfClass
{
public function InsertAccount($firstname, $lastname)
{
...
}
}
}
?>
以下是
您可以做到这一点的方法:创建一个包含您的方法的 php 文件。请注意,在这种情况下,我们的文件仅接受 POST 请求:
文件:我的方法.php
<?php
if($_SERVER["REQUEST_METHOD"] == "POST"){
//Request Data comes in JSON format from the android app
$json_request = json_decode(file_get_contents("php://input"));
$json_result = array();
//Get name from $json_request variable
$name = $json_request->{"name"};
//Your method
sayHi($name);
}
function sayHi($name){
echo "Hello " . $name;
}
?>
在您的安卓应用程序中,向您的 php 文件发出 HTTP 请求,如下所示
活动内部的类
private class CheckName extends AsyncTask<String, Void, String> {
private String apiUrl;
private String name;
public CheckName(String apiUrl, String name) {
this.apiUrl = apiUrl;
this.name = name;
}
@Override
protected String doInBackground(String... params) {
try {
// HTTP Client
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(apiUrl);
// DATA TO SEND
JSONObject request = new JSONObject();
request.put("name", name);
// ENTITY
StringEntity se = new StringEntity(request.toString());
// PARAMS
httpPost.setEntity(se);
httpPost.setHeader("Content-Type", "application/json");
// RESPONSE
HttpResponse response = httpClient.execute(httpPost);
// RESULT DATA
result = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
} catch (Exception e) {
e.printStackTrace();
}
//The result that you get should be: Hello Joe
return result;
}
}
希望对你有帮助
编辑:这就是您在应用程序上实现它的方式
CheckName user = new CheckName("http://my-domain-name.com/my-method.php", "Joe");
user.execute();