Handle Response as a raw primitive instead of a JSONObject from Postman
Post man return simple 1 as response but i am not getting here using this in android java:
You’re getting an error because you’re trying to retrieve the key "1" from the JSON object, but in your case Postman is returning the value 1 as the entire response, not a value under a key.
🔍 Example: What you’re receiving from the API
If your API response is just:
json1Then this is not a JSON object. It’s just a raw number (int) and not:
json{ "status": 1 }✅ Solution: Handle response as a raw primitive instead of a JSONObject
You should use a StringRequest or JsonPrimitive type logic.
✅ Use StringRequest if response is just 1:
javaStringRequest request = new StringRequest(Request.Method.POST, url, response -> { if (response.trim().equals("1")) { // Success Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show(); } else { // Failure or other value Toast.makeText(getApplicationContext(), "Failed: " + response, Toast.LENGTH_SHORT).show(); } }, error -> { Toast.makeText(getApplicationContext(), "Network Error", Toast.LENGTH_SHORT).show(); }) { @Override public Map<String, String> getHeaders() { Map<String, String> headers = new HashMap<>(); headers.put("Content-Type", "application/json"); return headers; }@Overridepublic byte[] getBody() {JSONObject jsonObject = new JSONObject();try {jsonObject.put("username", user);jsonObject.put("password", password);} catch (JSONException e) {e.printStackTrace();}return jsonObject.toString().getBytes(StandardCharsets.UTF_8);}};RequestQueue queue = Volley.newRequestQueue(this);queue.add(request);
✅ Summary:
- If the response is just
1, useStringRequest, notJsonObjectRequest. response.getInt("1")will throw error because it's not a key in a JSON object.- Always
.trim()the response string when comparing (to avoid newline/whitespace issues).

Post a Comment
0Comments