如何在textview中显示json对象

时间:2014-09-23 17:14:52

标签: android json textview

这是活动.. plss帮助..它没有出现在我的文本视图中..我在这里尝试做的是在导航抽屉中单击配置文件选项时显示管理员的配置文件。提前谢谢..

请好好评论一下如何解决这个问题的建议..

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


        }
    }

这是我的json ..

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

2 个答案:

答案 0 :(得分:0)

你做错了。它应该是这样的。

   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");

答案 1 :(得分:0)

HashMap<String, String> modelItem = new HashMap<>();

// adding each child node to HashMap key => value
modelItem.put("oId", ID);
modelItem.put("offenceName", offencename1);
modelItem.put("amount", amount);

// adding contact to contact list
contactList.add(modelItem);

id.setText(ID);
offence.setText(offencename1);
amt.setText(amount);