【发布时间】:2011-07-15 14:43:48
【问题描述】:
我试图打开一个 csv 文件 http://openchargemap.org/api/service.ashx?output=csv&countrycode=US&maxresults=2000 我试图将此数据添加到 SQLite3 数据库中。从此 csv 文件中获取一个 instream,然后使用 StringToken 分隔值并插入到 db 中。
public InputStream connect2(String url){
// Create the httpclient
HttpClient httpclient = new DefaultHttpClient();
// Prepare a request object
HttpGet httpget = new HttpGet(url);
// Execute the request
HttpResponse response;
// return string
String returnString = null;
InputStream instream = null;
try {
// Open the webpage.
response = httpclient.execute(httpget);
if(response.getStatusLine().getStatusCode() == 200){
// Connection was established. Get the content.
//ByteArrayOutputStream i = (ByteArrayOutputStream) response.getEntity();
HttpEntity entity = response.getEntity();
// If the response does not enclose an entity, there is no need
// to worry about connection release
if (entity != null) {
instream = entity.getContent();
// Close the stream.
instream.close();
}
}
else {
// code here for a response other than 200. A response 200 means the webpage was ok
// Other codes include 404 - not found, 301 - redirect etc...
// Display the response line.
returnString = "Unable to load page - " + response.getStatusLine();
}
}
catch (IOException ex) {
// thrown by line 80 - getContent();
// Connection was not established
returnString = "Connection failed; " + ex.getMessage();
}
return instream;
}
代码在 IO Exception ex Connection Failed 处被捕获; openchargemap.org
【问题讨论】:
-
你意识到你正在返回封闭的流吗?并发布 rhe 堆栈跟踪。
-
是的,它永远不会到达那一行 response = httpclient.execute(httpget) 给它异常。