Codenameone 如何在CN1中为php服务器使用RESTfulWebServiceClient
我想用php为服务器生成代码,并将其用作后端,而不是java中的Web服务。我注意到,CN1中的webservices向导在服务器端生成了详细信息。在我自己的例子中,我想用php创建服务器,并使用RESTfulWebServiceClient指向urlCodenameone 如何在CN1中为php服务器使用RESTfulWebServiceClient,codenameone,Codenameone,我想用php为服务器生成代码,并将其用作后端,而不是java中的Web服务。我注意到,CN1中的webservices向导在服务器端生成了详细信息。在我自己的例子中,我想用php创建服务器,并使用RESTfulWebServiceClient指向url 请问我该怎么办 是否有任何webservice向导可以在netbeans中生成类似java的php服务器 在应用内购买的自动重新订阅中,我是否可以在不使用RESTfulWebServiceClient的情况下与mysql数据库交互? 感谢您的回复
我已经用php创建了Web服务,并在代码名one中使用了url。我能够使用url将收据提交到数据库中。请参见下图
[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收据。订阅删除广告仍然存在。请帮忙,谢谢
这是我的php Web服务器代码
<?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().'}';
}
});
我们的Web服务向导需要一个Java服务器,而不生成PHP代码
通过使用ConnectionRequest
并使用GET
、POST
、PUT
&DELETE
调用URL,您可以使用Codename One中的任何restful服务
看。如果您使用PHP创建一个Web服务并使用curl,我可以帮助您使用一个等效的代码名。我们的Web服务向导需要一个Java服务器,而不生成PHP代码
通过使用ConnectionRequest
并使用GET
、POST
、PUT
&DELETE
调用URL,您可以使用Codename One中的任何restful服务
看。如果您使用PHP创建Web服务并使用curl,我可以帮助您使用等效的代码名One。FYI您在我的答案上按了edit而不是您的问题FYI您在我的答案上按了edit而不是您的问题先生。请编辑我的问题。先生,您可以看看是否有合适的导游。感谢数字1048le无效您从Web服务获取的JSON不是有效的JSON。我建议在模拟器中使用Network Monitor工具,您可以看到请求进入并看到JSONI在网络监视器的响应体上发现:{“error”:{“text”:SQLSTATE[23000]:完整性约束冲突:1048列“TRANSACTION_ID”不能为null}当我点击订阅按钮时,我在帖子正文中的请求中发现了这一点:{“purchaseDate”:149001720982,“orderData”:“sku”:“iapdemo.noads.month.auto”,“transactionId”:“cn1-iap-sim-8c008610-3b3b-43ed-9ee3-2dab830026a5”,“storeCode”:“simulator”}这似乎与您在邮件中发布的错误不一致。错误提到试图解析数字“1048le”。这在那个JSON中没有出现。先生。请我编辑了我的问题。先生,您可以查看正确的指南。谢谢数字1048le无效。您从Web服务获得的JSON不是有效的JSON。我建议使用模拟器中的网络监控工具,您可以看到请求进入并看到JSONI在resp上发现这一点网络监视器中的onse主体:{“error”:{“text”:SQLSTATE[23000]:完整性约束冲突:1048列“TRANSACTION_ID”不能为null}当我单击subscription按钮时,我在post主体中的请求中发现了这一点:{“purchaseDate”:149001720982,“orderData”:“sku”:“iapdemo.noads.month.auto”,“transactionId”:“cn1-iap-sim-8c008610-3B3B3B-43ed-9ee3-2dab830026a5”,“storeCode”:“simulator”}似乎与您在消息中发布的错误不符。该错误提到试图解析数字“1048le”。该JSON中没有出现该错误。
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);
}
};
}