1

I have 2 below that run a search functionality but Im skipping anywhere between 200-1100.

it uses the openLibrary Search API as well as referencing my own SQL server on AWS, its fully functional but is just skipping so many frames

package tk.bookpass.bookpass;
import java.sql.*;
import android.content.*;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.view.View;
import android.widget.*;
import android.os.*;
import java.io.IOException;
import java.util.*;

public class SearchResults extends AppCompatActivity {
    int size=0;
    String ResultISBN1;
    String ResultISBN2;
    String ResultISBN3;
    ArrayList<String> resultsStore = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_results);

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitNetwork().build();
        StrictMode.setThreadPolicy(policy);

        //seekBar Code
        SeekBar seekBar = findViewById(R.id.seekBar);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                double pageNum = size;
                pageNum = Math.floor((pageNum / 3));
                TextView pageNumber = findViewById(R.id.pageNum);
                pageNumber.setText("Page: "+ Integer.toString(seekBar.getProgress())+ " Of " +(int)pageNum);


                displayResults(resultsStore);
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {}
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {}
        });


        //cardView to details
        CardView cardView1 = findViewById(R.id.cardView1);
        cardView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent regIntent = new Intent(SearchResults.this,BookDetails.class);
                SearchResults.this.startActivity(regIntent);
            }
        });
        CardView cardView2 = findViewById(R.id.cardView2);
        cardView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent regIntent = new Intent(SearchResults.this,BookDetails.class);
                SearchResults.this.startActivity(regIntent);
            }
        });
        CardView cardView3 = findViewById(R.id.cardView3);
        cardView3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent regIntent = new Intent(SearchResults.this,BookDetails.class);
                SearchResults.this.startActivity(regIntent);
            }
        });

    //run search

        ImageButton imageButton = findViewById(R.id.imageButton);
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent regIntent = new Intent (SearchResults.this, SearchResults.class);
         //start
                size=0;
                startSearch();
            }
        });
    }


    private void startSearch(){

        EditText searchText = findViewById(R.id.searchText);
        String searchingTerm = searchText.getText().toString();

        ArrayList<String> searchStore = new ArrayList<>();
        try{
            searchStore =getSearchResults(searchingTerm);
        }
        catch(IOException e)
        {
                    //do nothing
        }
        resultsStore = checkResults(searchStore);
        size = resultsStore.size();

        setUpSeekBar();
        displayResults(resultsStore);
    }

    private ArrayList<String> checkResults(ArrayList<String> searchStore) {
        LinkedHashSet<String> resultsHash = new LinkedHashSet<String>();
            try {
                Class.forName("com.mysql.jdbc.Driver");
                Connection con = DriverManager.getConnection(//removed);
                Statement stmt = con.createStatement();
                ResultSet rs = stmt.executeQuery("select ISBN_fk from Book_Stock");
                while (rs.next())
                    resultsHash.add(rs.getString("ISBN_FK"));

                con.close();

            } catch (Exception e) {
        }
        ArrayList<String> resultsStore = new ArrayList<String>(resultsHash);
        ArrayList<String> duplicatevalues = new ArrayList<String>();
        for (String finalval : resultsStore) {
            if (searchStore.contains(finalval)) {
                duplicatevalues.add(finalval);
            }
        }
        return duplicatevalues;
    }

    private void displayResults(ArrayList<String> resultsStore)
    {
        SeekBar seekBar = findViewById(R.id.seekBar);
        int position = seekBar.getProgress();
        fillCardOne(position, resultsStore);
        fillCardTwo(position, resultsStore);
        fillCardThree(position, resultsStore);

    }

    private void setUpSeekBar() {
        SeekBar seekBar = findViewById(R.id.seekBar);
        seekBar.getThumb();
        seekBar.setProgress(0);
        if (size < 3) {

        }
        else {
            double pageNum = size;
            pageNum = Math.floor((pageNum / 3));
            seekBar.setMax((int)pageNum);
            TextView pageNumber = findViewById(R.id.pageNum);
            pageNumber.setText("Page: "+ Integer.toString(seekBar.getProgress())+ " Of " +(int)pageNum);
            seekBar.setVisibility(seekBar.VISIBLE);
        }
    }

    private void fillCardOne(int position, ArrayList<String> resultsStore){
        int resultsNumber = (0+(position*3));
        String Title=null;
        String Author=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection(//removed);
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("select * from Book_Details where ISBN = "+resultsStore.get(resultsNumber));
            while (rs.next())
            {
                ResultISBN1 = rs.getString("ISBN");

                Title =rs.getString("Title");
                Author =rs.getString("Author");
            }
            con.close();

        } catch (Exception e) {;
        }

        TextView bookName = findViewById(R.id.bookName);
        bookName.setText("Title: "+Title); //title

        TextView authorName = findViewById(R.id.authorName);
        authorName.setText("Author: "+Author);
    }
    private void fillCardTwo(int position, ArrayList<String> resultsStore){
        int resultsNumber = (1+(position*3));
        String Title=null;
        String Author=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection(//removed);
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("select * from Book_Details where ISBN = "+resultsStore.get(resultsNumber));
            while (rs.next())
            {
                ResultISBN2 = rs.getString("ISBN");

                Title =rs.getString("Title");
                Author =rs.getString("Author");
            }
            con.close();

        } catch (Exception e) {;
        }

        TextView bookName2 = findViewById(R.id.bookName2);
        bookName2.setText("Title: "+Title); //title

        TextView authorName2 = findViewById(R.id.authorName2);
        authorName2.setText("Author: "+Author);

    }
    private void fillCardThree(int position, ArrayList<String> resultsStore){
        int resultsNumber = (2+(position*3));
        String Title=null;
        String Author=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection(//removed);
            Statement stmt = con.createStatement();
            ResultSet rs = stmt.executeQuery("select * from Book_Details where ISBN = "+resultsStore.get(resultsNumber));
            while (rs.next())
            {
                ResultISBN3 = rs.getString("ISBN");

                Title =rs.getString("Title");
                Author =rs.getString("Author");
            }
            con.close();

        } catch (Exception e) {;
        }

        TextView bookName3 = findViewById(R.id.bookName3);
        bookName3.setText("Title: "+Title); //title

        TextView authorName3 = findViewById(R.id.authorName3);
        authorName3.setText("Author: "+Author);

    }
    private ArrayList<String> getSearchResults(String searchingTerm)throws IOException
    {
        ArrayList<String> searchStore = new ArrayList<String>();
        searchingTerm =searchingTerm.replace(" ", "%20");
        searchStore = search2.reader(searchingTerm);
        return searchStore;
    }
}

class2

package tk.bookpass.bookpass;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import android.util.Log;

public class search2 {
    public static ArrayList<String> reader(String searchingTerm) throws IOException {
        int switches = 0;
        ArrayList<String> searchStore = new ArrayList<String>();
        URL search = new URL("http://openlibrary.org/search.json?q=" + searchingTerm);//search string
        BufferedReader in = new BufferedReader(
                new InputStreamReader(search.openStream()));
        String inputLine;
        while ((inputLine = in.readLine()) != null) //read file
        {
            if (switches == 0) {
                if (inputLine.contains("\"isbn\": [")) {
                    switches = 1;
                }
            } else if (switches == 1) {
                switches = 0;
                inputLine = inputLine.replace("\"", "");
                inputLine = inputLine.replace(",", "");
                inputLine = inputLine.replace(" ", "");
                String ISBN = "0";
                try {
                        searchStore.add(inputLine);
                } catch (Exception e) {
                    switches = 1; //if the ISBN is of the number letter format run again on the next line
                }
            }
        }
        return searchStore;
    }
}

any help would be appreciated I need to keep the search functions and the same basic layout.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
tom j
  • 25
  • 2

1 Answers1

0

Step #1: Get rid of these lines:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitNetwork().build();
StrictMode.setThreadPolicy(policy);

Step #2: Fix the NetworkOnMainThreadException crashes that arise, by moving your network I/O to background threads. See this Stack Overflow question for various solutions, along with any decent book or course on Android app development.

Along the way, please reconsider your use of JDBC. That is not designed for use on mobile clients. Wrap your database in a Web service, then consume that Web service from your Android app.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • yes that JDBC is a temporary thing. il take a look into the StrictMode but would that really be whats slowing it down? – tom j Feb 18 '18 at 19:17
  • @tomj: The 1100 frames complaint is because you are doing too much work on the main application thread. You are doing network I/O on the main application thread. So, stop doing network I/O on the main application thread. – CommonsWare Feb 18 '18 at 19:18