2017-05-23 43 views
-1

我想顯示在我的搜索如果它不會收到類似的結果我的話我輸入它應該敬酒我「沒有Result.but問題是,它獲得數據後0.5秒。當我點擊按鈕,首先它的祝酒詞沒有結果的話,得到的數據和recyclerview.I顯示他們想,如果沒有類似的文字是我敬酒在最後沒有結果;Toast消息,如果沒有數據從服務器

String text = txtsearch.getText().toString(); 
      new AsyncTaskSearch("http://192.168.1.100/afgApp/search.php", text).execute(); 
        final Timer timer = new Timer(); 
        timer.scheduleAtFixedRate(new TimerTask() { 
         @Override 
         public void run() { 
          runOnUiThread(new Runnable() { 
           @Override 
           public void run() { 
            if (!Activity_Search.data.equals("")) { 
             Toast.makeText(Activity_Search.this, data, Toast.LENGTH_SHORT).show(); 
             timer.cancel(); 
            }else { 
             Toast.makeText(Activity_Search.this, "No results", Toast.LENGTH_SHORT).show(); 
            } 
           } 
          }); 
         } 
        },1,500); 
        Activity_Search.data = ""; 
       } 

//Asynctask.java

public class AsyncTaskSearch extends AsyncTask{ 
    public String link=""; 
    public String text=""; 


    public AsyncTaskSearch(String link,String text){ 

     this.link=link; 
     this.text=text; 

    } 
    @Override 
    protected Object doInBackground(Object[] params) { 
     try{ 
      String data= URLEncoder.encode("text","UTF8")+"="+URLEncoder.encode(text,"UTF8"); 

      URL url=new URL(link); 
      URLConnection connection=url.openConnection(); 

      connection.setDoOutput(true); 
      OutputStreamWriter writer=new OutputStreamWriter(connection.getOutputStream()); 
      writer.write(data); 
      writer.flush(); 
      BufferedReader reader=new BufferedReader(new InputStreamReader(connection.getInputStream())); 
      StringBuilder builder=new StringBuilder(); 

      String line=null; 

      while((line=reader.readLine())!=null){ 
       builder.append(line); 

      } 

      Activity_Search.data=builder.toString(); 


     }catch (Exception e){ 

     } 
     return ""; 
    } 
} 
+0

發佈'AsyncTaskSearch'的代碼以獲得有關如何組織吐司的更好反饋,但我的評論仍然存在 - 您想在AsyncTask的onPostExecute中敬酒。 – Shark

回答

0

我不知道你是如何使用你的AysncTask的。試試這個代碼,它對我來說工作得很好:

private class AsyncGetClass extends AsyncTask<Object, Void, String> { 
    ProgressDialog progressDialog; 
    String TAG, url; 
    AsyncHttpResponseHandler asyncHttpResponseHandler; 
    Context context; 

    public AsyncGetClass(Context context) { 
     // TODO Auto-generated constructor stub 
     this.context = context; 

    } 

    @Override 
    protected void onPreExecute() { 
     // TODO Auto-generated method stub 
     super.onPreExecute(); 
     if (!isProgressHiding) 
      if (progressDialog == null) { 
       progressDialog = createProgressDialog(context); 
       progressDialog.show(); 
      } else { 
       progressDialog.show(); 
      } 
    } 

    @SuppressWarnings("unchecked") 
    @Override 
    protected String doInBackground(Object... params) { 
     // TODO Auto-generated method stub 
     TAG = (String) params[0]; 
     url = (String) params[1]; 
     asyncHttpResponseHandler = (AsyncHttpResponseHandler) params[3]; 

     InputStream is = null; 
     String result = ""; 

     // HTTP post 
     try { 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpGet httpGet = new HttpGet(url); 
      Log.e(TAG, url); 
      HttpResponse response = httpclient.execute(httpGet); 

      HttpEntity entity = response.getEntity(); 
      is = entity.getContent(); 
     } catch (Exception e) { 
      Log.e(TAG, "Error in http connection " + e.toString()); 
      Toast.makeText(Activity_Search.this, "No results", Toast.LENGTH_SHORT).show(); 
     } 

     // Convert response to string 
     try { 
      BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 
      is.close(); 
      result = sb.toString(); 
      // Log.i(TAG + ".result", result); 
      return result; 
     } catch (Exception e) { 
      Log.e(TAG, "Error converting result " + e.toString()); 
      return null; 
     } 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     // TODO Auto-generated method stub 
     super.onPostExecute(result); 
     if (!isProgressHiding) 
      if (progressDialog != null) 
       progressDialog.dismiss(); 
     if (result != null) 
      try { 
       Log.i(TAG, result); 
       asyncHttpResponseHandler.onSuccess(result); 
      } catch (Exception e) { 
       Log.e(TAG, "Must implement the interface " + e.toString()); 
      } 
    } 

} 
+0

我發佈了我的asynctask代碼@Zohaib Hassan – mahdi

+0

如果你是AysncTask工作正常,並顯示你的預期結果,那麼只需在你的AysncTask類中覆蓋方法「OnPostExecute()」,並刪除你的「Toast」顯示代碼和條件從「doInBackground ()「方法並放入」onPostExecute()「方法。 –

+0

把這個吐司代碼放在「AysncTask」類中,而不是放在你的主類中。 –