4

Facing Problem on getting Response in ArrayList.

I have following Respose on String value

 var res_message: String = ""
 res_message = "${result.vehicletypes} "

Getting below Value on this String

     [VehicleType(_id=1, vehicleType=Hatchback, __v=0), 
      VehicleType(_id=2, vehicleType=Maruti, __v=0), 
      VehicleType(_id=3, vehicleType=Honda, __v=0), 
      VehicleType(_id=4, vehicleType=Bike, __v=0)] 

Retrofit Result is

 vehicletypes = {ArrayList@6055}  size = 4
 0 = {Model$VehicleType@6058} "VehicleType(_id=1, 
vehicleType=Hatchback, __v=0)"
 1 = {Model$VehicleType@6059} "VehicleType(_id=2, 
 vehicleType=Maruti, __v=0)"
  2 = {Model$VehicleType@6060} "VehicleType(_id=3, 
 vehicleType=Honda, __v=0)"
  3 = {Model$VehicleType@6061} "VehicleType(_id=4, 
  vehicleType=Bike, __v=0)"

Below Code snippest sending request to API.

            disposable = apiServices.getVehicle(token)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(
                    { result ->


                        res_message = "${result.vehicletypes} "
                        Log.d("Type==", res_message)
                    },
                    { error ->
                        res_message = "${error.message}"
                        // validateToken()
                    }
            )

Model Class

  data class Vehicles(val success: Boolean, val vehicletypes: List<VehicleType>, val message: String)
  data class VehicleType(val _id: String, val vehicleType: String, val __v: String)

I want to get this value on Arralist VehicleType List on below vehicleListArray

   private var vehicleListArray: ArrayList<Model.VehicleType>? = null

How we can achieve this. Thanks in advance.

1
  • What is return type of fun getPeople() method in apiService definition? Commented Mar 9, 2018 at 7:19

2 Answers 2

1

Assuming that what you are trying to parse is a response from a service that is able to send you propper format for lists (eg Json) than Retrofit can handle parsing lists with ease.

In your apiService definition:

fun getPeople(token: Whatever): Observable<List<VehicleType>>

And if you don't have it already:

Retrofit.Builder()
   .addConverterFactory(GsonConverterFactory.create(gson))
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you @muminers I will try.
I would use Flowable instead of Observable but the answer it´s Ok! :thumbsup:
@axierjhtjz Good you've pointed it out. Acctually it isn't easy to decide in my opinion. Here is piece of doc that covers this subject: github.com/ReactiveX/RxJava/wiki/…
Yep, not easy to make a pick, that´s why I said "I would use". Good read by the way.
@axierjhtjz why not Single, are you expect more values? for simple POST it's ok to use Completable
0

I got solution I have to handle Respose as below code snippet.

private fun getVehicleType() { 
    disposable?.add(apiServices.getVehicle(token)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.io())
            .subscribe(this::handleResponse, this::handleError))
}

private fun handleResponse(vehicles: Model.Vehicles) {
    VehiclesArrayList = ArrayList(vehicles.vehicletypes)
    Log.d("type==","n--"+VehiclesArrayList )
    mAdapter = DataAdapter(VehiclesArrayList !!, this)
    v_android_list.adapter = mAdapter
}

private fun handleError(error: Throwable) {
    Log.d("type", error.localizedMessage)
    Toast.makeText(context, "Error ${error.localizedMessage}", Toast.LENGTH_SHORT).show()
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.