📌  相关文章
📜  在 android 中读取 json url - NAYCode.com - Java 代码示例

📅  最后修改于: 2022-03-11 14:52:10.704000             🧑  作者: Mango

代码示例1
//*********** For more tutorial visit NAYCode.com

@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();

         }

}

}