我是一般的android開發和軟件編程的新手,並且相信我的應用程序中存在線程問題。該應用程序所做的是根據對api的兩個查詢搜索兩組結果,並將每組結果存儲在其自己的列表中。生成的新列表僅包含兩個列表中的元素。該應用在桌面上的虛擬設備中運行,但掛在我的Galaxy Nexus上。我正在使用arraylist,但我想知道是否hashset在完成這種類型的操作時會更快。以下是我的主要活動。 getfirst和secondID在asynctask以及getfirst和secondtitle中完成,以防止networkonmainthread異常。這是線程這個應用程序的最佳方式嗎?謝謝你的幫助。我如何讓我的應用程序不掛在設備上
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.totlayout);
//set the UI elements
searchOne = (EditText) findViewById(R.id.searchOne);
searchTwo = (EditText) findViewById(R.id.searchTwo);
findMovies = (Button) findViewById(R.id.findMovies);
searchOne.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
//make person search url1
final StringBuilder personSearchURLOne = new StringBuilder(getName.getName1(searchOne));
searchURLOne = personSearchURLOne.toString();
return false;
}
});
searchTwo.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
//make person search url2
final StringBuilder personSearchURLTwo = new StringBuilder(getName.getName2(searchTwo));
searchURLTwo = personSearchURLTwo.toString();
return false;
}
});
}
public void getData(String searchURLOne, String searchURLTwo){
try{
//get ID 1
idOne = new getFirstID().execute(searchURLOne).get();
Log.d("JSONArray idOne", idOne.toString());
//get ID 2
idTwo = new getSecondID().execute(searchURLTwo).get();
Log.d("JSONArray idTwo", idTwo.toString());
//make credit search url1
final StringBuilder creditURLOne = new StringBuilder(buildCreditURL.getCreditURLOne(idOne));
final String creditOne = creditURLOne.toString();
Log.d("creditOne contains", creditOne);
//make credit search url2
final StringBuilder creditURLTwo = new StringBuilder(buildCreditURL.getCreditURLTwo(idTwo));
final String creditTwo = creditURLTwo.toString();
//get array of tiles for credit url 1
titleOne = new getFirstTitle().execute(creditOne).get();
Log.d("titleOne Contains", titleOne.toString());
//get array of titles for credit url 2
titleTwo = new getSecondTitle().execute(creditTwo).get();
//parse out common films into new array
myCommonFilms = new ArrayList<String>(commonFilms.getCommonFilms(titleOne, titleTwo));
}
catch(InterruptedException e){
e.printStackTrace();
}catch(ExecutionException e){
e.printStackTrace();
}
}
public void displayResults(View view) throws InterruptedException, ExecutionException{
//do something in response to button
getData(searchURLOne, searchURLTwo);
Intent intent = new Intent(this, DisplayResultsActivity.class).putStringArrayListExtra("myCommonFilmsList", myCommonFilms);
startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.totlayout, menu);
return true;
}
}
在getData中,存在對getFirstID和getSecondID以及getSecondTitle和getSecondTitle的調用。這些clases實現asynctask並查詢api。我想嘗試必須getData擴展asynctask,但無法想出一個方法來傳遞兩個參數,因爲asynctask在String中進行...在我的研究中並通過玩弄它我的印象是asynctask只會在stringbuilder可變長度。如果有一種方法,我可以傳遞兩個參數即。 searchURLOne和searchURLTwo或者一個字符串數組,即[[searchURLOne] [searchURLTwo]],這是值得嘗試的。 – user881667
讓我們先弄清楚如何讓它工作,然後弄清楚如何優化它。當你說它掛在設備上時,這是否意味着它只需要很長時間,或者是否意味着它會無限期地掛起?如果它永遠掛起,請檢查手機和桌面之間的網絡差異。 – Cliff
asyncTask可以使用您選擇的指定類型的任意數量的參數。你當然可以擴展它並傳遞任何你想要的東西。 –