android无法连接到网站

时间:2011-11-09 20:47:01

标签: android connection

您好我正在尝试连接到网站,下载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

0 个答案:

没有答案