【发布时间】:2015-12-18 11:28:18
【问题描述】:
我正在开发一个购物应用程序,其中商品以滚动视图显示。
点击其中一项必须填写必填字段,使用 PHP 和 MY SQL 在数据库中搜索并在应用程序中显示确切的产品。
问题:
我可以将值传递给数据库,如果我在数据库中搜索并显示它,则值返回 null。
请找到Java类和php文件。
FullImage.java:
public class FullImage extends AppCompatActivity
{
ImageView fullimage;
TextView name, des, price;
private ProgressDialog progressDialog;
String model_grid, name_grid, price_grid, name_full = null, price_full = null, image_full;
ArrayList < RowItem > val_grid_full = new ArrayList < > ();
RowItem row1_grid = new RowItem();
URL url = null;
BufferedReader reader;
String text;
Context context_tab1;
String rate = "Rs ";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.full_image);
// get intent data
Intent in = getIntent();
// Selected image id
model_grid = in .getExtras().getString("model");
name_grid = in .getExtras().getString("name");
price_grid = in .getExtras().getString("price");
fullimage = (ImageView) findViewById(R.id.image_full);
name = (TextView) findViewById(R.id.text_name);
des = (TextView) findViewById(R.id.text_des);
price = (TextView) findViewById(R.id.price_full);
new DataFromServer().execute();
}
public class DataFromServer extends AsyncTask <String, Void, String>
{
@Override
protected void onPreExecute()
{
progressDialog = ProgressDialog.show(FullImage.this, "", "Please wait...");
}
@Override
protected String doInBackground(String...params)
{
try
{
url = new URL("http://xxx");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Host", "xxx.com");
Uri.Builder builder = new Uri.Builder()
.appendQueryParameter("m", model_grid)
.appendQueryParameter("n", name_grid)
.appendQueryParameter("p", price_grid);
String query = builder.build().getEncodedQuery();
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush();
writer.close();
os.close();
conn.connect();
reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
text = sb.toString();
}
catch (Exception ex)
{
ex.toString();
}
finally
{
try
{
reader.close();
}
catch (Exception ex)
{
ex.toString();
}
}
return text;
}
@Override
protected void onPostExecute(String text)
{
//Close progress dialog
progressDialog.dismiss();
JSONArray ja;
try
{
ja = new JSONArray(text);
int len = ja.length();
for (int i = 0; i < len; i++)
{
JSONObject json_data = ja.getJSONObject(i);
name_full = json_data.getString("name");
image_full = json_data.getString("image");
price_full = json_data.getString("price");
RowItem rData1 = new RowItem();
rData1.setFull_grid_name(name_full);
rData1.setFull_grid_image(image_full);
rData1.setFull_grid_price(price_full);
val_grid_full.add(rData1);
name.setText(name_full);
price.setText(rate + (price_full));
String url = "xxx" + image_full;
Picasso.with(context_tab1).load(url).into(fullimage);
}
}
catch (Exception e)
{
e.toString();
}
// grid_shopping.setAdapter(new GridAdapter_tab1(Grid_tab1.this,val_grid_tab1));
}
}
}
PHP.php:
<?php
include('con.php');
$model = $_POST['m'];
$price = $_POST['p'];
$name = $_POST['n'];
$q=mysql_query("SELECT oc_product_description.name,oc_product.image,oc_product.price,oc_product.model FROM oc_product INNER JOIN oc_product_description ON oc_product.product_id = oc_product_description.product_id WHERE model = '$model' and price = '$price' and name = '$name'");
while($e2=mysql_fetch_array($q)){
$output[]=$e2;
}
print(json_encode($output));
?>
请注意,上面的 sql 查询工作正常。
仅当我在查询中传递 POST 值时,它才返回 null。
任何帮助将不胜感激。
【问题讨论】:
-
试试这个演示,它可以帮助您更多地从服务器获取数据的最佳库:github.com/mcxiaoke/android-volley
-
首先停止使用 mysql_
函数。其中大部分已在 PHP 7.0 中删除。我会推荐使用 MySQL PDO 类。有关 PDO 中的示例,请查看此链接 php.net/manual/en/pdostatement.fetchall.php