2017-08-21 81 views
0

我在Android Studio中遇到了如何使用Android Volley字符串請求來執行JSON數組的問題。如何使用Android volley獲取並傳遞JSON數組?

我的目標是每當用戶輸入正確的登錄名和密碼。它會帶來該特定用戶的其他價值,這在我的情況下是真實姓名部門

我有這些JSON陣列輸出

{ 
    "login":"ID001", 
    "realname":"Tom", 
    "dept":"ICTD" 
} 

生產本PHP代碼如下

的login.php

<?php 
    $conn = mysqli_connect("","","",""); 

    if(
     isset($_POST['login']) && 
     isset($_POST['password']) 
    ){ 
     $login = $_POST['login']; 
     $password = $_POST['password']; 
     $sql = "SELECT * FROM users WHERE login = '$login' AND pw = '$password' "; 
     $result = mysqli_query($conn, $sql); 
     if($result && mysqli_num_rows($result) > 0){ 
      while($row = mysqli_fetch_array($result)){ 
       $logindb  = $row['login']; 
       $realnamedb = $row['realname']; 
       $deptdb  = $row['dept']; 
       echo "success_login"; 
       $response = array('login' => $logindb, 'real_name' => $real_namedb, 'dept' => $deptdb); 
       echo json_encode($response); 
      } 
     mysqli_free_result($result); 
     } else { 
      echo "login_failed"; 
     } 
    } 
?> 
<!DOCTYPE html> 
<html> 
<head> 
    <title>Login</title> 
</head> 
<body> 
    <form action="login.php" method="post"> 
     <table> 
      <tr> 
       <td>Login :</td> 
       <td><input type="text" name="login"></td> 
      </tr> 
      <tr> 
       <td>Password :</td> 
       <td><input type="text" name="password"></td> 
      </tr> 
      <tr> 
       <td><input type="submit" name="Submit" value="Login"></td> 
      </tr> 
     </table> 
    </form> 
</body> 
</html> 

在Android Studio中,我目前的問題是JSON數組無法在內部顯示嘗試{}

LoginActivity.java

public class LoginActivity extends AppCompatActivity{ 

    EditText etLogin, etPassword; 
    Button bLogin; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_login); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 


     etLogin   = (EditText)findViewById(R.id.etLogin); 
     etPassword  = (EditText)findViewById(R.id.etPassword); 
     bLogin   = (Button)findViewById(R.id.bLogin); 
     bLogin.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       String url = "http://localhost/login.php"; 
       StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() { 
        @Override 
        public void onResponse(String response) { 
         if(response.equals("success_login")){ 
          try { 
           JSONObject jsonObject = new JSONObject(response); 
           JSONArray booking  = jsonObject.getJSONArray("login"); 

           if(booking.length() > 0){ 
            for (int countItem = 0; countItem<booking.length(); countItem++){ 

             JSONObject bookingObject = booking.getJSONObject(countItem); 
             final String login     = bookingObject.isNull("login"); 
             final String realname    = bookingObject.isNull("realname"); 
             final String dept     = bookingObject.isNull("dept"); 


             Intent intent = new Intent(LoginActivity.this, NextActivity.class); 
             intent.putExtra("login", login); 
             intent.putExtra("realname", realname); 
             intent.putExtra("dept", dept); 
             LoginActivity.this.startActivity(intent); 
            } 
           } 
          } catch (JSONException e) { 
           e.printStackTrace(); 
          } 
         } else{ 
          Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_SHORT).show(); 
         } 
        } 
       }, new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 
         Toast.makeText(getApplicationContext(), "Volley error", Toast.LENGTH_SHORT).show(); 
        } 
       }){ 
        @Override 
        protected Map<String, String> getParams() throws AuthFailureError { 
         Map<String, String> params = new HashMap<>(); 
         params.put("login", etLogin.getText().toString()); 
         params.put("password", etPassword.getText().toString()); 
         return params; 
        } 
       }; 
       MySingleton.getInstance(getApplicationContext()).addToRequestQueue(stringRequest); 
      } 
     }); 
    } 

} 

感激,如果有人能幫助我。謝謝。

+0

是上面給JSON輸出你的整個JSON輸出?或者它是一部分? –

+0

@ R.R.M,如上輸出。它不是一個部分。 – Rozaimi

+0

如果是這樣的話,那麼如果你寫了response.equals(「success_login」)代碼的條件,它將不會進入內部。 –

回答

0
  1. 獲取JSON對象這樣

String url = "http://localhost/login.php"; 
      StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() { 
       @Override 
       public void onResponse(String response) { 
         Log.e("response>>>>","",response); 
        if(response.equals("success_login")){ 
         try { 
          JSONObject jsonObject = new JSONObject(response); 



            final String login     = jsonObject.getString("login"); 
            final String realname    = jsonObject.getString("realname"); 
            final String dept     = jsonObject.getString("dept"); 


            Intent intent = new Intent(LoginActivity.this, NextActivity.class); 
            intent.putExtra("login", login); 
            intent.putExtra("realname", realname); 
            intent.putExtra("dept", dept); 
            LoginActivity.this.startActivity(intent); 
           } 
          } 
         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 
        } else{ 
         Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_SHORT).show(); 
        } 
       } 
      }, new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        Toast.makeText(getApplicationContext(), "Volley error", Toast.LENGTH_SHORT).show(); 
       } 
      }){ 
       @Override 
       protected Map<String, String> getParams() throws AuthFailureError { 
        Map<String, String> params = new HashMap<>(); 
        params.put("login", etLogin.getText().toString()); 
        params.put("password", etPassword.getText().toString()); 
        return params; 
       } 
      }; 
      MySingleton.getInstance(getApplicationContext()).addToRequestQueue(stringRequest); 
     } 
+0

它沒有工作。 。我無法顯示Log.d(TAG,「JSON Object」+ jsonObject); – Rozaimi

+0

可以提供給我,Logcat,Log.d(TAG,「response >>>>」+ response); 在OnResponse中使用它 –

+0

它沒有顯示我的日誌中的任何值。 。它認爲字符串響應與jsonobject衝突。 。 – Rozaimi

相關問題