我想顯示在我的搜索如果它不會收到類似的結果我的話我輸入它應該敬酒我「沒有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 "";
}
}
發佈'AsyncTaskSearch'的代碼以獲得有關如何組織吐司的更好反饋,但我的評論仍然存在 - 您想在AsyncTask的onPostExecute中敬酒。 – Shark