My app is used to save sensor data and upload to server.
First I have this data.
data class SensorRecord (val upDown:String,
val config:Int,
val date: Date,
val ms: Int,
val dur: Int,
public var v:MutableList<SensorValue>
)
data class SensorValue (var accelerometer:Accelerometer? = null,
var gyroscope:Gyroscope? = null,
var gravity:Gravity? = null,
var magnetic:Magnetic? = null,
var linearAcceleration:LinearAcceleration? = null,
var rotationVector:RotationVector? = null,
var gameRotationVector:GameRotationVector? = null,
var pressure:Pressure? = null,
var orientation:Orientation? = null
){
data class Accelerometer(var x:Float,
var y:Float,
var z:Float
)
data class Gyroscope(var x:Float,
var y:Float,
var z:Float
)
data class Gravity(var x:Float,
var y:Float,
var z:Float
)
data class Magnetic(var x:Float,
var y:Float,
var z:Float
)
data class LinearAcceleration(var x:Float,
var y:Float,
var z:Float
)
data class RotationVector(var x:Float,
var y:Float,
var z:Float,
var scalar:Float
)
data class GameRotationVector(var x:Float,
var y:Float,
var z:Float
)
data class Pressure(var hpa:Float)
data class Orientation(var azimuth:Float,
var pitch:Float,
var roll:Float)
}
And then add many values to list, so the data is very big.
var record:SensorRecord? = null
record = SensorRecord(upDown, config, Date(), ms.toInt(), duration.toInt(), arrayListOf<SensorValue>())
record!!.v.add(sensorValue)
record!!.v.add(sensorValue)
record!!.v.add(sensorValue)
record!!.v.add(sensorValue)
.
.
.
.
add many data
Finally upload to server
fun upload(){
val gson = GsonBuilder()
.setDateFormat("yyyy-MM-dd HH-mm-ss")
.create();
var jsonString = gson.toJson(record)
val requestQueue = Volley.newRequestQueue(applicationContext)
val stringRequest = object : StringRequest(Request.Method.POST, "http://myupload.com",
object : Response.Listener<String> {
override fun onResponse(response: String) {
}
},
object : Response.ErrorListener {
override fun onErrorResponse(error: VolleyError) {
}
})
{
override fun getParams(): Map<String, String> {
val params = HashMap<String, String>()
params["record"] = jsonString
return params
}
}
requestQueue.add(stringRequest)
}
If data is small, there is no problem, but if the data is big, the app crashes.
Throwing OutOfMemoryError "Failed to allocate a 150994952 byte allocation with 25165824 free bytes and 86MB until OOM, max allowed footprint 135855256, growth limit 201326592"
I found some answer, they suggest to use streaming
java.lang.OutOfMemoryError: Java heap space when try to convert Java Object to Json String
But I don't understand how to use streaming to upload my object
Retrofit
hasMultiPartBody
upload, e.g with POST. Example withFile
bytes:And then Service would accept this MultiPartBody
The method in interface would look like