Привет, я пытаюсь подключиться к сайту, чтобы скачать XML.Но после вызова строки
URLConnection ucon = url.openConnection();
программа, похоже, ничего не делает.
Вот фрагмент моего кода:
Первая система называется (11-09 21:35:50.820: INFO/System.out(8043): connecting to: http://mysite/calls2.xml
).Но второй System.out.println("exit point");
не называется, и он не выдает никаких исключений.
В чем может быть проблема?(http://mysite/calls2.xml не существует, я просто написал это, но проблема не в URL, я попробовал другой URL, и он тоже не работал.)
Я обновил, вот мой полный класс загрузчика.
public class Downloader extends AsyncTask<String, Void, String>{
public interface DownloadCompleteListener {
public void onTaskComplete(String aText);
public void onError();
}
private Context context = null;
private DownloadCompleteListener listener;
public Downloader(Context context, DownloadCompleteListener aListener){
this.context = context;
listener = aListener;
}
@Override
protected String doInBackground(String... params) {
//params[0] - url
//params[1] - path
//params[2] - filename
try {
URL url = new URL(params[0]); //you can write here any link
File file = new File(params[1]+params[2]);
long startTime = System.currentTimeMillis();
/* Open a connection to that URL. */
System.out.println("connecting to: "+url.toString());
URLConnection ucon = url.openConnection();
System.out.println("exit point");
/*
* Define InputStreams to read from the URLConnection.
*/
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
/*
* Read bytes to the Buffer until there is nothing more to read(-1).
*/
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
/* Convert the Bytes read to a String. */
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
System.out.println("fos: "+fos.toString());
fos.close();
Log.d("download", "download ready in"
+ ((System.currentTimeMillis() - startTime) / 1000)
+ " sec");
} catch (Exception e) {
Log.d("download", "Error: " + e);
System.out.println("Exception: "+e);
return null;
}
System.out.println("params: "+params[1]+params[2]);
return params[1]+params[2];
}
@Override
protected void onPostExecute(String result)
{
if(result==null){
System.out.println("onerror");
listener.onError();
}
else{
System.out.println("ontaskcomplete");
listener.onTaskComplete(result);
}
}
}
В журнале logcat не так много интересного, так как я пытаюсь подключиться снова и снова, это сообщения:
11-09 22:00:31.840: INFO/System.out(8741): connecting to: http://mysite/calls2.xml
11-09 22:01:00.280: DEBUG/jdwp(8773): adbd disconnected
11-09 22:01:00.950: DEBUG/jdwp(8783): adbd disconnected
11-09 22:01:08.560: INFO/System.out(8741): connecting to: http://mysite/calls2.xml
11-09 22:01:55.280: WARN/GDataClient(305): Unable to execute HTTP request.org.apache.http.conn.ConnectTimeoutException: Connect to /74.125.232.224:443 timed out
11-09 22:01:55.640: INFO/System.out(8741): connecting to: http://mysite/calls2.xml