0

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:

  1. 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)??

  1. Result.java --> select one of the results in list view, how to get details by searching database by posting id of the item?
  • Your question is kind of vague. What exactly are you having a problem with - the db query? Calling the web service? Displaying the data you get back? You have some code already, so what part is behaving incorrectly, and in what way? – nasch Feb 15 '15 at 17:48
  • @nasch thx for your reply. :) I tested the code in an emulator but the app crashed after I entered my search. So 1) trying to figure out whether my search.java has anything wrong, and 2) how to display search results list in another activity result.java? and enable selection of one result to view details item_selected.java? – cookiesncream Feb 15 '15 at 18:16
  • Clearly it has something wrong, since it crashed. What is the stack trace from logcat? – nasch Feb 15 '15 at 18:23
  • @nasch I have managed to troubleshoot finished my search.java, able to search successfully. Now to qn 2), do you know how to save the search results and display them as selectable results in result.java, with a search bar ontop of the listview, so that the user can edit his query in the search bar and search again? – cookiesncream Feb 16 '15 at 12:15
  • That's a lot of stuff packed into one question. I recommend picking one problem that you're having and either edit this question to reflect that, or post a new question. That's a lot easier than trying to answer all that at once. Remember to show what you've already tried, and explain what isn't working. – nasch Feb 16 '15 at 20:10
  • @nasch hi I have edited the question. My 2 main problems are listed above. So far I tried to run the app but it crashed when I try to search something. I think the problem lies in the php or the search.java's "if (success == 1) {...}" part. And, thanks for responding to my problem! – cookiesncream Feb 18 '15 at 16:19
  • For your #1, maybe something like [this](http://stackoverflow.com/questions/10407159/how-to-manage-start-activity-for-result-on-android) – Manuel Ramírez Feb 18 '15 at 17:38
  • Your logcat is the first place to go when your app crashes. That will tell you what the exception was and where it occurred. – nasch Feb 18 '15 at 21:01
  • @nasch hi how to save search results (fetch_assoc rows) and display them in listview of next activity? – cookiesncream Feb 21 '15 at 12:20
  • Look up how to do persistent storage, and how to use either ListView or RecyclerView. – nasch Feb 21 '15 at 17:45

1 Answers1

0

I solved my question by putting this within Search.java to test whether can search:

 @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());
                //save search data


                mCommentList = new ArrayList<HashMap<String, String>>();

                mComments = json.getJSONArray(TAG_POSTS);

               // JSONArray mComments = new JSONArray(TAG_POSTS);

                    // looping through all posts according to the json object returned
                for (int i = 0; i < mComments.length(); i++) {
                    JSONArray innerArray = mComments.optJSONArray(i);
                    for (int j = 0; j < innerArray.length(); j++) {
                        JSONObject c = innerArray.getJSONObject(j);
                        //gets the content of each tag
                        String title = c.getString(TAG_TITLE);
                        String address = c.getString(TAG_ADDRESS);
                        String brand = c.getString(TAG_BRAND);
                        String company = c.getString(TAG_COMPANY);
                        String id = c.getString(TAG_ID);
                        //so our JSON data is up to date same with our array list

                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();
                        map.put(TAG_TITLE, title);
                        map.put(TAG_ADDRESS, address);
                        map.put(TAG_BRAND, brand);
                        map.put(TAG_COMPANY, company);
                        map.put(TAG_ID, id);

                        mCommentList.add(map);
                    }
                }
                Intent r = new Intent(Search.this, Results.class);
                //either
                //r.putExtra("arraylist", mCommentList);
                // startActivityForResult(r, 5000);
               //or
                r.putExtra("searchquery", searchquery);
                startActivity(r);

                return json.getString(TAG_MESSAGE);
            }else{
                Log.d("Invalid Search!", json.toString());

                return json.getString(TAG_MESSAGE);

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

        return null;

    }

Then in Results.java:

public void updateJSONdata() {
        Bundle b = getIntent().getExtras();
        String searchquery = b.getString("searchquery");

        // 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());

        try {

            mResultList = new ArrayList<HashMap<String, String>>();

            mResults = json.getJSONArray(TAG_POSTS);

            // JSONArray mComments = new JSONArray(TAG_POSTS);

            // looping through all posts according to the json object returned
            for (int i = 0; i < mResults.length(); i++) {
                JSONArray innerArray = mResults.optJSONArray(i);
                for (int j = 0; j < innerArray.length(); j++) {
                    JSONObject c = innerArray.getJSONObject(j);
                    //gets the content of each tag
                    String title = c.getString(TAG_TITLE);
                    String address = c.getString(TAG_ADDRESS);
                    String brand = c.getString(TAG_BRAND);
                    String company = c.getString(TAG_COMPANY);
                    String id = c.getString(TAG_ID);
                    //so our JSON data is up to date same with our array list

                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put(TAG_TITLE, title);
                    map.put(TAG_ADDRESS, address);
                    map.put(TAG_BRAND, brand);
                    map.put(TAG_COMPANY, company);
                    map.put(TAG_ID, id);

                    mResultList.add(map);
                }
            }

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

    /**
     * Inserts the parsed data into our listview
     */

    private void updateList() {
         //if you choose the other method
        // final ArrayList<HashMap<String, String>> mResultList = (ArrayList<HashMap<String, String>>) getIntent().getSerializableExtra("arraylist");
        // System.out.println("...serialized data.."+mResultList);

        Bundle b = getIntent().getExtras();
        final String searchquery = b.getString("searchquery");

        ListAdapter adapter = new SimpleAdapter(this, mResultList,
                R.layout.single_result, new String[]{TAG_TITLE, TAG_ADDRESS, TAG_BRAND,
                TAG_COMPANY, TAG_ID}, new int[]{R.id.outlet, R.id.address, R.id.brand,
                R.id.company});
        setListAdapter(adapter);

        listView.setSelector(R.drawable.listselector);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                                            @Override
                                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                                // ListView Clicked item value
                                                Map<String, String> map = (Map<String, String>) mResultList.get(position);
                                                String record_id = map.get(TAG_ID);
                                                Intent r = new Intent(Results.this, Record.class);
                                                r.putExtra("key", record_id);
                                                r.putExtra("searchquery", searchquery);
                                                startActivity(r);
                                            }

                                        }
        );
    }