使用PHP创建的Rest Web Service

时间:2012-04-30 03:06:38

标签: php android web-services rest

我在互联网上找到这个非常简单的PHP Web服务

<?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。

我的Android代码。有什么不对吗?

    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();

由于

1 个答案:

答案 0 :(得分:0)

似乎是标题问题,也许您的应用正在使用OK 200以外的其他方式进行响应