本文介绍了如何将数组和变量从PHP发送到Android应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要帮助.曾经见过类似的问题,但我仍然做不到.所以我发布了我的.到目前为止,PHP代码运行良好.我只是不知道如何获取数组.我如何从php获取数组并将其在我的alertdialog中列出,如下所示:
I need help.Have seen similar questions but I cant still do it. So I post mine.php codes runs well so far. Ijust dont know how to get the array.How can I get the array from php and list it in my alertdialog like this:
soy sauce - $1
onion - $1
total= $2
这是否可能?或者,请告诉我在此处添加的内容.谢谢您的时间.任何形式的帮助将不胜感激.
Or is this possible or not? Or maybe please tell me what to add in here.Thanks for your time in advance. Any form of help would be appreciated.
这是我的代码:
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";
?>
推荐答案
示例:getArray.php
example:getArray.php
<? echo json_encode($your_array);?>
并且在android中使用例如改造库(http客户端)
and in android use for example retrofit library(http client)
interface phpApi{
@GET("url/getArray.php")
Call<YourObject> getArray();
}
这篇关于如何将数组和变量从PHP发送到Android应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!