如何在CN1中为php服务器使用RESTfulWebServiceClient

时间:2017-03-15 04:01:16

标签: codenameone

我想在php中为服务器生成代码,并将其用作后端而不是java中的webservices。我注意到,CN1中的webservices向导在服务器端生成了详细信息。在我自己的情况下,我想在php中创建服务器并使用RESTfulWebServiceClient指向url。

  1. 请问我该怎么做?
  2. 是否有任何web服务向导可以像netbeans中的java一样生成php服务器?
  3. 在应用内购买的自动重新订阅中,我可以在不使用RESTfulWebServiceClient的情况下与mysql数据库进行交互吗? 感谢您的回复

    我在php中创建了webservices并使用了代号为url的url。我能够使用url将收据提交到数据库中。见下图

  4. enter image description here

    我仍有挑战,下面的错误代码已生成

     [EDT] 0:0:0,0 - Codename One revisions: 3dc2fe6c4df57ff264ea094d13e0275639b780862293
    
    [EDT] 0:0:0,0 - Receipts were last refreshed at Fri Mar 17 07:30:21 WAT 2017 so we won't refetch.
    WARNING: Apple will no longer accept http URL connections from applications you tried to connect to http://slimapp/api/customer/add to learn more check out https://www.codenameone.com/blog/ios-http-urls.html
    [Task Thread] 0:0:0,417 - Expected null for key value while parsing JSON token at row: 1 column: 44 buffer: 
    [Task Thread] 0:0:0,417 - Expected true for key value while parsing JSON token at row: 1 column: 47 buffer: 
    [Task Thread] 0:0:0,417 - Expected null for key value while parsing JSON token at row: 1 column: 49 buffer: 
    [Task Thread] 0:0:0,417 - Expected null for key value while parsing JSON token at row: 1 column: 52 buffer: 
    [Task Thread] 0:0:0,417 - Expected true for key value while parsing JSON token at row: 1 column: 57 buffer: l
    [Task Thread] 0:0:0,417 - Expected null for key value while parsing JSON token at row: 1 column: 70 buffer: 1048l
    [Task Thread] 0:0:0,417 - Expected null for key value while parsing JSON token at row: 1 column: 88 buffer: 1048l
    java.lang.NumberFormatException: For input string: "1048le"
        at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
        at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
        at java.lang.Double.parseDouble(Double.java:538)
        at com.codename1.io.JSONParser.parse(JSONParser.java:286)
        at com.codename1.io.JSONParser.parseJSON(JSONParser.java:427)
        at com.codename1.ws.RESTfulWebServiceClient.lambda$null$6(RESTfulWebServiceClient.java:204)
        at com.codename1.ui.Display$1.run(Display.java:806)
        at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)
    WARNING: Apple will no longer accept http URL connections from applications you tried to connect to http://slimapp/api/customer/add to learn more check out https://www.codenameone.com/blog/ios-http-urls.html
    [Task Thread] 0:0:8,888 - Expected null for key value while parsing JSON token at row: 1 column: 44 buffer: 
    [Task Thread] 0:0:8,888 - Expected true for key value while parsing JSON token at row: 1 column: 47 buffer: 
    [Task Thread] 0:0:8,888 - Expected null for key value while parsing JSON token at row: 1 column: 49 buffer: 
    [Task Thread] 0:0:8,888 - Expected null for key value while parsing JSON token at row: 1 column: 52 buffer: 
    [Task Thread] 0:0:8,888 - Expected true for key value while parsing JSON token at row: 1 column: 57 buffer: l
    [Task Thread] 0:0:8,888 - Expected null for key value while parsing JSON token at row: 1 column: 70 buffer: 1048l
    [Task Thread] 0:0:8,888 - Expected null for key value while parsing JSON token at row: 1 column: 88 buffer: 1048l
    java.lang.NumberFormatException: For input string: "1048le"
        at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043)
        at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
        at java.lang.Double.parseDouble(Double.java:538)
        at com.codename1.io.JSONParser.parse(JSONParser.java:286)
        at com.codename1.io.JSONParser.parseJSON(JSONParser.java:427)
        at com.codename1.ws.RESTfulWebServiceClient.lambda$null$6(RESTfulWebServiceClient.java:204)
        at com.codename1.ui.Display$1.run(Display.java:806)
        at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)
    

    订阅后,我按下Syns Receipts。订阅删除广告仍然存在。请帮忙谢谢

    这是我的php网络服务器代码。

        <?php
    use \Psr\Http\Message\ServerRequestInterface as Request;
    use \Psr\Http\Message\ResponseInterface as Response;
    
    
    
    $app = new \Slim\App;
    
    $app->get('/api/customers', function (Request $request, Response $response) {
    echo "customers";
    
      //  Get All customers
      $sql = "SELECT * FROM receipts";
    
      try{
    $db = new db();
    
    $db = $db->connect();
    $stmt = $db->query($sql);
    $customers = $stmt->fetchAll(PDO::FETCH_OBJ);
      $db = null;
      echo json_encode($customers);
    
      }catch(PDOException $e){
    echo '{"error": {"text": '.$e->getMessage().'}';
      }
    
    
    
    });
    
    
    
    
    
    $app->get('/api/customers/{transactionId}', function (Request $request, Response $response) {
    echo "customers";
    $id = $request ->getAttribute("transactionId");
      //  Get single  customers
      $sql = "SELECT * FROM receipts where transaction_id = $id";
    
      try{
    $db = new db();
    
    $db = $db->connect();
    $stmt = $db->query($sql);
    $customers = $stmt->fetchAll(PDO::FETCH_OBJ);
      $db = null;
      echo json_encode($customers);
    
      }catch(PDOException $e){
    echo '{"error": {"text": '.$e->getMessage().'}';
      }
    
    
    
    });
    
    
    
    
    //add a user
    $app->post('/api/customer/add', function (Request $request, Response $response) {
    //echo "customers";
    
    
    
    
    
    $transactionId = $request->getParam('transactionId');
    
    $sku = $request->getParam('sku');
    $purchaseDate = $request->getParam('purchaseDate');
    $orderData = $request->getParam('orderData');
    $storeCode = $request->getParam('storeCode');
    
    echo "emmy ";
    
    
     // (:transactionId,:sku,:purchaseDate,:orderData,:storeCode)";
    
    // 'transactionid' 
    
     $sql = "INSERT INTO receipts(transaction_id,sku,purchase_date,order_data,store_code)VALUES
              (:transactionid,:sku,:purchaseDate,:orderData,:storeCode)";
    
    
    
    
    
      try{
    $db = new db();
    
    $db = $db->connect();
    $stmt = $db->prepare($sql);
    
    
    
    $stmt->bindParam(':transactionid', $transactionId);
    $stmt->bindParam(':sku', $sku);
    $stmt->bindParam(':purchaseDate', $purchaseDate);
    $stmt->bindParam(':orderData', $orderData);
    $stmt->bindParam(':storeCode', $storeCode);
    
    
    
    
    
    
    $stmt->execute();
    
    echo '{"notice": {"text": "customar added"}';
    
    
      }catch(PDOException $e){
    echo '{"error": {"text": '.$e->getMessage().'}';
      }
    
    
    
    });
    

    这是我的网址和代码名称中的部分代码:

     private static final String receiptsEndpoint = "http://slimapp/api/customers";
      private static final String receiptsEndpoint2 = "http://slimapp/api/customer/add";
    
    
    private ReceiptStore createReceiptStore() {
        return new ReceiptStore() {
    
            RESTfulWebServiceClient client = createRESTClient(receiptsEndpoint);
    
          RESTfulWebServiceClient client2 = createRESTClient(receiptsEndpoint2);
    
            @Override
            public void fetchReceipts(SuccessCallback<Receipt[]> callback) {
                RESTfulWebServiceClient.Query query = new RESTfulWebServiceClient.Query() {
    
                    @Override
                    protected void setupConnectionRequest(RESTfulWebServiceClient client, ConnectionRequest req) {
                        super.setupConnectionRequest(client, req);
           //  req.setPost(true);
         // req.setHttpMethod("post"); //Change to GET if necessary
                        req.setUrl(receiptsEndpoint);
                    }
    
    
    
                    protected void setupConnectionRequest2(RESTfulWebServiceClient client2, ConnectionRequest req2) {
                        super.setupConnectionRequest(client, req2);
            req2.setPost(true);
         req2.setHttpMethod("post"); //Change to GET if necessary
                        req2.setUrl(receiptsEndpoint2);
                    }
    
    
                };
    
    
                client.find(query, rowset->{
                    List<Receipt> out = new ArrayList<Receipt>();
                    for (Map m : rowset) {
    
    
                        Result res = Result.fromContent(m);
                        Receipt r = new Receipt();
                        r.setTransactionId(res.getAsString("transactionId"));
    
    
    
                         System.out.println(r.getTransactionId()+"  ko");
                        r.setPurchaseDate(new Date(res.getAsLong("purchaseDate")));
                        r.setQuantity(1);
                        r.setSku(res.getAsString("sku"));
    
                        if (m.containsKey("cancellationDate") && m.get("cancellationDate") != null) {
                            r.setCancellationDate(new Date(res.getAsLong("cancellationDate")));
                        }
                        if (m.containsKey("expiryDate") && m.get("expiryDate") != null) {
                            r.setExpiryDate(new Date(res.getAsLong("expiryDate")));
                        }
                        out.add(r);
    
                    }
                    callback.onSucess(out.toArray(new Receipt[out.size()]));
                }); 
    
    
            }
    
            @Override
            public void submitReceipt(Receipt r, SuccessCallback<Boolean> callback) {
    
                  Map m = new HashMap();
                m.put("transactionId", r.getTransactionId());
              m.put("sku", r.getSku());
                m.put("purchaseDate", r.getPurchaseDate().getTime());
                m.put("orderData", r.getOrderData());
               m.put("storeCode", r.getStoreCode());
    
           System.out.println(r.getTransactionId());
                client2.create(m, callback);
    
    
    
        }
    };
                }
    

    请帮助我在上面查看我的代码,可能是在php webserver代码或代号为一个代码。当我点击Syns Receipts按钮,没有收据更新并且记录进入数据库时​​,我能够订阅并接收订阅成功并且记录已插入数据库的消息。请帮助,我不知道出了什么问题

1 个答案:

答案 0 :(得分:0)

我们的Web服务向导需要一个Java服务器,并且不会生成PHP代码。

您可以使用ConnectionRequest使用来自Codename One的任何其他服务,并使用GETPOSTPUT&amp; DELETE

请参阅developer guide section on networking。如果您使用PHP创建Web服务并具有curl用法,我可以使用Codename One等效。