如何将数组和变量从PHP发送到Android应用程序?

我需要帮助。 曾经看到过类似的问题,但我仍然做不到。所以我发布了我的。 到目前为止,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"; 
     ?>
zjy99 回答:如何将数组和变量从PHP发送到Android应用程序?

示例: getArray.php

<? echo json_encode($your_array);?>

并且在android中使用例如改造库(http客户端)

interface phpApi{
   @GET("url/getArray.php")
   Call<YourObject> getArray();
 }
本文链接:https://www.f2er.com/3067049.html

大家都在问