如果搜索结果不匹配,则显示在下一页或Toast中未找到

时间:2017-04-13 15:26:55

标签: android

我正在做的是如果找到的数据然后在网格视图中显示数据,则从Web服务中搜索数据。 这是我的普通搜索代码,如果匹配则在编辑文本中输入,然后在下一页网格视图中显示结果。 如果没有找到数据显示没有找到数据,我想要的是什么。 如果我键入任何随机工作,如 knydwf ,那么它会显示Web服务上的所有数据。我想要的是显示没有找到数据的吐司 这是我的代码

/********************for normal search ************************************/

class Get_normal_search extends AsyncTask<String, String, String>{

    JSONObject jsonobject;
    JSONArray jsonarray;

    ProgressDialog mProgressDialog;
    @Override
    protected void onPreExecute() {
        mProgressDialog = new ProgressDialog(Frnt_mapActivity.this);
        mProgressDialog.setMessage("Loading...");
        mProgressDialog.setCancelable(false);
        mProgressDialog.show();
        super.onPreExecute();
    }

    // Slow Implementation
    private String inputStreamToString(InputStream is) {
        String s = "";
        String line = "";
        // Wrap a BufferedReader around the InputStream
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        // Read response until the end
        try {
            while ((line = rd.readLine()) != null) {
                s += line;
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // Return full string
        return s;
    }


    @SuppressLint("NewApi")
    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub

        normlSearchList = new ArrayList<NormalSearchBeams>();

        //  String gt_ky = edit_search.getText().toString().trim();
        String gt_ky = edit_search.getText().toString().replaceAll("\\s+","");

        System.out.println("CK@@@@@@"+gt_ky.trim());
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost("http://towncitycards.com/webservice_action.php?action=search_box&search_term="+gt_ky.trim().toString());
        try{

            HttpResponse response = httpClient.execute(httpPost);
            String data = inputStreamToString(response.getEntity().getContent());
            jsonobject = new JSONObject(data);
            jsonarray = new JSONArray(jsonobject.getString("product"));

            for(int j=0; j<jsonarray.length();j++){
                JSONObject itemobj = jsonarray.getJSONObject(j);
                //SHOW SEARCH RESULT
                NormalSearchBeams normalsearchItems = new NormalSearchBeams();
                normalsearchItems.setId(itemobj.getString("ID"));
                normalsearchItems.setPost_title(itemobj.getString("post_title"));
                normalsearchItems.setImage(itemobj.getString("image"));
                normalsearchItems.setDiscount(itemobj.getString("discount"));

                //imglist.add(itemobj.getString("image"));
                normlSearchList.add(normalsearchItems);

            }

        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);


        try{
            Intent in = new Intent(Frnt_mapActivity.this, Norml_SearchActivity.class);
            startActivity(in);
        }
        catch (Exception e) {
            // TODO: handle exception
        }
        if(mProgressDialog!=null && mProgressDialog.isShowing()){
            mProgressDialog.dismiss();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

试试这个:

class Get_normal_search extends AsyncTask<String, String, String>{

    JSONObject jsonobject;
    JSONArray jsonarray;

    ProgressDialog mProgressDialog;
    @Override
    protected void onPreExecute() {
        mProgressDialog = new ProgressDialog(Frnt_mapActivity.this);
        mProgressDialog.setMessage("Loading...");
        mProgressDialog.setCancelable(false);
        mProgressDialog.show();
        super.onPreExecute();
    }

    // Slow Implementation
    private String inputStreamToString(InputStream is) {
        String s = "";
        String line = "";
        // Wrap a BufferedReader around the InputStream
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        // Read response until the end
        try {
            while ((line = rd.readLine()) != null) {
                s += line;
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // Return full string
        return s;
    }


    @SuppressLint("NewApi")
    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub

        normlSearchList = new ArrayList<NormalSearchBeams>();

        //  String gt_ky = edit_search.getText().toString().trim();
        String gt_ky = edit_search.getText().toString().replaceAll("\\s+","");

        System.out.println("CK@@@@@@"+gt_ky.trim());
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost("http://towncitycards.com/webservice_action.php?action=search_box&search_term="+gt_ky.trim().toString());
        try{

            HttpResponse response = httpClient.execute(httpPost);
            String data = inputStreamToString(response.getEntity().getContent());
            jsonobject = new JSONObject(data);
// check if you have success for the search            
if(jsonobject.has ("success")){
            jsonarray = new JSONArray(jsonobject.getString("product"));

            for(int j=0; j<jsonarray.length();j++){
                JSONObject itemobj = jsonarray.getJSONObject(j);
                //SHOW SEARCH RESULT
                NormalSearchBeams normalsearchItems = new NormalSearchBeams();
                normalsearchItems.setId(itemobj.getString("ID"));
                normalsearchItems.setPost_title(itemobj.getString("post_title"));
                normalsearchItems.setImage(itemobj.getString("image"));
                normalsearchItems.setDiscount(itemobj.getString("discount"));

                //imglist.add(itemobj.getString("image"));
                normlSearchList.add(normalsearchItems);

            }
} else{
Toast.makeText(getActivity(), "Data Not Found",
   Toast.LENGTH_LONG).show();
}

        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);


        try{
            Intent in = new Intent(Frnt_mapActivity.this, Norml_SearchActivity.class);
            startActivity(in);
        }
        catch (Exception e) {
            // TODO: handle exception
        }
        if(mProgressDialog!=null && mProgressDialog.isShowing()){
            mProgressDialog.dismiss();
        }
    }
}