• Post Reply Bookmark Topic Watch Topic
  • New Topic

Volley and arraylist of objects as response  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, so I have been searching everywhere on how to get my app to work. I have an app deployed on Tomcat. It needs to send an ArrayList of data as json preferably to an Android app. The app uses Volley API to get the response and I need to parse it back to the ArrayList. I have been asking everywhere I could, but haven't received any solid replies. Can someone give me links or pointers on how to achieve the aforementioned task?
 
Sheriff
Posts: 23451
46
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Yasushi, welcome to the Ranch!

Just to clarify: your Android app wants to use Volley to send a request to your web app. And it expects to receive a list of data encoded in JSON from the web app, and you want to decode that JSON data into a List. Right?

I did some searching the web and I found examples of Android code which use a "JsonRequest"; it sends a request and receives JSON as a response. I assume you found those examples too? So as I see it your question boils down to converting some JSON text into a Java List object. Which is independent of the fact that Volley sent the request and received the response. Am I right, or is there more to your question that that?
 
Yasushi Utsumi
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply Paul. Yes, that's exactly my doubt. I did find some examples and I'm trying them out right now. Like you said, I'm using JsonRequest.
 
Paul Clapham
Sheriff
Posts: 23451
46
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, if you want to convert JSON to ArrayList (does it really have to be an ArrayList or will any List do?) then that doesn't sound like it's really Android-specific. I'll flag this thread to be in a general Java forum too, so it should be seen by more people.
 
Ranch Foreman
Posts: 340
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Android app uses Volley to make a request and receives a response.
Here is a link to the Android's Volley web page and example:
https://developer.android.com/training/volley/index.html
https://developer.android.com/training/volley/simple.html#java

Android API comes with built-in JSON parsing API.

In your app, I am assuming the web-app on the Tomcat server accepts the Android app's request and returns a JSON response - the resonse is received by the Android app.

To parse the response output (which is a JSON string) and build the data into an ArrayList, the following is the way to code. In this case I am assuming the response JSON string has Earthquake data from a geological website.

Here is some JSON string data to be converted to Java objects and then store them in a List:

"features": [
    {
      "type": "Feature",
      "properties": {
        "mag": 4.5,
        "place": "150km NW of Nuku`alofa, Tonga",
        "time": 1514837288820,
        "updated": 1516127564040,
        "tz": -720,
        "url": "https:\/\/earthquake.usgs.gov\/earthquakes\/eventpage\/us1000byui",
...
        "magType": "mb",
        "type": "earthquake",
        "title": "M 4.5 - 150km NW of Nuku`alofa, Tonga"


In the following code snippet both the classes JSONObject and JSONArray are imported as org.json.*;



The List data earthquakes is then used in the Android app to, well, list the earthquake details in ListView. This is how I think the app works.
 
Yasushi Utsumi
Greenhorn
Posts: 4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys. I got it to work.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!