从服务器接收Object而不是String?

时间:2016-02-13 05:09:31

标签: android spinner android-volley

尝试连接服务与volley并将该值绑定到spinner但是当我尝试设置适配器显示对象值而不是字符串时不知道哪里出错了这是我的代码:

  final   Spinner spinner=(MaterialSpinner)findViewById(R.id.spinner);
        String Url = "http://xxx.xx.xx.xxx/xxx/CRM/AcoountCreatePageLoad.svc/Account/AccountPageLoad";
        JsonObjectRequest jsonObjRequest = new JsonObjectRequest(Request.Method.GET, Url, new JSONObject(),
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        String server_response = response.toString();
                        try {
                            JSONObject json_object = new JSONObject(server_response);
                            JSONArray json_array = new JSONArray(json_object.getString("AccountPageLoadResult"));
                            for (int i = 0; i < json_array.length(); i++) {
                                Model_Spinner_yog model_spinner_yog = new Model_Spinner_yog();
                                JSONObject json_arrayJSONObject = json_array.getJSONObject(i);
                                model_spinner_yog.setName(json_arrayJSONObject.getString("Name"));
                                yog.add(model_spinner_yog);



                            }

                            ArrayAdapter<Model_Spinner_yog> yogs=new ArrayAdapter<Model_Spinner_yog>(Create_Account.this, android.R.layout.simple_spinner_dropdown_item,yog);
                            spinner.setAdapter(yogs);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

我的模特课:

public class Model_Task_List implements Serializable {


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Model_Task_List(int id, String subject, String userid, String taskStatus, int taskID, String userName) {
        this.id = id;
        Subject = subject;
        TaskID = taskID;
        TaskStatus = taskStatus;
        this.userid = userid;
        UserName = userName;
    }

    public int id;
    public String UserName;
    public String Subject;
    public String TaskStatus;

    public int getTaskID() {
        return TaskID;
    }

    public void setTaskID(int taskID) {
        TaskID = taskID;
    }

    public int TaskID;
    public static String KEY_table="task";
    public  static String KEY_id="id";
    public  static String KEY_username="name";
    public  static String KEY_subject="subject";
    public  static String KEY_task="status";
    public static String KEY_taskid="taskid";
    public static String KEY_owner="owner";

    public String getUserid() {
        return userid;
    }

    public void setUserid(String userid) {
        this.userid = userid;
    }

    public String userid;

    public String getSubject() {
        return Subject;
    }

    public void setSubject(String subject) {
        Subject = subject;
    }

    public String getTaskStatus() {
        return TaskStatus;
    }

    public void setTaskStatus(String taskStatus) {
        TaskStatus = taskStatus;
    }

    public String getUserName() {
        return UserName;
    }

    public void setUserName(String userName) {
        UserName = userName;
    }

    public Model_Task_List(String subject, String taskStatus, String userName) {
        Subject = subject;
        TaskStatus = taskStatus;
        UserName = userName;
    }
    public Model_Task_List(){

    }
}

响应:

{"AccountPageLoadResult":"[{\"PairLinkID\":126,\"Name\":\"Andhra Pradesh\",\"RID\":0,\"PID\":0,\"Status\":true,\"Description\":\"Andhra\"},{\"PairLinkID\":127,\"Name\":\"Delhi\",\"RID\":0,\"PID\":0,\"Status\":true,\"Description\":\"Delhi\"},{\"PairLinkID\":128,\"Name\":\"Gujarat\",\"RID\":0,\"PID\":0,\"Status\":true,\"Description\":\"Gujarat\"},{\"PairLinkID\":129,\"Name\":\"Haryana\",\"RID\":0,\"PID\":0,\"Status\":true,\"Description\":\"Haryana\"},{\"PairLinkID\":130,\"Name\":\"KARNATAKA\",\"RID\":0,\"PID\":0,\"Status\":true,\"Description\":\"Karnataka\"},{\"PairLinkID\":131,\"Name\":\"Kerala\",\"RID\":0,\"PID\":0,\"Status\":true,\"Description\":\"Kerala\"},{\"PairLinkID\":132,\"Name\":\"Maharashtra\",\"RID\":0,\"PID\":0,\"Status\":true,\"Description\":\"Maharashtra\"},{\"PairLinkID\":133,\"Name\":\"Punjab\",\"RID\":0,\"PID\":0,\"Status\":true,\"Description\":\"Punjab\"},{\"PairLinkID\":134,\"Name\":\"Rajasthan\",\"RID\":0,\"PID\":0,\"Status\":true,\"Description\":\"Rajasthan\"},{\"PairLinkID\":135,\"Name\":\"TAMILNADU\",\"RID\":0,\"PID\":0,\"Status\":true,\"Description\":\"Tamil Nadu\"},{\"PairLinkID\":136,\"Name\":\"Telangana\",\"RID\":0,\"PID\":0,\"Status\":true,\"Description\":\"Telangana\"},{\"PairLinkID\":137,\"Name\":\"Uttar Pradesh\",\"RID\":0,\"PID\":0,\"Status\":true,\"Description\":\"Uttar Pradesh\"},{\"PairLinkID\":138,\"Name\":\"West Bengal\",\"RID\":0,\"PID\":0,\"Status\":true,\"Description\":\"West Bengal\"},{\"PairLinkID\":214,\"Name\":\"\",\"RID\":0,\"PID\":0,\"Status\":true,\"Description\":\"\"}]"}

1 个答案:

答案 0 :(得分:0)

根据您的响应,您不应将其转换为字符串,然后再转换为JSONObject。使用下面的代码作为参考,然后重新制作代码。

 @Override
    public void onResponse(JSONObject response) {
       if(response != null){
        JSONArray array = response.optJSONArray("AccountPageLoadResult");
        for(int i = 0; i<array.length();i++){
           JSONObject object = array.optJSONObject(i);
           // get Other data from object using optString("name") or getString("name")
        }
      }
    }