← Back to list

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:

www.jyotishgher.in · 2025-07-16 04:15 · 0 claps · 1.2 min read
#postman #primitive-data-types #json-object #android
Open on Medium ↗

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:

Handle Response as a raw primitive instead of a JSONObject from Postman

Handle Response as a raw primitive instead of a JSONObject from Postman

int status = response.getInt(“1”); if this is your case then My fiends

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:

json
1

Then 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:

java
StringRequest 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;
    }
    @Override
    public 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, use StringRequest, not JsonObjectRequest.
  • 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_id
6d7ac9f2bf0d
slug
handle-response-as-a-raw-primitive-instead-of-a-jsonobject-from-postman-6d7ac9f2bf0d
url
https://medium.com/@jyotishgher/handle-response-as-a-raw-primitive-instead-of-a-jsonobject-from-postman-6d7ac9f2bf0d
canonical_url
https://medium.com/@jyotishgher/handle-response-as-a-raw-primitive-instead-of-a-jsonobject-from-postman-6d7ac9f2bf0d
author_url
https://medium.com/@jyotishgher
status
ok
fetched_at
2026-08-24 07:32:38