[英]The asynctask part of my code closes/stops the app whenever I click the back/home button
[英]Whats wrong with this code? Whenever i try to click the button my application just stops responding? this is a post request code
這是我的發帖請求代碼。 請幫我調試一下。 謝謝。 我可以運行我的應用程序,但不會出錯。
public void ibutton4Click()
{
{
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("10.0.0.1/cgi-bin/ForwardPress.cgi");
try {
HttpResponse response = client.execute(post);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
startActivity(new Intent(this,MainActivity.class));
}
}
}
有關NetworkOnMainThread錯誤以及為什么會這樣的更多信息,
1. 設計和交互中的響應式UI
2. 有關Adroid類的更多信息
3. 有用的SO鏈接
要在單獨的線程中獲取數據 ,
1.編寫自定義Asynctasks
:
AsyncTask開發參考
Android AsyncTask示例
要么
2.使用類似AsyncHttpClient的名稱: http ://loopj.com/android-async-http/
您可以使用onSuccess
和onFailure
方法來處理響應。
如果您只想獲取數據而不做其他任何事情,對其進行處理或保存,則選項2更好。 同樣,您需要首先解析響應。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.