Android 微调器中未显示jsonobject的不同字符串

Android 微调器中未显示jsonobject的不同字符串,android,json,android-spinner,jsonobject,Android,Json,Android Spinner,Jsonobject,我有以下回应,我试图显示从每个jsonobject到我的微调器的日期,但是我的微调器是空的,有人能告诉我我犯了什么错误吗 下面是我的代码片段 [ { "friend_id": "1", "user_spoc_dob": "1993-11-11", "status": true }, { "friend_id": "1", "user_mother_dob": "1974-12-12",

我有以下回应,我试图显示从每个jsonobject到我的微调器的日期,但是我的微调器是空的,有人能告诉我我犯了什么错误吗

下面是我的代码片段

[

    {
        "friend_id": "1",
        "user_spoc_dob": "1993-11-11",
        "status": true
    },
    {
        "friend_id": "1",
        "user_mother_dob": "1974-12-12",
        "status": false
    },
    {
        "friend_id": "1",
        "user_father_dob": "1972-11-19",
        "status": false
    },
    {
        "friend_id": "1",
        "user_dob": "1994-02-20",
        "status": true
    }
]
MainActivity.java

 protected ArrayList<HashMap<String, String>> doInBackground(
                String... args) {
            ServiceHandler sh = new ServiceHandler();
            // Making a request to url and getting response
            statedata = new ArrayList<HashMap<String, String>>();
            String jsonStr = sh.makeServiceCall(STATE_URL, ServiceHandler.GET);
            Log.d("Response: ", "> " + jsonStr);
            if (jsonStr != null) {
                try {
                   jsonObj = new JSONArray(jsonStr);
                    for (int i = 0; i < jsonObj.length(); i++) {
                        JSONObject c = jsonObj.getJSONObject(i);
                        String wifebday=c.getString("user_spoc_dob");
                        String mothersbday=c.getString("user_mother_dob");
                        String fatherbday=c.getString("user_father_dob");
                        String ownbbday=c.getString("user_dob");
                        System.out.println("Bday" + wifebday + mothersbday + fatherbday + ownbbday);
                        HashMap<String, String> map = new HashMap<String, String>();
                        //map.put(USER_STATUSS, c.getString(USER_STATUSS));

                        map.put(wifebday,c.getString(wifebday));
                        map.put(mothersbday,c.getString(mothersbday));
                        map.put(fatherbday,c.getString(fatherbday));
                        map.put(ownbbday,c.getString(ownbbday));
                        statedata.add(map);
                    }
                    System.out.println("WifeBday"+statedata.size());
                   /* JSONObject test=jsonObj.getJSONObject(0);
                    String wifebday=test.getString("user_spoc_dob");
                    System.out.println("WifeBday"+wifebday);
                    JSONObject mothers=jsonObj.getJSONObject(1);
                    String mothersbday=test.getString("user_mother_dob");
                    System.out.println("MotherBday"+mothersbday);
                    JSONObject father=jsonObj.getJSONObject(2);
                    String fatherbday=test.getString("user_father_dob");
                    System.out.println("FatherBday"+fatherbday);
                    JSONObject ownb=jsonObj.getJSONObject(3);
                    String ownbbday=test.getString("user_dob");
                    System.out.println("UserBaday"+ownbbday);*/
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }
            return statedata;
        }

        protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
            super.onPostExecute(result);
            pDialog.dismiss();
            arrallstates = new String[statedata.size()];
            for (int index = 0; index < statedata.size(); index++) {
                HashMap<String, String> map = statedata.get(index);
               // arrallstates[index] = map.get(USER_STATUSS);
            }
            // pass arrConuntry array to ArrayAdapter<String> constroctor :
            adapterallstates = new ArrayAdapter<String>(
                    TestSpinner.this,
                    android.R.layout.simple_spinner_dropdown_item, arrallstates);
            System.out.println("adpttest"+adapterallstates);
            spiner.setAdapter(adapterallstates);
        }
    }
受保护的ArrayList doInBackground(
字符串…args){
ServiceHandler sh=新的ServiceHandler();
//向url发出请求并获得响应
statedata=newarraylist();
字符串jsonStr=sh.makeServiceCall(STATE\uURL,ServiceHandler.GET);
Log.d(“响应:”、“>”+jsonStr);
if(jsonStr!=null){
试一试{
jsonObj=新的JSONArray(jsonStr);
for(int i=0;i
虽然您只需要日期列表,但我建议您使用
字符串的列表,而不是
Map
s

试一试:

protected ArrayList<String> doInBackground(
            String... args) {
        ServiceHandler sh = new ServiceHandler();
        // Making a request to url and getting response
        statedata = new ArrayList<String>();
        String jsonStr = sh.makeServiceCall(STATE_URL, ServiceHandler.GET);
        Log.d("Response: ", "> " + jsonStr);
        if (jsonStr != null) {
            try {
               jsonObj = new JSONArray(jsonStr);
                for (int i = 0; i < jsonObj.length(); i++) {
                    JSONObject c = jsonObj.getJSONObject(i);
                    String wifebday= (c.has("user_spoc_dob")) ? c.getString("user_spoc_dob") : null;
                    String mothersbday= (c.has("user_mother_dob")) ? c.getString("user_mother_dob") : null;
                    String fatherbday= (c.has("user_father_dob") ? c.getString("user_father_dob") : null;
                    String ownbbday= (c.has("user_dob")) ? c.getString("user_dob") : null;
                    System.out.println("Bday" + wifebday + mothersbday + fatherbday + ownbbday);

                    // if test, to avoid adding null values
                    if(wifebday!=null) statedata.add(wifebday);
                    if(mothersbday!=null) statedata.add(mothersbday); 
                    if(fatherbday!=null) statedata.add(fatherbday); 
                    if(ownbbday!=null) statedata.add(ownbbday); 
                }
                System.out.println("WifeBday"+statedata.size());
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }
        return statedata;
    }

    protected void onPostExecute(ArrayList<String> result) {
        super.onPostExecute(result);
        pDialog.dismiss();
        arrallstates = new String[statedata.size()]; // you can also use "result" array instead
        for (int index = 0; index < statedata.size(); index++) {
           arrallstates[index] = statedata.get(index);// or result.get(index);
        }
        // pass arrConuntry array to ArrayAdapter<String> constroctor :
        adapterallstates = new ArrayAdapter<String>(
                TestSpinner.this,
                android.R.layout.simple_spinner_dropdown_item, arrallstates);
        System.out.println("adpttest"+adapterallstates);
        spiner.setAdapter(adapterallstates);
    }
}
受保护的ArrayList doInBackground(
字符串…args){
ServiceHandler sh=新的ServiceHandler();
//向url发出请求并获得响应
statedata=newarraylist();
字符串jsonStr=sh.makeServiceCall(STATE\uURL,ServiceHandler.GET);
Log.d(“响应:”、“>”+jsonStr);
if(jsonStr!=null){
试一试{
jsonObj=新的JSONArray(jsonStr);
for(int i=0;iArrayList<String> dateList =new  ArrayList<String>();
if (result != null) {
            try {
                JSONArray array = new JSONArray(result);
                for (int i = 0; i < array.length(); i++) {
                    JSONObject c = array.getJSONObject(i);
                    String date = c.getString("user_spoc_dob");
                    Log.e("", "TAG : - " + id);
                    dateList.add(date);
                }
            } catch (Exception e) {
                Log.e("", "Home Exception : " + e.toString());
            }
 adapterallstates = new ArrayAdapter<String>(
                TestSpinner.this,
                android.R.layout.simple_spinner_dropdown_item, dateList);

        spiner.setAdapter(adapterallstates);
[
  {
    "friend_id": "1",
    "user_spoc_dob": "1993-11-10",
    "status": true
  },
  {
    "friend_id": "1",
    "user_spoc_dob": "1993-11-11",
    "status": true
  },
  {
    "friend_id": "1",
    "user_spoc_dob": "1993-11-12",
    "status": true
  },
  {
    "friend_id": "1",
    "user_spoc_dob": "1993-11-13",
    "status": true
  },
  {
    "friend_id": "1",
    "user_spoc_dob": "1993-11-14",
    "status": true
  },
  {
    "friend_id": "1",
    "user_spoc_dob": "1993-11-15",
    "status": true
  },
  {
    "friend_id": "1",
    "user_spoc_dob": "1993-11-16",
    "status": true
  },
  {
    "friend_id": "1",
    "user_spoc_dob": "1993-11-17",
    "status": true
  },
  {
    "friend_id": "1",
    "user_spoc_dob": "1993-11-18",
    "status": true
  },
  {
    "friend_id": "1",
    "user_spoc_dob": "1993-11-19",
    "status": true
  }
]
import com.google.gson.annotations.SerializedName;

public class Data {

    @SerializedName("friend_id")
    private String friend_id;

    @SerializedName("user_spoc_dob")
    private String user_spoc_dob;

    @SerializedName("status")
    private boolean status;

    public String getFriend_id() {
        return friend_id;
    }

    public void setFriend_id(String friend_id) {
        this.friend_id = friend_id;
    }

    public String getUser_spoc_dob() {
        return user_spoc_dob;
    }

    public void setUser_spoc_dob(String user_spoc_dob) {
        this.user_spoc_dob = user_spoc_dob;
    }

    public boolean isStatus() {
        return status;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }

    @Override
    public String toString() {
        return ""+user_spoc_dob;
    }
}
 ArrayList<Data> datas = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            Data data = new Data();
            data.setFriend_id("1");
            data.setUser_spoc_dob("1993-11-1" + i);
            data.setStatus(true);
            datas.add(data);
        }
         // you can understand that i don't know which link you are calling to generate your `JSON`, so i decided to create my own `JSON` data, in which you will learn what `GSON` library can do for you. Due to which you can say Google has made data interchange quite easy for everyone. 

        /**
         * This code will convert your ArrayList object to a equivalent JSON
         * String, 
         */
        String result = (new Gson()).toJson(datas);
        Log.d("TAG", ""+result);

        /**
         * This code will convert your JSON String to a equivalent ArrayList
         * Object
         */
        ArrayList<Data> datas1 = (new Gson()).fromJson(result, new TypeToken<ArrayList<Data>>() {}.getType());



        two_Spinner = (Spinner) findViewById(R.id.SpinnerOneActivity_two_spinner);
        ArrayAdapter<Data> adapter = new ArrayAdapter<Data>(this, android.R.layout.simple_list_item_1, datas1);
        two_Spinner.setAdapter(adapter);