H4F H4F - 4 months ago 15
JSON Question

json: unable to parse this type of json response

I know object is like some:

{
and array like some:
[

But when i do post request api response with,

{"code": 401, "message":"some message text"}


What kind of json is that, I had tried alot to display the message in a textview but none worked meanwhile I'm able to display object response or array... I'm using
Volley
by the way, I had tried also to check

If(response.has("message"){
}else{
}


And the same check for "code" to switch it to message from string resources
Thanks in advance

HERE THE FULL REQUEST:

private void checkLogin(final String email, final String password) {
// Tag used to cancel the request
String tag_string_req = "req_login";

pDialog.setMessage("Logging in ...");
showDialog();

StringRequest strReq = new StringRequest(Method.POST,
AppConfig.URL_LOGIN, new Response.Listener<String>() {

@Override
public void onResponse(String response) {
Log.d(TAG, "Login Response: " + response.toString());
hideDialog();

try {
JSONObject jObj = new JSONObject(response);
if (jObj.has("user")) {
// user successfully logged in
// Create login session
session.setLogin(true);

JSONObject user = jObj.getJSONObject("user");
String uid = user.getString("id_user");
String uname = user.getString("name");
String uemail = user.getString("email");
String utoken = user.getString("user_token");
// Inserting row in users table
db.addUser(uname, uemail, uid, utoken);
// Launch main activity
Intent intent = new Intent(LoginActivity.this,
Activity1.class);
startActivity(intent);
finish();
}else {
JSONObject jOsbj = new JSONObject(response);
TextView scss = (TextView) findViewById(R.id.loginerror);
scss.setText(jOsbj.getInt("code"));
String errorMsg = scss.getText().toString();
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_LONG).show();
}


} catch (JSONException e) {
// JSON error
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}

}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Login Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
hideDialog();
}
}) {

@Override
protected Map<String, String> getParams() {
// Posting parameters to login url
Map<String, String> params = new HashMap<String, String>();
params.put("email", email);
params.put("password", password);

return params;
}

};

// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}

Answer

below is the JSONResponse -

{
    "code": 401,
    "message": "some message text"
}

u can parse it as below -

  1. assumming u have url response in Inputstream in then u read it in a stringBuffer buffer as below -

    StringBuffer buffer = new StringBuffer();
    int ch = -1;
    
    while ( (ch=in.read()) != -1){
       buffer.append((char) ch);
    }
    
  2. Now simply parse the response from buffer -

    JSONObject jObj = new JSONObject(buffer.toString());
    String message = jObj.getString("message");
    
  3. U r extracted message is in string message.