2015-09-19 19 views
0

我使用Volley並需要發送到服務器列表的對象UserList<Integer>排球:在JSONObject中使用列表<T>發出請求

User類:

public class User { 

     public int id; 
     public String name; 

     public MyClass(int id, String name){ 
      this.id = id; 
      this.name= name; 
     } 
} 

我使用的代碼

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); 

問題是JSONObject的無法解析User

{ 
    "users": [ 
     null, 
     null, 
     null 
    ], 
    "categories": [ 
     5, 
     8, 
     9 
    ] 
} 

如何與JSONObject解析呢?

參數「users」應該是array而不是string

我需要JSONObject因爲Volley使用

JsonObjectRequest(int method, String url, JSONObject jsonRequest, 
      Listener<JSONObject> listener, ErrorListener errorListener) 

enter image description here

全碼:

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; 
     } 
    } 
+0

這JSON框架,你用? – Henry

+0

在項目中我使用Jackson,Volley使用org.json – Alexandr

回答

1

我想你應該User用戶更換MyClass類這樣

public class User { 

    public int id; 
    public String name; 

    public User(int id, String name){ 
     this.id = id; 
     this.name= name; 
    } 
} 

我已經測試了建築的JSONObject與您的代碼中,用戶沒有空!

enter image description here

UPDATE:

嘗試用我下面的代碼爲您的新空白項目(我的項目運行正常,用戶不爲空):

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; 
    } 
} 
} 
+0

謝謝,因爲試過運行代碼。 MyClass在我的示例中只是一個錯字,使用正確的構造函數。而在屏幕截圖中,「用戶」字符串是json,而不是數組。奇怪的是我試着運行代碼並給null,請看我的更新。 – Alexandr

+0

你在哪裏定義了你的用戶類?嘗試創建一個新的'User.java'文件並定義這個類。然後,重新運行應用程序 – BNK

+0

此外,暫時刪除Jackson,只需使用默認的JSON – BNK