douhui7136 2017-03-24 10:53
浏览 81

使用PHP中的$ _POST将数据从数据库解析到Android

i'm new to Android Programming, i'm making a app which has server side database that contains list of Drug-names with each Drug having their particular Reference ids . now what i want is when user search the Drug name by clicking the button it should run database search and return the value based on whether Drug is present in the database or not with the input's particular Reference ids. what should i do to do that.. i'm stuck here with the reference id problem it is not fetching the reference id just querying the Drug name?? Kindly give your suggestions if going wrong..

//This is my SecondActivity.java

    public class SecondActivity extends AppCompatActivity implements View.OnClickListener {

    final String LOG = "SecondActivity";
        Button btnCheck;
        EditText etDrugname;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_second);
            etDrugname = (EditText) findViewById(R.id.etDrugname);
            btnCheck = (Button) findViewById(R.id.btnCheck);
            btnCheck.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            HashMap postData = new HashMap();
            String Drugname = etDrugname.getText().toString();
           postData.put("txtDrugname", Drugname);
            PostResponseAsyncTask task1 = new PostResponseAsyncTask(SecondActivity.this, postData,
                    new AsyncResponse() {
                        @Override
                        public void processFinish(String s) {
                            Log.d(LOG, s);
                            if (s.contains("Drug found")) {

   Intent intent = new Intent(SecondActivity.this, DisplayActivity.class);

   intent.putExtra("Drugname", etDrugname.getText().toString());

startActivity(intent);
                            } else {
                                Intent intent = new Intent(SecondActivity.this, Display2Activity.class);
                                intent.putExtra("Drugname", etDrugname.getText().toString());
                                startActivity(intent);
                            }
                        }
                    });
            task1.execute("http://www.e-bioinformatics.net/db/utipedia/test/login2.php");
        }
    }

// This is my DisplayActivity.java, where i want my result to appear

`

ListView lv;
    //ListView PMID1;
    ArrayAdapter<String> adapter;
    String address = "http://www.e-bioinformatics.net/db/utipedia/test/login2.php";
    InputStream is = null;
    String line = null;
    String result = null;
    String[] data;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display);

        TextView result = (TextView) findViewById(R.id.result);
        result.setText(getIntent().getExtras().getString("Drugname"));

        lv = (ListView) findViewById(PMID);


        StrictMode.setThreadPolicy((new StrictMode.ThreadPolicy.Builder().permitNetwork().build()));
        getData();


        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
        lv.setAdapter(adapter);


    private void getData() {
        try {
            URL url = new URL(address);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");


            is = new BufferedInputStream(connection.getInputStream());

        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();

            while ((line = br.readLine()) != null) {
                sb.append(line + "
");
            }

            is.close();

            result = sb.toString();

        } catch (Exception e) {
            e.printStackTrace();
        }


        try {
            JSONArray ja = new JSONArray(result);
            JSONObject jo;
            data = new String[ja.length()];

            for (int i = 0; i < ja.length(); i++) {
                jo = ja.getJSONObject(i);
                data[i] = jo.getString("Referenceid");
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }


    }

//this is my php script

<?php
    $input= $_POST['txtDrugname'];
    $input = strtolower($input);

if($input == ''){
echo "OOPS! It seems the input section is left blank. <br>please submit the <b>drug name.</b>";
}else{

/*  $ucaredb = mysql_connect("localhost", "ebioinfo_ucare", "admin_ucare")
    or die(mysql_error());*/

    $utidb = mysql_connect("localhost", "ebioinfo_uti0912", "uti@097")
    or die(mysql_error());



    mysql_select_db("ebioinfo_utipedia",$utidb);

    /*
    $sql_antibiotics = "SELECT * FROM antibiotics"; 
    $result_antibiotics = mysql_query($sql_antibiotics, $ucaredb);
    $sql_resistancegene = "SELECT * FROM resistancegene"; 
    $result_resistancegene = mysql_query($sql_resistancegene, $ucaredb);*/

    $sql_drug = "SELECT * FROM UTIPEDIA"; 
    $result_drug = mysql_query($sql_drug, $utidb);




    $i=0;


        while ($row_drug = mysql_fetch_array($result_drug))
        {
            $resistance_drug = $row_drug['Drugname'];
            $resistance_drug = strtolower($resistance_drug);

            $resistance_ref = $row_drug['Referenceid'];
            $part = explode(',', $resistance_ref);


            if($input == $resistance_drug)
            {

                    $i=1;
                    echo "Drug found";
                    echo "<h2><b>$input</b> is resistant against <i>Escherichia coli.</i></h2> <br>For further details see the following bibliography: ";
                    //echo "$resistance_ref";
                                        print(json_encode($resistance_ref));





            }


        }if($i !=1){echo "$input is not found in the database!";}




    }
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
    • ¥15 乘性高斯噪声在深度学习网络中的应用
    • ¥15 运筹学排序问题中的在线排序
    • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
    • ¥30 求一段fortran代码用IVF编译运行的结果
    • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
    • ¥15 C++ 头文件/宏冲突问题解决
    • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
    • ¥50 安卓adb backup备份子用户应用数据失败
    • ¥20 有人能用聚类分析帮我分析一下文本内容嘛