Raw Primitive Response in Postman

Jyotishgher Astrology
By -
0

 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
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).


Tags:

Post a Comment

0Comments

Post a Comment (0)