I want to create an Android app to be a search engine that can
- search my remote database from server like MAMP,
- list the search results and
- select one of the results to view details.
I have already set up the database.
Search.java - the launch page that shows only a search bar, allows user to search:
public class Search extends Activity implements OnClickListener{
private EditText searchterm;
private Button mSubmit;
private SharedPreferences preferences;
private String preFileName="searchrequest"; //this is the Preference file Name
private String prefKey="searchterm"; //Key to store the User Name
// Progress Dialog
private ProgressDialog pDialog;
// JSON parser class
JSONParser jsonParser = new JSONParser();
//php login script
//localhost :
private static final String SEARCH_URL = "http://xxx.xxx.x.xxx:8888/searchdb/search.php";
//ids
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
searchterm = (EditText)findViewById(R.id.searchterm);
mSubmit = (Button)findViewById(R.id.searchnow);
mSubmit.setOnClickListener(this);
preferences=getSharedPreferences(preFileName, MODE_PRIVATE);
if(!preferences.getString(prefKey, "not_set").equals("not_set")){
prefKey.setText(preferences.getString(preFileName, "not_set"));
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.searchnow:
new SearchQuery().execute();
break;
default:
break;
}
}
public class SearchQuery extends AsyncTask<String,String,String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Search.this);
pDialog.setMessage("Checking for records...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
// Check for success tag
int success;
String searchquery = searchterm.getText().toString();
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("searchquery", searchquery));
Log.d("request!", "starting");
//Posting user data to script
JSONObject json = jsonParser.makeHttpRequest(
SEARCH_URL, "POST", params);
// full json response
Log.d("Search attempt", json.toString());
// json success element
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Successful Search!", json.toString());
//need help on how to save search data
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
Editor editor = pref.edit();
editor.putString("searchquery", searchquery);
editor.apply();
Intent i = new Intent(Search.this, Result.class);
startActivity(i);
return json.getString(TAG_MESSAGE);
}else{
Log.d("Invalid query. Please try again.", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
pDialog.dismiss();
if (file_url != null){
Toast.makeText(Search.this, file_url, Toast.LENGTH_LONG).show();
}
}
}
}
Search.php:
<?php
@ $db = new mysqli('localhost','username','password','db');
if (mysqli_connect_errno()) {
echo 'Error: Could not connect to database.
Please try again later.';
exit;
}
if (!empty($_POST)) {
$query_params = array(
$term = $_POST['searchquery']
);
$words = explode(" ", trim($term));
$termArray = array();
foreach($words as $word){
if(!empty($word)){
$termArray[] = "+$word";
}
}
$searchinput = implode(" ", $termArray);
$query = "SELECT *
FROM repairsvc
WHERE MATCH(title,address,cat,brand,company)
AGAINST ('".$searchinput."' IN BOOLEAN MODE)
ORDER BY title ASC";
try {
$result = $db->query($query);
}
catch (PDOException $ex) {
// For testing, you could use a die and message.
//die("Failed to run query: " . $ex->getMessage());
//or just use this use this one to product JSON data:
$response["success"] = 0;
$response["message"] = "Database Error1. Please Try Again!";
die(json_encode($response));
}
//This will be the variable to determine whether or not the user's information is correct.
//we initialize it as false.
$num_results = $result->num_rows;
if ($num_results == 0)
{
$search_ok = false;
}
else
{$search_ok = true;}
if ($search_ok) {
$response["success"] = 1;
$response["message"] = "Search Successful!";
$response["records"] = array();
$records = array();
while ($row = $result->fetch_assoc()) {
$records[] = array('title'=>$row["title"], 'address'=>$row["address"],
'company'=>$row["company"], 'id'=>$row["id"], 'brand'=>$row["brand"]); // push into the array
}
var_dump($records);
// foreach ($records as $row) {
// echo "Outlet: ", $row['title'], "; Address: ", $row['address'];
// }
//update our repsonse JSON data
array_push($response["records"], $records);
// echoing JSON response
echo json_encode($response);
} else {
$response["success"] = 0;
$response["message"] = "Invalid Search! Please try again.";
die(json_encode($response));
}
} else {
?>
<h1>Search</h1>
<form name="form1" action="search.php" method="post">
Enter Search:<br />
<input type="text" name="searchquery" id="searchquery" placeholder="Search a repair service"/>
<br /><br />
<input type="submit" value="Search Now" name="completedsearch" />
</form>
<?php
}
?>
Problems:
- How to save the search results from Search.java and let another activity Result.java to list results?
Save this from the php in first activity:
while ($row = $result->fetch_assoc()) {
$records[] = array('title'=>$row["title"], 'address'=>$row["address"],
'company'=>$row["company"], 'id'=>$row["id"], 'brand'=>$row["brand"]); // push into the array
}
//update our repsonse JSON data
array_push($response["records"], $records);
echo json_encode($response);
and put as listview in second activity (Result.java)??
- Result.java --> select one of the results in list view, how to get details by searching database by posting id of the item?