我在互联网上找到了这个非常简单的 PHP REST WebService
<?php
class MVRest {
private $supportedMethods;
public function __construct($supportedMethods) {
$this->supportedMethods = $supportedMethods;
}
public function handleRawRequest($_SERVER, $_GET, $_POST) {
$url = $this->getFullUrl($_SERVER);
$method = $_SERVER['REQUEST_METHOD'];
switch ($method) {
case 'GET':
case 'HEAD':
$arguments = $_GET;
break;
case 'POST':
$arguments = $_POST;
break;
case 'PUT':
case 'DELETE':
parse_str(file_get_contents('php://input'), $arguments);
break;
}
$accept = $_SERVER['HTTP_ACCEPT'];
$this->handleRequest($url, $method, $arguments, $accept);
}
protected function getFullUrl($_SERVER) {
$protocol = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
$location = $_SERVER['REQUEST_URI'];
if ($_SERVER['QUERY_STRING']) {
$location = substr($location, 0, strrpos($location, $_SERVER['QUERY_STRING']) - 1);
}
return $protocol.'://'.$_SERVER['HTTP_HOST'].$location;
}
public function handleRequest($url, $method, $arguments, $accept) {
switch($method) {
case 'GET':
$this->performGet($url, $arguments, $accept);
break;
case 'HEAD':
$this->performHead($url, $arguments, $accept);
break;
case 'POST':
$this->performPost($url, $arguments, $accept);
break;
case 'PUT':
$this->performPut($url, $arguments, $accept);
break;
case 'DELETE':
$this->performDelete($url, $arguments, $accept);
break;
default:
/* 501 (Not Implemented) for any unknown methods */
header('Allow: ' . $this->supportedMethods, true, 501);
}
}
protected function methodNotAllowedResponse() {
/* 405 (Method Not Allowed) */
header('Allow: ' . $this->supportedMethods, true, 405);
}
public function performGet($url, $arguments, $accept) {
echo $url.'<br />';
print_r($arguments);
echo $accept.'<br />';
}
public function performHead($url, $arguments, $accept) {
$this->methodNotAllowedResponse();
}
public function performPost($url, $arguments, $accept) {
$fp = fopen('data.txt', 'w');
fwrite($fp, '1');
fwrite($fp, '23');
fclose($fp);
echo 'GOTTED';
}
public function performPut($url, $arguments, $accept) {
$this->methodNotAllowedResponse();
}
public function performDelete($url, $arguments, $accept) {
$this->methodNotAllowedResponse();
}
}
$rest = new MVRest("GET, POST");
$rest->handleRawRequest($_SERVER, $_GET, $_POST);
我使用 jQuery 进行消费并且效果很好,但是当我使用 android 消费它时。我得到一个 IOException。io.getMessage() 返回 mydomain.com。
我的安卓代码。有什么问题吗?
HttpPost postMethod = new HttpPost("http://mydomain.com/rest.php");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("name","paulo"));
nameValuePairs.add(new BasicNameValuePair("last_name","fernandes"));
try {
postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
DefaultHttpClient hc = new DefaultHttpClient();
HttpResponse response = hc.execute(postMethod);
Toast.makeText(context, response.getStatusLine().toString(), Toast.LENGTH_LONG).show();
} catch (ClientProtocolException e) {
Toast.makeText(context, "1: "+e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (IOException e) {
Toast.makeText(context, "2: "+e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
Toast.makeText(context, result, Toast.LENGTH_LONG).show();
谢谢