0

我的 HttpGet 请求正在调用我的 indexAction,而不是 getAction。这是怎么回事?

这是我的代码:

    public function getAction() {

    $id = $this->_getParam('id');

    if(!$id)
    {
        $this->getResponse()
             ->setHttpResponseCode(400)
             ->setBody("no id");
        return;
    }

    try
    {
        $q = Doctrine_Query::create()
             ->from('Milotin_Model_Locations l')
             ->where ('l.id=?', $id);

        $result = $q->fetchArray();

        if(count($result) == 1)
        {
            $this->getResponse()
                 ->setHttpResponseCode(200)
                 ->setBody(json_encode($result));
        }
    }
    catch(Exception $e)
    {
        $this->getResponse()
             ->setHttpResponseCode(500)
             ->setBody($e->getMessage());
    }
}



    public function indexAction() {
}

这是我在 Android 中的代码:

    private static void getLoc()
{
    final HttpResponse response;

    final HttpGet getRequest = new HttpGet(LOCATION_URI + "?geolat=" + geoLat + "&geolong=" + geoLong);

    try {
        response = mHttpClient.execute(getRequest);

        if(response.getStatusLine().getStatusCode() == 200)
        {
            //do something
        }

    } catch (ClientProtocolException e) {
        Log.e(TAG, e.getMessage());
        e.printStackTrace();
    } catch (IOException e) {
        Log.e(TAG, e.getMessage());
        e.printStackTrace();
    } catch (JSONException e) {
        Log.e(TAG, e.getMessage());
        e.printStackTrace();
    }

}

我的 HttpPost 工作正常(它调用 postAction),有什么解释吗?

谢谢。

4

1 回答 1

0

我找到了答案。这实际上是 Zend Framework 的行为。如果在 GET 请求中没有找到 'id' 元素,它将重定向到 indexAction,而不是 getAction。

例子:

'GET localhost/student' 将重定向到 indexAction,而 'GET localhost/student/23' 将重定向到 getAction。(23 是 id)

在 Zend Framework:初学者指南中找到它,作者 Vikram Vaswani。

于 2011-12-27T06:51:49.377 回答