Im trying to open up a csv file from
http://openchargemap.org/api/service.ashx?output=csv&countrycode=US&maxresults=2000
Im trying to add this data into a SQLite3 Database. Get an instream from this csv file and then use StringToken to separate the values and insert into a 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;
}
The code is getting caught at IO Exception ex Connection Failed; openchargemap.org
It may have to do with the fact that the CSV “Content-Disposition” is “attachment”. You might find the following useful:
http://download.oracle.com/javase/tutorial/networking/urls/readingURL.html