У меня возникла проблема в Android Studio о том, как запросить объект JSON.
Мой Logcat может печатать только String onResponse, но не значение JSONObject. У меня проблема в строке try {} внутри AccessActivity.java
Мое объяснение кода, как показано ниже.
У меня есть этот вывод JSON Object, который я извлекаю из базы данных mysql.
{ "access":"PA001", "password":"123", "fullname":"ARCADE", "branch":"HQ", "section":"MPR" }
В моем PHP-коде я получаю данные и кодирую их как объект JSON.
access.php
<?php $conn = mysqli_connect("","","",""); if( isset($_POST['access']) && isset($_POST['password']) ){ $access = $_POST['access']; $password = $_POST['password']; $sql = "SELECT * FROM table WHERE access = '$access' AND password = '$password' "; $result = mysqli_query($conn, $sql); if($result && mysqli_num_rows($result) > 0){ while($row = mysqli_fetch_array($result)){ $accessdb = $row['access']; $passworddb = $row['password']; $fullnamedb = $row['fullname']; $branchdb = $row['branch']; $sectiondb = $row['section']; echo "success_access"; $response = array('access' => $accessdb, 'password' => $passworddb, 'fullname' => $fullnamedb, 'branch' => $branchdb, 'section' => $sectiondb); echo json_encode($response); } mysqli_free_result($result); } else { echo "access_failed"; } } ?>
Но моя проблема возникает в Android Studio,
Вот мой код.
AccessActivity.java
public class AccessActivity extends AppCompatActivity{ final String TAG = this.getClass().getName(); EditText etAccess, 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); etAccess = (EditText)findViewById(R.id.etAccess); 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/access.php"; StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() { @Override public void onResponse(String response) { Log.e(TAG, "Response is = " + response); if(response.equals("success_access")){ try { JSONObject jsonObject = new JSONObject(response); Log.e(TAG, "JSON Object is = " + jsonObject); final String accessdb = jsonObject.getString("access"); final String passworddb = jsonObject.getString("password"); final String fullnamedb = jsonObject.getString("fullname"); final String branchdb = jsonObject.getString("branch"); final String sectiondb = jsonObject.getString("branch"); Intent intent = new Intent(AccessActivity.this, NextActivity.class); intent.putExtra("access", accessdb); intent.putExtra("password", passworddb); intent.putExtra("fullname", fullnamedb); intent.putExtra("branch", branchdb); intent.putExtra("section", sectiondb); AccessActivity.this.startActivity(intent); } catch (JSONException e) { e.printStackTrace(); } } else{ Toast.makeText(getApplicationContext(), "Access 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("access", etAccess.getText().toString()); params.put("password", etPassword.getText().toString()); return params; } }; MySingleton.getInstance(getApplicationContext()).addToRequestQueue(stringRequest); } }); } }
ОБНОВИТЬ :
мой логарифм отображает это
08-22 10:22:22.059 19801-19908/com.apps.test D/NetworkSecurityConfig: No Network Security Config specified, using platform default 08-22 10:22:22.064 19801-19801/com.apps.test E/my.com.apps.test.Activity: Response is = success_access{"access":"PA001","password":"123","fullname":"ARCADE","branch":"HQ","section":"HQ"} 08-22 10:22:22.112 19801-19825/com.apps.test D/EGL_emulation: eglMakeCurrent: 0xb3305060: ver 2 0 (tinfo 0xb3303200) 08-22 10:22:22.128 19801-19825/com.apps.test D/EGL_emulation: eglMakeCurrent: 0xb3305060: ver 2 0 (tinfo 0xb3303200) 08-22 10:22:22.134 19801-19825/com.apps.test D/EGL_emulation: eglMakeCurrent: 0xb3305060: ver 2 0 (tinfo 0xb3303200) 08-22 10:22:22.140 19801-19825/com.apps.test D/EGL_emulation: eglMakeCurrent: 0xb3305060: ver 2 0 (tinfo 0xb3303200) 08-22 10:22:23.697 19801-19825/com.apps.test D/EGL_emulation: eglMakeCurrent: 0xb3305060: ver 2 0 (tinfo 0xb3303200) 08-22 10:22:25.751 19801-19825/com.apps.test D/EGL_emulation: eglMakeCurrent: 0xb3305060: ver 2 0 (tinfo 0xb3303200) 08-22 10:22:25.780 19801-19825/com.apps.test D/EGL_emulation: eglMakeCurrent: 0xb3305060: ver 2 0 (tinfo 0xb3303200)
Цените, если кто-то может помочь мне решить эту проблему. Благодарю.