Android:从服务器获取编辑文本中的数据

时间:2012-07-09 11:07:49

标签: android json httpclient android-edittext

我有三个EditText

  1. 用户名
  2. 名称
  3. 电子邮件
  4. 我想在用户登录后自动填写这些EditText,并且我希望使用json从服务器获取这些值。 任何人都可以帮助我这样做。我是json解析的新手所以请帮助

1 个答案:

答案 0 :(得分:1)

您必须在单击时实现该方法以获取EditText的值。

String username = youedittext.getText().toString();
String name = youedittext2.getText().toString();
String email = youedittext3.getText().toString();

DefaultHttpClient client = new DefaultHttpClient();
                    ArrayList<NameValuepair> nvp = new ArrayList<NameValuePair>();
                    nvp.add(new BasicNameValuePair("username", username));
                    nvp.add(new BasicNameValuePair("name", name));
                    nvp.add(new BasicNameValuePair("mail", mail));
        HttpPost hpost = new HttpPost(url);
        hpost.setEntity(new UrlEncodedFormEntity());
        HttpResponse response = client.execute(hpost);

        HttpEntity entity = response.getEntity();
        InputStream is = entity.getContent();

        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        String result = sb.toString();

        JSONObject jobject = new JSONObject(result);
                    //Here you have to read the JSONObject it's very easy

                Log.i("Content:",jobject.toString());
相关问题