【问题标题】:RESTful web service with Zend Framework 2带有 Zend Framework 2 的 RESTful Web 服务
【发布时间】:2014-06-04 23:04:48
【问题描述】:

我正在开发一个代表 RESTful Web 服务的 Zend Framework 2 模块。控制器将两个参数作为输入(来自外部应用程序),使用这些参数查询指定的数据库并返回一个 JSON。

相册控制器.php

<?php
namespace Album\Controller;


use Album\Model\Album;
use Album\Model\AlbumTable;
use Zend\Mvc\Controller\AbstractRestfulController;
use Zend\Http\Request;
use Zend\View\Model\JsonModel;
use Zend\Mvc\Controller\Plugin;

class AlbumController extends AbstractRestfulController
{

    /*public function indexAction()
    {
        return array();
    }*/

    protected $albumTable;

    public function getList()
    {

        $request = $this->getRequest();

        // if I set par1 and par statically, the service works
        $par1 = $request->getPost('par1');
        $par2 = $request->getPost('par2');

        $results = $this->getAlbumTable()->getAlbumByYearAndGenre($par1, $par2);
        $data = array();

        foreach ($results as $result) {
            $data[] = $result;
        }

        return new JsonModel(array(
            'Albums' => $data
        ));
    }

    public function getAlbumTable()
    {
        if (! $this->albumTable) {
            $sm = $this->getServiceLocator();
            $this->albumTable = $sm->get('Album\Model\AlbumTable');
        }
        return $this->albumTable;
    }

    public function get($id)
    {
        // TODO: Implement Method
    }

    public function create($data)
    {
        // TODO: Implement Method
    }

    public function update($id, $data)
    {
        // TODO: Implement Method
    }

    public function delete($id)
    {
        // TODO: Implement Method
    }
}

相册表.php

<?php
namespace Album\Model;

use Zend\Db\TableGateway\TableGateway;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class AlbumTable implements ServiceLocatorAwareInterface

{

protected $tableGateway;
protected $serviceLocator;
protected $adapter;

public function __construct(TableGateway $tableGateway)
{
    $this->tableGateway = $tableGateway;
}

public function fetchAll()
{
    $resultSet = $this->tableGateway->select();
    return $resultSet;
}

public function setServiceLocator(ServiceLocatorInterface $serviceLocator) {
    $this->serviceLocator = $serviceLocator;
}

public function getServiceLocator() {
    return $this->serviceLocator;
}

public function getAdapter()
{
    if (!$this->adapter) {
        $sl = $this->getServiceLocator();
        $this->adapter = $sl->get('db2');
    }
    return $this->adapter;
}

public function getAlbumByYearAndGenre($par1, $par2)
{




    $sql = "my query";

    $statement = $this->getAdapter()->query($sql);
    return $statement->execute();
}

}

外部应用程序(Android 应用程序)使用 JSONParser 来检索一些信息:

public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";


    public JSONParser() {

    }


    public JSONObject makeHttpRequest(String url, String method,
            List<NameValuePair> params) {


        try {


            if(method == "POST"){

                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));

                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
                httpClient.close();
            }else if(method == "GET"){

                DefaultHttpClient httpClient = new DefaultHttpClient();
                String paramString = URLEncodedUtils.format(params, "utf-8");
                url += "?" + paramString;
                HttpGet httpGet = new HttpGet(url);

                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
                httpClient.close();
            }          

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"));
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
           e.printStackTrace();
        }


        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return jObj;

    }
}

为了模拟应用的行为,我创建了一个 JSONParserTest:

public class JSONParserTest {
    public static void main(String[] args) {
        JSONParser jparser = new JSONParser();
        String url = "myurl";
        int par1 = 0;
        String par2 = "..."
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("par1", Integer.toString(par1)));
        params.add(new BasicNameValuePair("par2", par2);
        JSONObject json = jparser.makeHttpRequest(url, "POST", params);
        try {
            JSONArray jarray = json.getJSONArray("Albums");
            System.out.println(jarray.toString());
            System.out.println(json.toString());
            for(int i = 0; i < jarray.length(); i++)
            {
                JSONObject object = jarray.getJSONObject(i);
                System.out.println(object.getString("par1"));
                System.out.println(object.getString("par2"));
            } 

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

问题是如果我在控制器中静态设置参数并打开路径,我会得到 JSON。但是如果我将参数绑定到请求,似乎 zend 没有得到它们(我得到一个空的 JSON)。其实这是个例外:

org.json.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]
    at org.json.JSONTokener.syntaxError(JSONTokener.java:433)
    at org.json.JSONObject.<init>(JSONObject.java:188)
    at org.json.JSONObject.<init>(JSONObject.java:314)
    at com.example.mypackage.jclasses.JSONParser.makeHttpRequest(JSONParser.java:89)
    at com.example.mypackage.jclasses.JSONParserTest.main(JSONParserTest.java:22)
Exception in thread "main" java.lang.NullPointerException
    at com.example.mypackage.jclasses.JSONParserTest.main(JSONParserTest.java:24)

(第 22 行是 JSONObject json = jparser.makeHttpRequest(url, "POST", params);,第 24 行是:JSONArray jarray = json.getJSONArray("Albums");

【问题讨论】:

    标签: php android json rest zend-framework2


    【解决方案1】:

    似乎 Json 字符串格式错误。尝试做一个 System.out.println(sb.toString());在 JSONParser 类中查看 StringBuilder 是如何构建 de Json 的。

    try {
      BufferedReader reader = new BufferedReader(new InputStreamReader(
        is, "iso-8859-1"));
      StringBuilder sb = new StringBuilder();
      String line = null;
      while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
      }
      is.close();
      System.out.println(sb.toString());
      json = sb.toString();
    } catch (Exception e) {
       e.printStackTrace();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-03
      相关资源
      最近更新 更多