0

I find on internet this very simple REST WebService in PHP

<?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. I get an IOException. The io.getMessage() returned mydomain.com.

My Android code. 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

2
  • in your AndroidManifest.xml have you given permission for your app to connect to the internet? Commented Apr 30, 2012 at 3:26
  • Try doing a packet trace to make sure the data being sent/received is as expected. Commented Apr 30, 2012 at 4:25

1 Answer 1

0

Seems a header problem, maybe your app is responding with something other than OK 200

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.