我尝试运行几个请求来执行不同的服务器并调试程序行为。首先,我创建并运行了几个异步任务,它们发出了几个请求
public class MyTask extends AsyncTask<Object, Void, Void> {
int runnnigDownloadThreadsCount = 0;
@Override
protected Void doInBackground(Object... params) {
runnnigDownloadThreadsCount++;
try {
// TODO здесь необходимо исправить serverName на оригинальный урл с указанием параметра сервера
List<TravelTicket> ticketsList = TravelTicket.downloadTickets((TravelTicket.RequestServerParams) params[0], (String) params[1]);
if (ticketsList != null)
synchronized (TravelBookingApplication.travelTicketsList) {
TravelBookingApplication.travelTicketsList.addAll(ticketsList);
}
return null;
} catch (Exception e) {
throw new Error(e);
}
}
@Override
protected void onPostExecute(Void result) {
runnnigDownloadThreadsCount--;
if (runnnigDownloadThreadsCount == 0) {
Intent i = new Intent(getActivity().getApplicationContext(), HotelsAndTicketsResponseListActivity.class);
i.putExtra("requestType", "tickets");
getActivity().startActivity(i);
if (dialog != null)
dialog.dismiss();
}
}
}
String[] urls = new String[]{
"http://dl.dropbox.com/u/34053723/provider1.json"
,
"http://dl.dropbox.com/u/34053723/provider2.json",
"http://dl.dropbox.com/u/34053723/provider3.json",
"http://dl.dropbox.com/u/34053723/provider4.json"
};
for (final String serverName : urls) {
// TODO ЗАМЕНИТЬ!!!
// for (final String serverName : params.serverNames) {
new MyTask().execute(params, serverName);
}
接下来在函数 TravelTicket.downloadTickets(...) 我使用 RestTemplate 加载数据
public static ArrayList<TravelTicket> downloadTickets(RequestServerParams requestServerParams, String serverName) throws Exception {
// Запрос к серверу с указанием конретного провайдера и идентификторов запроса
// Заголовки
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setUserAgent(TravelBookingApplication.USER_AGENT);
HttpEntity requestEntity = new HttpEntity(requestHeaders);
RestTemplate restTemplate = new RestTemplate();
每个线程都使用最后一行接近HttpEntity requestEntity = new HttpEntity(requestHeaders);
但在第一次运行后调试器冻结。看起来它在断点处停止,但没有断点。