Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android URL和HTTP Post错误_Android_Url_Http Post - Fatal编程技术网

Android URL和HTTP Post错误

Android URL和HTTP Post错误,android,url,http-post,Android,Url,Http Post,当我试图使用HTTPPOST将数据传递到服务器时,我遇到了一个问题。我想把这些数据插入数据库。但是,当我测试它时,错误显示为“不幸的是,…已停止” TestActivity.java public class TestActivity extends AppCompatActivity { ArrayList<Cart> cartList; String URL_SEND; @Override protected void onCreate(Bund

当我试图使用HTTPPOST将数据传递到服务器时,我遇到了一个问题。我想把这些数据插入数据库。但是,当我测试它时,错误显示为“不幸的是,…已停止”

TestActivity.java

public class TestActivity extends AppCompatActivity {
    ArrayList<Cart> cartList;
    String URL_SEND;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        cartList = new ArrayList<>();
        URL_SEND = "http://192.168.56.1/api.php";
        new SendOrder().execute();
    }

    class SendOrder extends AsyncTask<Void,Void,Void> {
        @Override
        protected Void doInBackground(Void... params) {
            Cart cart1 = new Cart("1", 3);
            cartList.add(cart1);
            Cart cart2 = new Cart("2", 2);
            cartList.add(cart2);
            try {
                URL url = new URL(URL_SEND);
                HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setRequestProperty("Content-Type", "application/json");
                httpURLConnection.connect();

                JSONObject jsonObject = new JSONObject();
                jsonObject.put("idUser", "1");
                jsonObject.put("total", 50000);
                jsonObject.put("cartArray", new JSONArray(cartList));
                jsonObject.put("key", "12345");

                OutputStreamWriter writer = new OutputStreamWriter(httpURLConnection.getOutputStream());
                writer.write(jsonObject.toString());
                writer.flush();
                Toast.makeText(TestActivity.this, "Order success", Toast.LENGTH_LONG).show();
                cartList.clear();
            } catch (Exception e) {
                Toast.makeText(TestActivity.this, "Server error", Toast.LENGTH_LONG).show();
            }

            return null;
        }
    }
}
api.php

<?php
if ($_SERVER['REQUEST_METHOD']=='POST'){
    include_once('../admin/connect_database.php');
    $user_id = $_POST['idUser'];
    $cartArray = $_POST['cartArray'];
    $key = $_POST['key'];
    $total= $_POST['total'];
    $status = "PENDING";

    if ($key == $keyapi) {
        $sth = $dbh->prepare("INSERT INTO order(user_id, total, status_order, time)
            VALUES(:idUser, :total, :status, :waktu)");
        $res = $sth->execute(array(
            'idUser' => $user_id,
            'total' => $total,
            'status' => $status,
            'waktu' => date()
        ));
        $id = $dbh->lastInsertId();
        foreach ($cartArray as $cart) {
            $idProduct = $cart->idMenu;
            $qty = $cart->quantity;
            $sth = $dbh->prepare('INSERT INTO order_detail(id_order, id_product, qty) VALUES (:idOrder, :idProduct, :qty)');
            $res = $sth->execute(array(
                'idOrder' => $id,
                'idProduct' => $idProduct,
                'qty' => $qty
            ));
        }
    }
}

?>

从doInBackground()中删除这些行:

您不能在doInBackground()中执行UI工作: 您必须在onPostExecute()中吐司该数据:


请参阅此链接:

从doInBackground()中删除这些行:

您不能在doInBackground()中执行UI工作: 您必须在onPostExecute()中吐司该数据:


请参阅此链接:

您不应该在后台线程中执行UI操作。在doInBackground()中删除Toast的显示。如果您想确定POST是否成功,我建议根据您的实现返回一个布尔对象

public class SendOrder extends AsyncTask<Void,Void,Boolean> {

            @Override
            public void onPostExecute(Boolean result) {

                if(result != null && result) {

                   Toast.makeText(TestActivity.this, "Order success", Toast.LENGTH_LONG).show();
                }
                else {
                   Toast.makeText(TestActivity.this, "Order error", Toast.LENGTH_LONG).show();
                }
            }


            @Override
            protected Boolean doInBackground(Void... params) {
                Cart cart1 = new Cart("1", 3);
                cartList.add(cart1);
                Cart cart2 = new Cart("2", 2);
                cartList.add(cart2);
                try {
                    URL url = new URL(URL_SEND);
                    HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                    httpURLConnection.setDoOutput(true);
                    httpURLConnection.setRequestMethod("POST");
                    httpURLConnection.setRequestProperty("Content-Type", "application/json");
                    httpURLConnection.connect();

                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("idUser", "1");
                    jsonObject.put("total", 50000);
                    jsonObject.put("cartArray", new JSONArray(cartList));
                    jsonObject.put("key", "12345");

                    OutputStreamWriter writer = new OutputStreamWriter(httpURLConnection.getOutputStream());
                    writer.write(jsonObject.toString());
                    writer.flush();

                    cartList.clear();

                    return true;
                } catch (Exception e) {
                    // Your code to Log the error 

                    return false;
                }


            }
        }
}
公共类SendOrder扩展异步任务{
@凌驾
public void onPostExecute(布尔结果){
if(结果!=null&&result){
Toast.makeText(TestActivity.this,“订单成功”,Toast.LENGTH_LONG.show();
}
否则{
Toast.makeText(TestActivity.this,“顺序错误”,Toast.LENGTH_LONG.show();
}
}
@凌驾
受保护的布尔doInBackground(Void…params){
购物车1=新购物车(“1”,3);
cartList.add(cart1);
购物车2=新购物车(“2”,2);
cartList.add(cart2);
试一试{
URL=新URL(URL\u发送);
HttpURLConnection HttpURLConnection=(HttpURLConnection)url.openConnection();
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestMethod(“POST”);
setRequestProperty(“内容类型”、“应用程序/json”);
httpURLConnection.connect();
JSONObject JSONObject=新的JSONObject();
jsonObject.put(“idUser”,“1”);
jsonObject.put(“总计”,50000);
put(“cartArray”,新的JSONArray(cartList));
jsonObject.put(“键”,“12345”);
OutputStreamWriter writer=新的OutputStreamWriter(httpURLConnection.getOutputStream());
write(jsonObject.toString());
writer.flush();
cartList.clear();
返回true;
}捕获(例外e){
//您的代码将记录错误
返回false;
}
}
}
}

您不应该在后台线程中执行UI操作。在doInBackground()中删除Toast的显示。如果您想确定POST是否成功,我建议根据您的实现返回一个布尔对象

public class SendOrder extends AsyncTask<Void,Void,Boolean> {

            @Override
            public void onPostExecute(Boolean result) {

                if(result != null && result) {

                   Toast.makeText(TestActivity.this, "Order success", Toast.LENGTH_LONG).show();
                }
                else {
                   Toast.makeText(TestActivity.this, "Order error", Toast.LENGTH_LONG).show();
                }
            }


            @Override
            protected Boolean doInBackground(Void... params) {
                Cart cart1 = new Cart("1", 3);
                cartList.add(cart1);
                Cart cart2 = new Cart("2", 2);
                cartList.add(cart2);
                try {
                    URL url = new URL(URL_SEND);
                    HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                    httpURLConnection.setDoOutput(true);
                    httpURLConnection.setRequestMethod("POST");
                    httpURLConnection.setRequestProperty("Content-Type", "application/json");
                    httpURLConnection.connect();

                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("idUser", "1");
                    jsonObject.put("total", 50000);
                    jsonObject.put("cartArray", new JSONArray(cartList));
                    jsonObject.put("key", "12345");

                    OutputStreamWriter writer = new OutputStreamWriter(httpURLConnection.getOutputStream());
                    writer.write(jsonObject.toString());
                    writer.flush();

                    cartList.clear();

                    return true;
                } catch (Exception e) {
                    // Your code to Log the error 

                    return false;
                }


            }
        }
}
公共类SendOrder扩展异步任务{
@凌驾
public void onPostExecute(布尔结果){
if(结果!=null&&result){
Toast.makeText(TestActivity.this,“订单成功”,Toast.LENGTH_LONG.show();
}
否则{
Toast.makeText(TestActivity.this,“顺序错误”,Toast.LENGTH_LONG.show();
}
}
@凌驾
受保护的布尔doInBackground(Void…params){
购物车1=新购物车(“1”,3);
cartList.add(cart1);
购物车2=新购物车(“2”,2);
cartList.add(cart2);
试一试{
URL=新URL(URL\u发送);
HttpURLConnection HttpURLConnection=(HttpURLConnection)url.openConnection();
httpURLConnection.setDoOutput(true);
httpURLConnection.setRequestMethod(“POST”);
setRequestProperty(“内容类型”、“应用程序/json”);
httpURLConnection.connect();
JSONObject JSONObject=新的JSONObject();
jsonObject.put(“idUser”,“1”);
jsonObject.put(“总计”,50000);
put(“cartArray”,新的JSONArray(cartList));
jsonObject.put(“键”,“12345”);
OutputStreamWriter writer=新的OutputStreamWriter(httpURLConnection.getOutputStream());
write(jsonObject.toString());
writer.flush();
cartList.clear();
返回true;
}捕获(例外e){
//您的代码将记录错误
返回false;
}
}
}
}

post logcat…使用Log.e?我找不到查看logcat结果的位置。我正在使用Genymotion。在android studio中单击下面的android监视器。Genymotion上有logcat打印?还是安卓工作室?没关系,我找到了。检查绿色android iconpost logcat…是否使用Log.e?我找不到查看logcat结果的位置。我正在使用Genymotion。在android studio中单击下面的android监视器。Genymotion上有logcat打印?还是安卓工作室?没关系,我找到了。检查绿色安卓图标。成功了。但是,我如何才能展示第二个祝酒词(在catch块中)。在同一onPostExecute()中,将AsyncTask更改为AsyncTask并使用返回文本;从后台。现在,在postExecute中,您将得到结果文本。这里还有一个答案。谢谢。成功了。但是,我如何才能展示第二个祝酒词(在catch块中)。在同一onPostExecute()中,将AsyncTask更改为AsyncTask并使用返回文本;从后台。现在,在postExecute中,您将得到结果文本。这里还有另一个答案。这个代码是有效的。谢谢。这个答案显示POST表单是否工作
Toast.makeText(TestActivity.this, "Order success", Toast.LENGTH_LONG).show();
Toast.makeText(TestActivity.this, "Server error", Toast.LENGTH_LONG).show();
public class SendOrder extends AsyncTask<Void,Void,Boolean> {

            @Override
            public void onPostExecute(Boolean result) {

                if(result != null && result) {

                   Toast.makeText(TestActivity.this, "Order success", Toast.LENGTH_LONG).show();
                }
                else {
                   Toast.makeText(TestActivity.this, "Order error", Toast.LENGTH_LONG).show();
                }
            }


            @Override
            protected Boolean doInBackground(Void... params) {
                Cart cart1 = new Cart("1", 3);
                cartList.add(cart1);
                Cart cart2 = new Cart("2", 2);
                cartList.add(cart2);
                try {
                    URL url = new URL(URL_SEND);
                    HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                    httpURLConnection.setDoOutput(true);
                    httpURLConnection.setRequestMethod("POST");
                    httpURLConnection.setRequestProperty("Content-Type", "application/json");
                    httpURLConnection.connect();

                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("idUser", "1");
                    jsonObject.put("total", 50000);
                    jsonObject.put("cartArray", new JSONArray(cartList));
                    jsonObject.put("key", "12345");

                    OutputStreamWriter writer = new OutputStreamWriter(httpURLConnection.getOutputStream());
                    writer.write(jsonObject.toString());
                    writer.flush();

                    cartList.clear();

                    return true;
                } catch (Exception e) {
                    // Your code to Log the error 

                    return false;
                }


            }
        }
}