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!";}
}