【发布时间】:2020-03-15 23:58:16
【问题描述】:
我需要帮助。 见过类似的问题,但我还是做不到。所以我发布我的。 到目前为止,php 代码运行良好。我只是不知道如何获取数组。 如何从 php 获取数组并将其列在我的 alertdialog 中,如下所示:
soy sauce - $1
onion - $1
total= $2
或者这是否可能?或者,也许请告诉我要在此处添加什么。 提前感谢您的时间。任何形式的帮助都将不胜感激。
这是我的代码:
public class updateData2 extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
ArrayList<NameValuePair> postVars = new ArrayList<>();
postVars.add(new BasicNameValuePair("JSON", String.valueOf(EverythingJSON)));
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://abcabc.com/buy_now.php");
try {
httppost.setEntity(new UrlEncodedFormEntity(postVars));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
try {
HttpResponse response = httpclient.execute(httppost);
String responseBody = EntityUtils.toString(response.getEntity());
} catch (ClientProtocolException e) {
e.printStackTrace();
Log.v("MAD", "Error sending... ");
} catch (IOException e) {
e.printStackTrace();
Log.v("MAD", "Error sending... ");
}
return null;
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(Cart.this,EverythingJSON.toString(), Toast.LENGTH_LONG).show();
AlertDialog.Builder builder = new AlertDialog.Builder(Cart.this);
builder.setTitle("ORDER:");
builder.setMessage(EverythingJSON.toString());
builder.setPositiveButton("Confirm",
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
}
});
builder.show();
}
}
php
<?php
require "conn.php";
$total = 0;
// $ing = array("ampalaya", "soy sauce");
$JSON_Received = $_POST["JSON"];
$obj = json_decode($JSON_Received, true);
$products_name_array = array();
$products_price_array = array();
// foreach($ing as $value){
foreach($obj['ques'] as $value){
$sql="select MIN(product_price) , product_id , store_id , product_name,product_unit_of_measure , product_price ,product_stock from products where product_name like '%$value%'";
$result2 = mysqli_query($conn, $sql);
if(mysqli_num_rows($result2) > 0){
while($row = mysqli_fetch_assoc($result2)){
$product_id=$row['product_id'];
$store_id=$row['store_id'];
$product_name=$row['product_name']; $products_name_array[] = $product_name;
$product_unit_of_measure=$row['product_unit_of_measure'];
$product_price=$row['product_price']; $products_price_array[] = $product_price;
$total = $total+$product_price;
}}
}
foreach($products_name_array as $key => $value) {
echo $products_name_array[$key] . " - " . "$" . $products_price_array[$key] . ".00" ."<br>";
}
echo "</br>";
echo "</br>";
echo "TOTAL IS: ".$total. ".00";
?>
【问题讨论】:
-
添加 URL 调用的响应。
-
到安卓工作室?我想你的安卓应用。
-
@Rj_Innocent_Coder 请至少给我一些提示,先生?我是新来的。提前谢谢你。
-
在 onPostExecute 方法中打印结果变量并共享输出
-
有人帮我吗?请T.T
标签: php android arrays android-alertdialog