Peps Peps - 2 months ago 18
Android Question

JSON Parsing in Android

I am having problem parsing JSON in my

model.class
I am creating an android app that will request for the user to input the username and password and after clicking the button it will now send a POST request and the request will response with a JSON data.

Here is my request in android studio:

//StudentInfo or Login
client.post("<url here>", params, new AsyncHttpResponseHandler() {

@Override
public void onSuccess(String response) {
try{
StudentModel sm = new StudentModel(obj);
sm.retrievalData();
flag = true;
Toast.makeText(getApplicationContext(), "Redirecting...", Toast.LENGTH_LONG).show();
navigatetoProfileActivity();
}
}catch(Exception e){
Toast.makeText(getApplicationContext(), "Error Occured [Server's JSON response might be invalid]!", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
});


And here is my StudentModel.class

public void retrievalData(){
//some code to parse the JSON



}


Well, I am a beginner in json parsing and here is the JSON Response.

{
"info": {
"add": "<data>",
"firstName": "<data>",
"lastName": "<data>",
"middleName": "<data>",
}
}

Answer

Well, what you can do in retrievalData() is:

String add = <JSONobjectVariableName>.getJSONObject("info").getString("add");
String firstName = <JSONobjectVariableName>.getJSONObject("info").getString("firstName");

and so on.

Comments