将csv打开到输入流中

时间:2011-07-15 14:43:48

标签: java android csv

我试图打开一个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中遇到连接失败; openchargemap.org

1 个答案:

答案 0 :(得分:0)

这可能与CSV“Content-Disposition”是“附件”的事实有关。您可能会发现以下内容:

http://download.oracle.com/javase/tutorial/networking/urls/readingURL.html