【问题标题】:Open csv into input stream在输入流中打开 csv
【发布时间】: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) 给它异常。

标签: java android csv


【解决方案1】:

这可能与 CSV“内容处置”是“附件”这一事实有关。您可能会发现以下内容很有用:

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

【讨论】:

  • 那么我将如何处理这种情况,有什么想法吗?
  • 尝试使用我发送的示例,看看您是否至少可以获得流。
猜你喜欢
  • 2016-03-25
  • 1970-01-01
  • 1970-01-01
  • 2019-06-26
  • 1970-01-01
  • 2013-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多