Reputation: 131
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);
}
Upvotes: 0
Views: 2469
Reputation: 2737
below is the JSONResponse -
{
"code": 401,
"message": "some message text"
}
u can parse it as below -
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);
}
Now simply parse the response from buffer -
JSONObject jObj = new JSONObject(buffer.toString());
String message = jObj.getString("message");
U r extracted message is in string message.
Upvotes: 2
Reputation: 60
you used
jsonObject jsonobject= new jsonObject("your json string");
String Code= jsonobject.getString(code);
String message= jsonobject.getString(message);
Upvotes: 1
Reputation: 2448
JSON STRING:
{
"code": 401,
"message": "some message text"
}
Now create a seprate class for response:
public class ResponseDTO{
int code;
String message;
}
Now if you are using android studio add following to gradle or if you are using Eclipse you can find jar on link: http://www.java2s.com/Code/Jar/g/Downloadgson222jar.htm
compile 'com.google.code.gson:gson:2.3'
After that use the following code to parse json string you already have:
String json = "the json you've recieved from server";
//ASSUMSE YOUR JSON IS NOT NULL
ResponseDTO response = new GsonBuilder().create().toJson(json.toString, ResponseDTO.class);
if (response != null) {
if (response.message.length() > 0 && response.message != null) {
//DO WHATEVER YOU WANT}
else{
//DO WHATEVER YOU WANT
}
}
}
Upvotes: 1