Sponser Link
Json parsing from url in android studio using java step by step
JavaScript Object Notation is standard format for publish the data on web or in file. It is very light weight. JSON is best alternative of SOAP format web service. JSON is on text base format and object can in an array format.
For parsing JSON data, we will create the JSON object, onPostExecute(String result) function will return the output html from given URL. We will explain in other article. We will create JSON node by passing parameter of array object name “clsStatus”. We have to give child node name for get the child node data.
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
/****************** Start Parse Response JSON Data *************/
String OutputData = "";
String UserName="";
JSONObject jsonResponse;
try {
/****** Creates a new JSONObject with name/value mappings from the JSON string. ********/
jsonResponse = new JSONObject(result);
/***** Returns the value mapped by name if it exists and is a JSONArray. ***/
/******* Returns null otherwise. *******/
JSONArray jsonMainNode = jsonResponse.optJSONArray("clsStatus");
/*********** Process each JSON Node ************/
int FlgStatus = 0;
int FlgState = 0;
int ColorCode = 0;
String Message ="";
String Card1="";
String Card2="";
String Card3="";
String EmpNo="";
String EmpName="";
int lengthJsonArr = jsonMainNode.length();
for(int i=0; i < lengthJsonArr; i++)
{
/****** Get Object for each JSON node.***********/
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
/******* Fetch node values **********/
FlgState= Integer.parseInt( jsonChildNode.optString("FlgState").toString());
FlgStatus = Integer.parseInt( jsonChildNode.optString("FlgStatus").toString());
ColorCode = Integer.parseInt( jsonChildNode.optString("ColorCode").toString());
Message = jsonChildNode.optString("Message").toString();
UserName = jsonChildNode.optString("UserName").toString();
if (FlgState==1)
{
EmpNo = jsonChildNode.optString("EmpNo").toString();
Card1 = jsonChildNode.optString("CardNo1").toString();
Card2 = jsonChildNode.optString("CardNo2").toString();
Card3 = jsonChildNode.optString("CardNo3").toString();
EmpName = UserName;
SaveEmpDetailInDevice(Card1, Card2, Card3, EmpNo, EmpName);
}
}
/****************** End Parse Response JSON Data *************/
EditText txtCardNo=(EditText)findViewById(R.id.edittxtCardNo);
if (FlgStatus==-1) // If Error is founded
{
lblMessage=(TextView)findViewById(R.id.lblmsg);
lblMessage.setText(Message);
txtCardNo.setText("");
Refreshtextbox_timer();
}
if (FlgStatus==1) // If Error is founded
{
lblMessage=(TextView)findViewById(R.id.lblmsg);
lblMessage.setText(Message);
txtCardNo.setText("");
Refreshtextbox_timer();
TextView toastMessage = (TextView) toast.getView().findViewById(android.R.id.message);
toastMessage.setTextColor(Color.BLACK);
toastMessage.setBackgroundColor(Color.RED);
toastMessage.setTextSize(25);
toast.show();
}
if (FlgStatus==2) // If request operation successful
{
lblMessage=(TextView)findViewById(R.id.lblmsg);
lblMessage.setText(Message);
}
if (ColorCode==1)
{
lblMessage=(TextView)findViewById(R.id.lblmsg);
lblMessage.setBackgroundColor(Color.WHITE);
lblMessage.setTextColor(Color.BLACK);
}
if (ColorCode==2)
{
lblMessage=(TextView)findViewById(R.id.lblmsg);
lblMessage.setBackgroundColor(Color.RED);
lblMessage.setTextColor(Color.YELLOW);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}