2016-11-06 122 views
0
public void searchLoginInfo(View view) { 
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, showUrl,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").toLowerCase(); 
        String password = user.getString("password".toLowerCase()); 
        String retypedpassword = user.getString("retypedpassword"); 
        String email = user.getString("email"); 
        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); 
} 

我正在使用JSON對象請求從我創建的數據庫中獲取信息。我能夠與我的Android應用程序插入數據到數據庫,但現在我想檢查例如用戶是否創建了一個用戶帳戶。 url和php文件都可以正常工作。我在onErrorResponse方法中使用了一個日誌來查看實際的錯誤,我得到了這個:E/Volley錯誤:com.android.volley.ParseError:org.json.JSONException:Value java.lang.String類型的連接無法轉換爲JSONObject。我不知道這似乎是什麼問題。如果我的web服務正在返回json數據,我也檢查過使用postamn。 Printscreen with the json data 任何人都可以幫助我嗎?在此先感謝JSON對象響應錯誤

+0

Volley的聲音無法轉換爲json任何字符串,你傳遞它。我們真的需要看看傳遞給Volley的是什麼,以確定它有什麼問題。 – saywhatnow

+0

@saywhatnow我該怎麼做? – Roberto

+0

在每一步,'Log.debug'的數據,所以你可以看到你實際工作。 如果你不明白爲什麼某件事會以某種特定的方式表現出來,那就把它看成小塊。 :) – saywhatnow

回答

0

而不是JSONRequest嘗試StringRequest並檢查您的響應是否有效json或不。 JSONRequest嘗試將字符串解析爲JSONObject,然後返回,如果響應不是JSON,則可能會失敗。

0

您需要測試您的Web服務是否返回值。 例如,您可以使用導航器或郵遞員測試此功能。

+0

我做了使用郵遞員,它返回JSON數據。我已經上傳了一張照片。 @SofDroid – Roberto

+0

所以,你的問題是在你的代碼。 – SofDroid

+0

你應該修改'JsonObjectRequest'對象'JsonArrayRequest' – SofDroid