无法从财务雅虎下载文件从互联网到SD卡

时间:2013-03-29 12:41:07

标签: android web-services yahoo-finance

    public void downloadFileViaHTTP()
    {
        Log.d("downloadFileViaHttp", "downloadFileViaHTTP...");
        try 
            {
                  String urlFile = "http://finance.yahoo.com/d/quotes.csv?s=ACC.NS+AMBUJACEM.NS+ASIANPAIN.NS+AXISBANK.NS+BAJAJ-AUT.NS+BANKBAROD.NS+" +               "BHARTIART.NS+BHEL.NS+BPCL.NS+CAIRN.NS+CIPLA.NS+COALINDIA.NS+DLF.NS+DRREDDY.NS+GAIL.NS+GRASIM.NS+HCLTECH.NS+HDFC.NS+HDFCBANK.NS+ HEROHONDA.NS+HINDALCO.NS+HINDUNILV.NS + +"HEROHONDA.NS+HINDALCO.NS+HINDUNILV.NS+ICICIBANK.NS+IDFC.NS+ITC.NS+INFY.NS+JINDALSTE.NS+JPASSOCIA.NS+KOTAKBANK.NS+LT.NS+LUPIN.NS+M%26M.NS+"+ "MARUTI.NS+NTPC.NS + ONGC.NS+PNB.NS+POWERGRID.NS+RANBAXY.NS+RELIANCE.NS+RELINFRA.NS+SBIN.NS+SESAGOA.NS+SIEMENS.NS+SPARC.NS+TATAMOTOR.NS+"+"TATAPOWER.NS+TATASTEEL.NS+TCS.NS+ULTRACEMC.NS+WIPRO.NS+ %5ENSEI&f=snl1hgp";

            URL url = new URL(urlFile);
            Log.d("url", url.toString());
            BufferedReader reader=null;
            Log.d("1","1");
            // create the new connection
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            Log.d("downloadFileViaHttp:HttpURLconnection", " " + urlConnection);
            urlConnection.setRequestMethod("GET");
            urlConnection.setDoOutput(true);

            //and connect!
            Log.d("2","2");
            urlConnection.connect();

            // pointer to the downloaded file path
            File sdcard= Environment.getExternalStorageDirectory(); 
            File MyFilePtrDest=new File(sdcard,"quotes.csv");

            Log.d("downloadFileViaHttp","localFileName = " + MyFilePtrDest.toString());

            // this will be used in reading the data from the Internet
            InputStream inputStream = urlConnection.getInputStream();

            // this will be used to write the downloaded data into the file we
            // created
            FileOutputStream fileOutput = new FileOutputStream(MyFilePtrDest);

            byte[] buffer = new byte[1024];
            int bufferLength = 0; // used to store a temporary size of the buffer               
            // write buffer contents to file
            while ((bufferLength = inputStream.read(buffer))>0) 
            {
            Log.d("content","content "+inputStream.read(buffer, 0, bufferLength));
            // add the data in the buffer to the file in the file output
            // stream (the file on the sd card
            fileOutput.write(buffer, 0, bufferLength);
            }
                inputStream.close();
                // close the output stream when done
                // fileOutput.flush();
                // fileOutput.close();
                urlConnection.disconnect();
            }
            catch (IOException e) 
            {
                // data were not found
                dataNotFound = true;``
                e.printStackTrace();
            }
        }

每当指针到达上面代码中的语句urlconnection.connect()时,我都会尝试从yahoo finance下载csv文件,它会给我java.net.unknownHostException。如何在上面的代码中解决问题?

下面是log cat

  

03-29 19:10:09.770:W / System.err(32292):   java.net.UnknownHostException:finance.yahoo.com 03-29 19:10:09.770:   W / System.err(32292):at   java.net.InetAddress.lookupHostByName(InetAddress.java:506)03-29   19:10:09.770:W / System.err(32292):at   java.net.InetAddress.getAllByNameImpl(InetAddress.java:294)03-29   19:10:09.770:W / System.err(32292):at   java.net.InetAddress.getAllByName(InetAddress.java:256)03-29   19:10:09.770:W / System.err(32292):at   org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection。(HttpConnection.java:69)   03-29 19:10:09.770:W / System.err(32292):at   org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection。(HttpConnection.java:48)   03-29 19:10:09.770:W / System.err(32292):at   org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection $ Address.connect(HttpConnection.java:322)   03-29 19:10:09.770:W / System.err(32292):at   org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)   03-29 19:10:09.770:W / System.err(32292):at   org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:285)   03-29 19:10:09.770:W / System.err(32292):at   org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:267)   03-29 19:10:09.770:W / System.err(32292):at   org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:205)   03-29 19:10:09.770:W / System.err(32292):at   com.indusa.stockapp.stockListing.StockListing.downloadFileViaHTTP(StockListing.java:252)   03-29 19:10:09.770:W / System.err(32292):at   com.indusa.stockapp.stockListing.StockListing.onCreate(StockListing.java:99)   03-29 19:10:09.770:W / System.err(32292):at   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)   03-29 19:10:09.770:W / System.err(32292):at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)   03-29 19:10:09.770:W / System.err(32292):at   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)   03-29 19:10:09.770:W / System.err(32292):at   android.app.ActivityThread.access $ 1500(ActivityThread.java:121)03-29   19:10:09.770:W / System.err(32292):at   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:943)   03-29 19:10:09.770:W / System.err(32292):at   android.os.Handler.dispatchMessage(Handler.java:99)03-29   19:10:09.780:W / System.err(32292):at   android.os.Looper.loop(Looper.java:130)03-29 19:10:09.780:   W / System.err(32292):at   android.app.ActivityThread.main(ActivityThread.java:3770)03-29   19:10:09.780:W / System.err(32292):at   java.lang.reflect.Method.invokeNative(Native Method)03-29   19:10:09.780:W / System.err(32292):at   java.lang.reflect.Method.invoke(Method.java:507)03-29 19:10:09.780:   W / System.err(32292):at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:912)   03-29 19:10:09.780:W / System.err(32292):at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)03-29   19:10:09.780:W / System.err(32292):at   dalvik.system.NativeStart.main(原生方法)

0 个答案:

没有答案