如何正确将get_result更改为Bind_result以防止应用返回Java.Lang.illegal?

时间:2018-08-13 11:07:12

标签: java php android api mysqli

我正在处理由get_result编写的项目,但是get_result需要mysqlnd才能在在线主机上工作。

无论如何,在我更改了此代码之后:

public function insertNewOrder($orderPrice,$orderComment,$orderAddress,$orderDetail,$userPhone)
{
    $stmt = $this->conn->prepare("INSERT INTO `Order`(`OrderDate`, `OrderStatus`, `OrderPrice`, `OrderDetail`, `OrderComment`, `OrderAddress`, `UserPhone`) VALUES (NOW(),0,?,?,?,?,?)")
    or die($this->conn->error);
    $stmt->bind_param("sssss",$orderPrice,$orderDetail,$orderComment,$orderAddress,$userPhone);
    $result = $stmt->execute();
    $stmt->close();

    if ($result) {
        $stmt = $this->conn->prepare("SELECT * FROM `order` WHERE `UserPhone`=? ORDER BY OrderId DESC LIMIT 1")
        or die($this->conn->error);
        $stmt->bind_param("s",$userPhone);
        $stmt->execute();
        $order = $stmt->get_result()->fetch_assoc();
        $stmt->close();
        return $order;
    }
    else
        return false;
}

对此:

public function insertNewOrder($orderPrice,$orderComment,$orderAddress,$orderDetail,$userPhone)
{
    $stmt = $this->conn->prepare("INSERT INTO `Order`(`OrderDate`, `OrderStatus`, `OrderPrice`, `OrderDetail`, `OrderComment`, `OrderAddress`, `UserPhone`) VALUES (NOW(),0,?,?,?,?,?)")
    or die($this->conn->error);
    $stmt->bind_param("sssss",$orderPrice,$orderDetail,$orderComment,$orderAddress,$userPhone);
    $result = $stmt->execute();
    $stmt->close();

    if ($result) {
        $stmt = $this->conn->prepare("SELECT OrderId, UserPhone, OrderDate, OrderStatus, OrderPrice, OrderDetail, OrderComment, OrderAddress FROM `order` WHERE `UserPhone`=? ORDER BY OrderId DESC LIMIT 1")
        or die($this->conn->error);
        $stmt->bind_param("s",$userPhone);
        $stmt->execute();
        $stmt->bind_result($arr['OrderId'], $arr['UserPhone'], $arr['OrderDate'], $arr['OrderStatus'], $arr['OrderPrice'], $arr['OrderDetail'], $arr['OrderComment'], $arr['OrderAddress']);

        $order = array();    // Initialise
        while ($stmt->fetch())
        {
            $order[] = $arr;
        }
        $stmt->close();
        return $order;
    }
    else
        return false;
}

api调用:

$response = array();
if(isset($_POST['orderDetail']) &&
isset($_POST['phone']) &&
isset($_POST['address']) &&
isset($_POST['comment']) &&
isset($_POST['price']))
{
$phone = $_POST['phone'];
$orderDetail = $_POST['orderDetail'];
$orderAddress = $_POST['address'];
$orderComment = $_POST['comment'];
$orderPrice = $_POST['price'];

$result = $db->insertNewOrder($orderPrice,$orderComment,$orderAddress,$orderDetail,$phone);
echo json_encode($result);



}
else{
echo json_encode("Required parameter (phone,detail,address,comment,price) is missing!");
}

发送订单后,我在应用中收到java.lang.illegalStateException: Excepted BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $这个错误,但是数据将发送到服务器应用。在我的应用程序中,客户端发送订单后,应用程序将向服务器发送通知并清除购物车。但就我而言,应用程序将发送订单并获得java.lang...,并且购物车中还有物品。

当我在本地主机上使用get_result时,应用程序不会显示任何错误,但是无论如何它都不会删除购物车中的数据,它只会发送订单。

这是购物车中的代码:

    private void sendOrderToServer(float sumPrice, List<Cart> carts, String orderComment, String orderAddress) {
    if (carts.size() > 0)
    {
        String orderDetail = new Gson().toJson(carts);

        mService.insertNewOrder(sumPrice,orderDetail,orderComment,orderAddress,Common.currentUser.getPhone())
                .enqueue(new Callback<OrderResult>() {
                    @Override
                    public void onResponse(Call<OrderResult> call, Response<OrderResult> response) {
                        sendNotificationToServer(response.body());
                    }

                    @Override
                    public void onFailure(Call<OrderResult> call, Throwable t) {
                        Toast.makeText(CartActivity.this, ""+t.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });
    }
}

private void sendNotificationToServer(final OrderResult orderResult) {

    mService.getToken("server_app_01","1")
            .enqueue(new Callback<Token>() {
                @Override
                public void onResponse(Call<Token> call, Response<Token> response) {
                    Map<String,String> contentSend = new HashMap<>();
                    contentSend.put("title","ATLASco");
                    contentSend.put("message","سفارش جدید دریافت شد"+orderResult.getOrderId());
                    DataMessage dataMessage = new DataMessage();
                    if (response.body().getToken() != null)
                        dataMessage.setTo(response.body().getToken());
                    dataMessage.setData(contentSend);

                    IFCMService ifcmService = Common.getGetFCMService();
                    ifcmService.sendNotification(dataMessage)
                            .enqueue(new Callback<MyResponse>() {
                                @Override
                                public void onResponse(Call<MyResponse> call, Response<MyResponse> response) {
                                    if (response.code() == 200)
                                    {
                                        if (response.body().success == 1)
                                        {
                                            Toast.makeText(CartActivity.this, "سفارش ثبت شد", Toast.LENGTH_SHORT).show();

                                            Common.cartRepository.emptyCart();
                                            finish();
                                        }
                                        else
                                        {
                                            Toast.makeText(CartActivity.this, "ارسال درخواست با مشکل مواجه شده است!", Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                }

                                @Override
                                public void onFailure(Call<MyResponse> call, Throwable t) {
                                    Toast.makeText(CartActivity.this, ""+t.getMessage(), Toast.LENGTH_SHORT).show();
                                }
                            });
                }

                @Override
                public void onFailure(Call<Token> call, Throwable t) {
                    Toast.makeText(CartActivity.this, ""+t.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });

}


private void loadCartItems() {
    compositeDisposable.add(
            Common.cartRepository.getCartItems()
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(new Consumer<List<Cart>>() {
                        @Override
                        public void accept(List<Cart> carts) throws Exception {
                            displayCartItem(carts);
                        }
                    })
    );
}

首先,我要修复绑定部分,然后再弄乱android部分。关于错误,应用程序没有收到任何错误,但是我的genymotion没有提供有关Google Play服务的一些提示,而当我尝试使用Open Gapps安装它时,它只会崩溃。 >

Logcat:

08-13 07:05:42.851 5535-5611/ir.atlaspio.atlasdrinkingservice V/FA:     Inactivity, disconnecting from the service
08-13 07:05:42.852 5535-5535/ir.atlaspio.atlasdrinkingservice V/FA: onUnbind called for intent. action: com.google.android.gms.measurement.START
Local AppMeasurementService is shutting down
08-13 07:05:43.248 5535-5535/ir.atlaspio.atlasdrinkingservice W/IInputConnectionWrapper: finishComposingText on inactive InputConnection

0 个答案:

没有答案
相关问题