Reputation: 1901
I use Volley
and need send to server list of objects User
and List<Integer>
.
User
class:
public class User {
public int id;
public String name;
public MyClass(int id, String name){
this.id = id;
this.name= name;
}
}
I use code
HashMap<String, Object> params = new HashMap<>();
List<Integer> categories = new ArrayList<>(Arrays.asList(5, 8, 9));
List<User> users = new ArrayList<>(Arrays.asList(
new User(1, "User1"),
new User(2, "User2"),
new User(3, "User3")
));
params.put("categories", categories);
params.put("users", users);
JSONObject jsonBody = new JSONObject(params);
The problem is JSONObject can't parse User
{
"users": [
null,
null,
null
],
"categories": [
5,
8,
9
]
}
How to parse it with JSONObject
?
Parameter "users
" should be array
not string
.
I need JSONObject
because Volley
use it
JsonObjectRequest(int method, String url, JSONObject jsonRequest,
Listener<JSONObject> listener, ErrorListener errorListener)
Full code:
package com.example.admin.testjson;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Map<String, Object> params = new HashMap<>();
List<Integer> categories = new ArrayList<>(Arrays.asList(5, 8, 9));
List<User> users = new ArrayList<>(Arrays.asList(
new User(1, "User1"),
new User(2, "User2"),
new User(3, "User3")
));
params.put("categories", categories);
params.put("users", users);
JSONObject jsonBody = new JSONObject(params);
}
}
package com.example.admin.testjson;
public class User {
int id;
String name;
public User(int id, String name){
this.id = id;
this.name = name;
}
}
Upvotes: 0
Views: 835
Reputation: 24114
I think you should replace MyClass
by User
in User class like this
public class User {
public int id;
public String name;
public User(int id, String name){
this.id = id;
this.name= name;
}
}
I have tested building JSONObject with your code, the users not null!
UPDATE:
Try use my following code for your new blank project (my project runs normally, users not null):
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
HashMap<String, Object> params = new HashMap<>();
List<Integer> categories = new ArrayList<>(Arrays.asList(5, 8, 9));
List<User> users = new ArrayList<>(Arrays.asList(
new User(1, "User1"),
new User(2, "User2"),
new User(3, "User3")
));
params.put("categories", categories);
params.put("users", users);
JSONObject jsonBody = new JSONObject(params);
Log.i("BNK", jsonBody.toString());
}
public class User {
public int id;
public String name;
public User(int id, String name){
this.id = id;
this.name= name;
}
}
}
Upvotes: 1