不能提取使用邮政凌空的嵌套的 json 对象的数据android

发布时间: 2017/3/4 4:00:36
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.
{
"success": true,
"message": {
"user": {
  "firstName": "aaa",
  "lastName": "aaa",
  "email": "xxx@gmail.com",
  "role": 1,
  "profileUrl": ""
},
"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjp7ImZpcnN0TmFtZSI6ImFhYSIsImxhc3ROYW1lIjoiYWFhIiwiZW1haWwiOiJ4eHhAZ21haWwuY29tIiwicm9sZSI6MSwicHJvZmlsZVVybCI6IiJ9LCJpYXQiOjE0ODY1MjMyOTAsImV4cCI6MTQ4NjUzNDczMH0.kFHji61mqqupMSNBL_ZLnaLbK1xmFxbD6MbWa_SPlyo",
"enum": {
  "_id": "5889d3f10893a7a42243998e",
  "updatedAt": "2017-01-26T10:48:17.233Z",
  "createdAt": "2017-01-26T10:48:17.233Z",
  "__v": 0,
  "mailCategories": [
    {
      "color": [
        "Work",
        "Document",
        "Social",
        "Advertising",
        "Client"
      ],
      "value": [
        "#1ab394",
        "#EF5352",
        "#1c84c6",
        "#23c6c8",
        "#F8AC59"
      ]
    }
  ],
  "folders": [
    "Send",
    "Draft"
  ],
  "uploadsImageTypes": [
    "jpg",
    "jpeg",
    "png",
    "gif"
  ],
  "uploadsFileTypes": [
    "jpg",
    "jpeg",
    "docx",
    "pdf",
    "txt",
    "ppt",
    "png",
    "gif"
  ],
  "workOrderStatus": {
    "color": [
      "#A09580",
      "#f8ac59",
      "#41b0f6",
      "#0d71b0",
      "#1ab394",
      "#067e47",
      "#2819bc",
      "#0d08f3",
      "#ef0c34",
      "#ef9aa9",
      "#f2830b"
    ],
    "value": [
      "Draft",
      "Requested",
      "Requested Awaiting Approval",
      "Assigned",
      "Work In Progress",
      "Ready",
      "Done",
      "Closed,Completed",
      "Closed,Incompleted",
      "On Hold",
      "Open"
    ]
  },
  "priorities": {
    "color": [
      "#0B7409",
      "#CE9B9B",
      "#7CC396",
      "#CEBE99"
    ],
    "value": [
      "Hard Down",
      "High",
      "Medium",
      "Low"
    ]
  },
  "categories": [
    "Equipment",
    "Cranes",
    "Delivery Vehicles",
    "Rotating Spares"
  ],
  "projects": [
    "Vehicle New",
    "System #7865",
    "Book Shop",
    "Site"
  ],
  "maintenanceTypes": {
    "color": "#FFFFFF",
    "bg_color": [
      "#1c84c6",
      "#262626",
      "#A09580",
      "#ed5565"
    ],
    "value": [
      "Electrical",
      "Damage",
      "Safty",
      "Broken"
    ]
  },
  "assets": [
    "Conveyor Belt 1",
    "Cranes",
    "Delivery Vehicles",
    "Rotating Spares"
  ],
  "assignToUser": [
    1,
    2,
    4,
    5,
    6
  ],
  "roles": [
    "Admin",
    "Manager",
    "Technician",
    "Customer",
    "Supplier",
    "Engineer",
    "Guest"
  ]
}
}
}

这是我的LoginActivity.java

public class LoginActivity extends AppCompatActivity {
EditText un, pw;
ImageButton sbtn;

SystemPreference systemPreference;

String URL_POST = "http://10.183.183.45:6663/api/v1/" + "auth/authenticate";

private GoogleApiClient client;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    un = (EditText) findViewById(R.id.login_username);
    pw = (EditText) findViewById(R.id.login_password);
    sbtn = (ImageButton) findViewById(R.id.signinbtn);

    sbtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            VolleyPost();
        }
    });
    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}

private void VolleyPost() {

    systemPreference = SystemPreference.getInstance(this.getApplicationContext());

    StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_POST, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.d("response", String.valueOf(response));

            JSONObject jsonObject = null;
            try {
                JSONObject msgjson = new JSONObject(response);
                String success = msgjson.getString("success");
                String message = msgjson.getString("message");

                JSONObject tokenObj = new JSONObject("token");
                String token = tokenObj.getString("token");

                if(msgjson.isNull(token)) {
                    String msg = msgjson.get("user").toString();
                    Log.d("msg",  msg);
                    Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                }else {
                    String msg = msgjson.get("message").toString();

                    Log.d("msg",  msg);
                    Toast.makeText(LoginActivity.this, "Authorized User", Toast.LENGTH_SHORT).show();
                    Intent i = new Intent(LoginActivity.this, MenuActivity.class);
                    startActivity(i);

                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d("aaa", "aaa");
            Toast.makeText(LoginActivity.this, "Wrong password or username", Toast.LENGTH_SHORT).show();
        }
    }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            String email = un.getText().toString();
            String password = pw.getText().toString();
            params.put("email", email);
            params.put("password", password);

            return params;
        }
    };

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

}

如何提取字符串的标记?

解决方法 1:

JSONObject msgjson = new JSONObject(response);
                String success = msgjson.getString("success");
                String message = msgjson.getString("message");

                JSONObject tokenObj = new JSONObject(message); //change to this
                String token = tokenObj.getString("token");

您正在创建 json 对象一无所有!!!

赞助商