使用PHP和MY SQL从数据库中搜索和检索数据(在Android应用程序中)

时间:2015-12-18 11:28:18

标签: php android mysql

Iam开发一个购物应用程序,其中的项目以滚动视图显示。

单击其中一个项目必须获取必填字段,使用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。

任何帮助都会得到赞赏。

0 个答案:

没有答案