我是一般的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可以使用您选择的指定类型的任意数量的参数。你当然可以扩展它并传递任何你想要的东西。 –