Search code examples
androidjsontextview

how to display json object in textview


this is the activity .. plss help .. it doesn't appear on my textview ..What I'm trying to do here is displaying the profile of the admin upon clicking profile option in navigation drawer .. thanks in advance ..

please kindly comment your suggestion on how to solve this ..

    public class Profile extends Fragment{
     int success;
View profile;
JSONParser jsonParser = new JSONParser();
JSONObject json;
TextView tvName,tvNumber,tvEmail,tvUsername,tvPass,tvAddress;
String adminName, adminNumber,adminEmail,adminUsername,adminPassword,adminAddress;

private static final String PROFILE_URL =   "http://jcasim.5gbfree.com/Project/JCASIM_Api/profile.php";
private static final String TAG_SUCCESS = "success";
// private static final String TAG_ID = "id";



public Profile (){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View profile = inflater.inflate(R.layout.profile, container,false);
    tvName = (TextView)profile.findViewById(R.id.tvAdminName);
    tvNumber = (TextView)profile.findViewById(R.id.tvAdminNumber);
    tvEmail = (TextView)profile.findViewById(R.id.tvAdminEmail);
    tvUsername = (TextView)profile.findViewById(R.id.tvAdminUsername);
    tvPass = (TextView)profile.findViewById(R.id.tvAdminPassword);
    tvAddress = (TextView)profile.findViewById(R.id.tvAdminAddress);

    new profile().execute();
    return profile;
}
class profile extends AsyncTask<String, String, String>{
    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub
        String addminObject ="";
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("addminObject", addminObject));
        json = jsonParser.makeHttpRequest(PROFILE_URL, "POST",params);
        try {
            // json success tag
            success = json.getInt(TAG_SUCCESS);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;

    }
    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        if (success == 1){
            try {
                JSONArray myarr = json.getJSONArray("addminObject");

                for(int i =0; i<myarr.length();i++){
                    JSONObject s = myarr.getJSONObject(i);

                    String adminName = s.getString("CompleteName");
                    String adminNumber = s.getJSONObject("1").getString("Number");
                    String adminEmail = s.getJSONObject("2").getString("Email");
                    String adminUsername = s.getJSONObject("3").getString("Username");
                    String adminPassword = s.getJSONObject("4").getString("Password");
                    String adminAddress = s.getJSONObject("5").getString("Address");


                    tvName.setText(adminName);
                    tvNumber.setText(adminNumber);
                    tvEmail.setText(adminEmail);
                    tvUsername.setText(adminUsername);
                    tvPass.setText(adminPassword);
                    tvAddress.setText(adminAddress);
                }

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }   


        }
    }

And here's my my json ..

    {"success":1,"addminObject":[{"CompleteName":"Joenevie V.Almonte","Number":"09167801027","Email":"devied24@gmail.com","Username":"admin","Password":"pass","Address":"Sa puso ng mahal ko .. :)"}]}

Solution

  • You are doing it wrong. It should be like this instead.

       String adminName = s.getString("CompleteName");
       String adminNumber = s.getString("Number");
       String adminEmail = s.getString("Email");
       String adminUsername = s.getString("Username");
       String adminPassword = s.getString("Password");
       String adminAddress = s.getString("Address");