Android Mysql实时搜索

时间:2018-11-07 22:52:44

标签: java android retrofit

我想对我的项目实施searchview,但出现错误:JSON文档未完全使用。请帮助我。在这种情况下,由于fetchSearch(“”);我只调用了所有项目;但是每次我遇到错误。对不起我的英语不好,但我尽我所能解释

public class SearchActivity extends AppCompatActivity {

    MaterialSearchBar materialSearchBar;
    RecyclerView recyclerView;
    List<Search> searchList;
    SearchAdapter adapter;
    ITezBazarApi mService;
    List<String> suggestList = new ArrayList<>();
    ProgressBar searchProgress;

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

        mService = Common.getAPI();
        searchProgress = (ProgressBar)findViewById(R.id.searchProgress);
        materialSearchBar = (MaterialSearchBar)findViewById(R.id.searchBar);
        recyclerView = (RecyclerView)findViewById(R.id.searchList);
        recyclerView.setLayoutManager(new GridLayoutManager(this,2));
        recyclerView.setHasFixedSize(true);
        materialSearchBar.setHint("Axtar");
        materialSearchBar.setCardViewElevation(10);
        fetchSearch("");
    }

    private void fetchSearch(String key) {
        Call<List<Search>> call = mService.search(key);
        call.enqueue(new Callback<List<Search>>() {
            @Override
            public void onResponse(Call<List<Search>> call, Response<List<Search>> response) {
                searchProgress.setVisibility(View.GONE);
                searchList = response.body();
                adapter = new SearchAdapter(searchList,SearchActivity.this);
                recyclerView.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(Call<List<Search>> call, Throwable t) {
                Toast.makeText(SearchActivity.this,""+t.toString(),Toast.LENGTH_SHORT).show();
            }
        });
    }

我的模特:

public class Search {
    @SerializedName("id") private int id;
    @SerializedName("name") private String name;
    @SerializedName("link") private String link;

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getLink() {
        return link;
    }
}

ITezBazarApi:

@GET("search.php")
    Call<List<Search>> search(@Query("key") String keyword);

RetrofitClient:

public class RetrofitClient {
    private static Retrofit retrofit = null;

    public static Retrofit getClient(String baseUrl){
        Gson gson = new GsonBuilder()
                .setLenient()
                .create();
        if (retrofit == null){
            retrofit = new Retrofit.Builder().baseUrl(baseUrl).addConverterFactory(GsonConverterFactory.create(gson))
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .build();
        }
        return retrofit;
    }
}

public static ITezBazarApi getAPI(){
        return RetrofitClient.getClient(BASE_URL).create(ITezBazarApi.class);
    }

Php:

public function search(){
        //$type = $_GET['users'];

        if (isset($_GET['key'])) {
            $key = $_GET["key"];
            //if ($type == 'users') {
            $query = "SELECT * FROM Menu WHERE name LIKE '%$key%'";
            $result = mysqli_query($this->conn, $query);
            $response = array();
            while( $row = mysqli_fetch_assoc($result) ){
                array_push($response,
                    array(
                        'id'=>$row['id'],
                        'name'=>$row['name'],
                        'link'=>$row['link'])
                );
            }
            echo json_encode($response);
            //}
        } else {
            //if ($type == 'users') {
            $query = "SELECT * FROM Menu";
            $result = mysqli_query($this->conn, $query);
            $response = array();
            while( $row = mysqli_fetch_assoc($result) ){
                array_push($response,
                    array(
                        'id'=>$row['id'],
                        'name'=>$row['name'],
                        'link'=>$row['link'])
                );
            }
            echo json_encode($response);
            //}
        }

        mysqli_close($this->conn);
    }

0 个答案:

没有答案