Giter Site home page Giter Site logo

Comments (5)

doyaaaaaken avatar doyaaaaaken commented on July 19, 2024

Hi, @Phalaen. Thank you for your feedback.
Under the hood, both to read from String and InputStream are the same in terms of parsing logic.
So, can you give me a reproductive program like the below code? (The below code correctly works.)

fun main() {
    val data = """Device serial,Date ,Temperature 51 (Medium) °C
        |11869, 2021-02-09 00:14:59, 7.2""".trimMargin()
    val rows = csvReader().readAll(data)
    println(rows) // Result: correctly parsed
}

from kotlin-csv.

Phalaen avatar Phalaen commented on July 19, 2024

Dear @doyaaaaaken,
thank you, this is my code:

import android.os.Build
import android.os.Bundle
import android.util.Log
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.*
import androidx.lifecycle.lifecycleScope
import java.net.MalformedURLException
import java.net.URL
import com.github.doyaaaaaken.kotlincsv.dsl.csvReader

class MainActivity : AppCompatActivity() {

@RequiresApi(Build.VERSION_CODES.O)
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

button.setOnClickListener {
// url to download json data
val url:URL? = try {
URL(" hidden ")
}catch (e:MalformedURLException){
Log.d("Exception", e.toString())
null
}

lifecycleScope.launch(Dispatchers.IO){
url?.getStream()?.apply {

    // default dispatcher for json parsing, cpu intensive work
    withContext(Dispatchers.Default){

       // val list = parseDOMNesaTempIst(this@apply)
        val list = parse(this@apply)
        // main dispatcher for interaction with ui objects
        withContext(Dispatchers.Main){

            textView.append("\n\nReading data from website....\n")
            textView.append(list.toString())
              }
             }
            }
        }
    }
}

}

// extension function to get string data from url
fun URL.getStream(): InputStream? {
val stream = openStream()
return stream
}

data class DataClass(
val FirstColumn: String,
val SecondColumn: String,
val ThirdColumn: String )

fun parse(data:InputStream): List?{

val list = mutableListOf<DataClass>()

try {
   val rows: List<List<String>> = csvReader().readAll(data)

   for (i in rows) {

var firstColumn : String =  i[0]
var secondColumn : String =  i[1]
var thirdColumn : String =  i[2]

list.add(DataClass(FirstColumn =firstColumn, SecondColumn = secondColumn,  ThirdColumn = thirdColumn))
   }

}
catch (e: Exception) {
e.printStackTrace()
}
return list
}

In R.layout.activity_main there is only a button ("button") and a textview ("textView").
Thank you

from kotlin-csv.

doyaaaaaken avatar doyaaaaaken commented on July 19, 2024

@Phalaen
I cannot reproduce that in my environment.
Can you check what the InputStream content actually is?
In your code, you can debug it just before val rows: List<List<String>> = csvReader().readAll(data) line.

Hint: You can convert InputStream into String by using the below code.

val inputStream: InputStream = TODO()
val csvContent: String = inputStream.bufferedReader().use(BufferedReader::readText)

from kotlin-csv.

Phalaen avatar Phalaen commented on July 19, 2024

Dear @doyaaaaaken ,
I tried converting inputstream into string and I think the problem is aready here, as the string is only partial:

Device serial,Date ,Rainfall 10 (Accumulated) mm
11869,2021-02-20,0
11869,2021-02-21,0
11869,2021-02-22,0
11869,2021-02-23,0
11869,2021-02-24,0.2
11869,2021-02-25,0
11869,2021-02-26,0
11869,2021-02-27,0

Is it normal? This never happened before to me

from kotlin-csv.

Phalaen avatar Phalaen commented on July 19, 2024

Solved!
Using the string text (val csvContent: String = inputStream.bufferedReader().use(BufferedReader::readText) ) parsing is correctly performed.
Thank you @doyaaaaaken !

from kotlin-csv.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.