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
. 如何解决上面代码中的问题?
下面是日志猫
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):在 java。 net.InetAddress.lookupHostByName(InetAddress.java:506) 03-29 19:10:09.770: W/System.err(32292): 在 java.net.InetAddress.getAllByNameImpl(InetAddress.java:294) 03-29 19: 10:09.770: W/System.err(32292): 在 java.net.InetAddress.getAllByName(InetAddress.java:256) 03-29 19:10:09.770: W/System.err(32292): 在 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): 在 org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89 ) 03-29 19:10:09.770: W/System.err(32292): 在 org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:285) 03- 29 19:10:09.770: W/System.err(32292): 在 org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:267) 03-29 19: 10:09.770: W/System.err(32292): 在 org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:205) 03-29 19:10:09.770 : W/System.err(32292): 在 com.indusa.stockapp.stockListing.StockListing.downloadFileViaHTTP(StockListing.java:252) 03-29 19:10:09.770: W/System.err(32292):在 com.indusa.stockapp.stockListing.StockListing.onCreate(StockListing.java:99) 03-29 19:10:09.770: W/System.err(32292): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java: 1050) 03-29 19:10:09.770: W/System.err(32292): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623) 03-29 19:10:09.770: W/System.err( 32292): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675) 03-29 19:10:09.770: W/System.err(32292): 在 android.app.ActivityThread.access$1500(ActivityThread.java: 121) 03-29 19:10:09.770: W/System.err(32292): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:943) 03-29 19:10:09.770: W/System。错误(32292):在 android.os.Handler.dispatchMessage(Handler.java:99)03-29 19:10:09.780:W/System.err(32292):在 android.os.Looper。循环(Looper.java:130)03-29 19:10:09.780:W/System.err(32292):在 android.app.ActivityThread.main(ActivityThread.java:3770)03-29 19:10:09.780: W/System.err(32292): 在 java.lang.reflect.Method.invokeNative(Native Method) 03-29 19:10:09.780: W/System.err(32292): 在 java.lang.reflect.Method。调用(Method.java:507)03-29 19:10:09.780:W/System.err(32292):在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)03-29 19:10:09.780: W/System.err(32292): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670) 03-29 19:10:09.780: W/System.err(32292 ): 在 dalvik.system.NativeStart.main(Native Method)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): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912) 03-29 19:10:09.780: W/System.err(32292) : 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670) 03-29 19:10:09.780: W/System.err(32292): 在 dalvik.system.NativeStart.main(Native Method)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): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912) 03-29 19:10:09.780: W/System.err(32292) : 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670) 03-29 19:10:09.780: W/System.err(32292): 在 dalvik.system.NativeStart.main(Native Method)670)03-29 19:10:09.780:W/System.err(32292):在 dalvik.system.NativeStart.main(本机方法)670)03-29 19:10:09.780:W/System.err(32292):在 dalvik.system.NativeStart.main(本机方法)