2016-11-06 156 views
0

我正在使用json對象請求從我創建的數據庫中獲取信息。我能夠與我的Android應用程序插入數據到數據庫,但現在我想檢查例如用戶是否創建了一個用戶帳戶。 當我嘗試創建json對象請求時,出現語法錯誤。這是一個問題,但我不明白什麼似乎是問題。 url和php文件都可以正常工作。JSON對象請求

String showUrl = "http://192.168.0.16/webapps/showUser.php"; 
public void searchLoginInfo(View view) { 
    JsonObjectRequest jsonObjectRequest = new /*HERE IS WHERE I GET THE ERROR*/ JsonObjectRequest(Request.Method.POST,showUrl,(String)null, new Response.Listener<JSONObject>() { 
     @Override 
     public void onResponse(JSONObject response) { 
      try { 
       JSONArray users = response.getJSONArray("users"); 
       for (int i = 0; i < users.length(); i++){ 
        JSONObject user = users.getJSONObject(i); 
        String username = user.getString("username"); 
        String password = user.getString("password"); 
        if (username.equals(myLoginList.get(0)) && password.equals(myLoginList.get(1))) { 
         Toast.makeText(LoginActivity.this, "Login Succesfull", Toast.LENGTH_LONG).show(); 
         Intent send = new Intent(LoginActivity.this, WelcomeActivity.class); 
         startActivity(send); 
         break; 
        }else{ 
         Toast.makeText(LoginActivity.this, "Login Failed!", Toast.LENGTH_LONG).show(); 
         Intent send = new Intent(LoginActivity.this, LoginActivity.class); 
         startActivity(send); 
        } 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      Log.e("Volley Error", error.toString()); 
      NetworkResponse networkResponse = error.networkResponse; 
      if (networkResponse != null) { 
       Log.e("Status code", String.valueOf(networkResponse.statusCode)); 
      } 
     } 
    }); 
    requestQueue.add(jsonObjectRequest); 
} 

這是我的錯誤:錯誤:(60,98)錯誤:不兼容的類型:字符串不能轉換到的JSONObject。

這是我showUser.php

<?php 
if($_SERVER["REQUEST_METHOD"]=="POST"){ 
    include 'connection.php'; 
    showUser(); 
} 

function showUser(){ 
    global $connect; 
    $query = "SELECT* FROM user;"; 
    $result = mysqli_query($connect,$query); 
    $number_of_rows = mysqli_num_rows($result); 
    $temp_array = array(); 
    if($number_of_rows>0){ 
     while($row = mysqli_fetch_assoc($result)){ 
      $temp_array[] = $row; 
     } 
    } 
    header('Content-Type:application/json'); 
    json_encode(array("users"=>$temp_array)); 
    mysqli_close($connect); 
} 
?> 
+0

這將是更好,如果你發佈錯誤 –

+0

@SarmadAijaz錯誤:(60,98)錯誤:不兼容的類型:字符串不能轉換爲JSONObject的 – Roberto

+0

什麼你在'response'變量中獲得的值? –

回答

0

只需卸下投給String。試試這個,

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, showUrl, null, 
    new Response.Listener<JSONObject>(){ 

     @Override 
     public void onResponse(JSONObject response) { 

     } 
    } 
); 

錯誤說服務器正在返回一個String輸出。您可以使用StringRequest解決錯誤,

StringRequest stringRequest = new StringRequest(Request.Method.POST, showUrl, 
    new Response.Listener<String>() { 

     @Override 
     public void onResponse(String response) { 
      Log.e("response", response); 
     } 
    }, 
    new Response.ErrorListener() { 

     @Override 
     public void onErrorResponse(VolleyError error) { 

     } 
    } 
); 
+0

應用程序崩潰 – Roberto

+0

檢查編輯並在問題中添加logcat輸出 –

+0

不投射它不會給我錯誤不過,它似乎還沒有達到onResponse方法 – Roberto