[英]Consuming a Rest WebService created using PHP
I find on internet this very simple REST WebService in PHP 我在互联网上发现了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);
I consumed using jQuery and it worked very well, but when I consumed it using android. 我使用jQuery消费,并且效果很好,但是当我使用android消费时。 I get an IOException.
我得到一个IOException。 The io.getMessage() returned mydomain.com.
io.getMessage()返回了mydomain.com。
My Android code. 我的Android代码。 Is there anything wrong?
有什么问题吗?
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();
Thanks 谢谢
似乎是标题问题,可能是您的应用响应正常200
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.