使用volley库搜索特定的json字符串

时间:2017-04-14 00:36:45

标签: java android mysql json android-volley

我正在尝试检索用户参加过的事件和日期(存储在MySQL中并使用PHP编码为JSON)。当用户输入他们的ID并单击按钮时,他们的事件和日期将显示在另一个活动的ListView中。但是,当这个用户已经完成了几个事件时,我只能解析一个用户的1个事件和1个日期。这是JSON的样子:[{“code”:“login_success”,“Events”:“Interview”,“Date”:“04/12/2017”},{“code”:“login_success”,“Events” “老板”, “日期”: “2017年4月15日”}]。这是我的代码:

//Check_Events.java
login_button01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //validation for username and password. Ensure corrected input

                studentID = StudentID.getText().toString();

                if(studentID.equals(""))
                {
                    builder.setTitle("Code not found");
                    displayAlert("Enter a valid passcode");
                }
                //validate data from server
                else
                {
                    StringRequest stringRequest = new StringRequest(Request.Method.POST, login_url01,
                            new Response.Listener<String>() {
                                @Override
                                public void onResponse(String response) {
                                    try {
                                        JSONArray jsonArray = new JSONArray(response);
                                        JSONObject jsonObject = jsonArray.getJSONObject(0);
                                        //"code" is the key from json object on server
                                        String code = jsonObject.getString("code");

                                        if(code.equals("login_failed"))
                                        {
                                            builder.setTitle("Login Error");
                                            displayAlert(jsonObject.getString("message"));
                                            //builder.setMessage("Response" + response);

                                        }
                                        //login successfully
                                        else
                                        {

//这是我不知道的部分//

    Toast.makeText(Check_Events.this,"Yay!!!", Toast.LENGTH_LONG).show();

      Intent intent = new Intent(Check_Events.this, Events.class);
      Bundle bundle = new Bundle();
      bundle.putString("Events", jsonObject.getString("Events"));
      bundle.putString("Date", jsonObject.getString("Date"));
      intent.putExtras(bundle);
      startActivity(intent);
           }
        } catch (JSONException e) {
             e.printStackTrace();
        }
         }
          }, new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                Toast.makeText(Check_Events.this, "Error login",Toast.LENGTH_LONG);
                                error.printStackTrace();
                            }

// Events.java

ListView myListv = (ListView) findViewById(R.id.listv);

        Bundle bundle1 = getIntent().getExtras();
        final String events= bundle1.getString("Events");
        Bundle bundle2 = getIntent().getExtras();
        final String date= bundle2.getString("Date");
        DETAILS = new String[] {events, date};
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, DETAILS);
        myListv.setAdapter(arrayAdapter);

1 个答案:

答案 0 :(得分:0)

在JSONArray下面添加for循环并将JSONObjects保留在其中

JSONArray jsonArray = new JSONArray(response);
for(int i =0; i < jsonArray.length(); i++){
 JSONObject jsonObject = jsonArray.getJSONObject(i);
}

将数据添加到arraylist并将数据设置为listview