Android Json codes for Retriving/Displaying data




 

public class ViewPersonalDetails extends Activity {
      EditText et1;
      String jsonResult;
       private String url_view = "http://10.0.2.2/JsonAllInOne/Viewall.php";
       private String URL_deletepdetails ="http://10.0.2.2/JsonAllInO/Deleteperson.php";
      ListView lw;
       ProgressDialog g;
      JSONArray pdetails=null;
      ArrayList<HashMap<String, String>>personalList;
       @Override
       protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewpersonal);
        et1=(EditText)findViewById(R.id.del);
        lw = (ListView) findViewById(R.id.listv);
        g = new ProgressDialog(this);
      g.setCancelable(false);
        personalList=new ArrayList<HashMap<String, String>>();
        getData();
       }
       // Async Task to access the web
       private class JsonReaddetails extends AsyncTask<String, Void, String> {     
        @Override
        protected String doInBackground(String... params) {
         HttpClient httpclient = new DefaultHttpClient();
         HttpPost httppost = new HttpPost(params[0]);
         try {
          HttpResponse response = httpclient.execute(httppost);
          jsonResult = inputStreamToString(
            response.getEntity().getContent()).toString();
         }
       
         catch (ClientProtocolException e) {
          e.printStackTrace();
         } catch (IOException e) {
          e.printStackTrace();
         }
         return null;
        }
        private StringBuilder inputStreamToString(InputStream is) {
         String rLine = "";
         StringBuilder answer = new StringBuilder();
         BufferedReader rd = new BufferedReader(new InputStreamReader(is));
       
         try {
          while ((rLine = rd.readLine()) != null) {
           answer.append(rLine);
          }
         }
       
         catch (IOException e) {
          // e.printStackTrace();
          Toast.makeText(getApplicationContext(),"Error..." + e.toString(), Toast.LENGTH_LONG).show();
         }
         return answer;
        }
       
        @Override
        protected void onPostExecute(String result) {
              ShowResult();
        }
       }
       // end async task
       

       public void getData() {
             JsonReaddetails pdetails = new JsonReaddetails();
        // passes values for the urls string array
        pdetails.execute(new String[] { url_view });
       }
       
       // build hash set for list view
       public void ShowResult() {
             // either you can call ArrayList here or in the method
//personalList = new ArrayList<HashMap<String, String>>();
        try {
         JSONObject jsonResponse = new JSONObject(jsonResult);
         pdetails = jsonResponse.getJSONArray("personaldetail");
       // here we are looping
         for (int i = 0; i < pdetails.length(); i++) {
              
          JSONObject y = pdetails.getJSONObject(i);
          String person = y.getString("PersonalNo");
          String first= y.getString("FirstName");
         String last = y.getString("LastName");
         String other = y.getString("OtherName");
         String genda = y.getString("Gender");
         String nation = y.getString("Nationality");
         String email = y.getString("Email");
         String driving = y.getString("DrivingPermit");
          String outPut ="\n \n"+"PerNo=:"+" "+ person+"\n"  +"FName=:"+" "+first+"\n"+"LName=:"+" "
          +last+"\n"+"OName=:"+" "+other+"\n"+"Gender=:"+" "+genda+"\n"+"Nationality=:"+nation+"\n"+"Email=:"+" "+email+"\n"+"Permit=:"+" "+driving+"\n";
          personalList.add(Createperson("personaldetails", outPut));
         }
        } catch (JSONException e) {
         Toast.makeText(getApplicationContext(), "Error" + e.toString(),
           Toast.LENGTH_SHORT).show();
        }
       
        SimpleAdapter simpleAdapter = new SimpleAdapter(this, personalList,android.R.layout.simple_list_item_1,
          new String[] { "personaldetails" }, new int[] { android.R.id.text1 });
        lw.setAdapter(simpleAdapter);
       }
       // this definies the table.You can use all field or you can use afew
       private HashMap<String, String> Createperson(String  personalno, String firstName) {
        HashMap<String, String> personalList = new HashMap<String, String>();
       personalList.put(personalno, firstName);
        return personalList;
       }
Share on Google Plus

About Unknown

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment

0 comments:

Post a Comment