【问题标题】:Android: getting data in edit text from serverAndroid:从服务器获取编辑文本中的数据
【发布时间】:2012-07-09 11:07:48
【问题描述】:

我有三个 EditText

  1. 用户名
  2. 姓名
  3. 电子邮件

我想在用户登录后自动填写这些 EditText,并且我希望使用 json 从服务器获取这些值。 任何人都可以帮助我做到这一点。我是json解析的新手所以请帮忙

【问题讨论】:

  • 嘿!你需要在这里跳过一些代码片段......所以我们可以了解一下?
  • 到目前为止你做了什么?您涵盖过任何帖子或教程吗?
  • 解析json字符串后,您将得到用户名、姓名和电子邮件作为字符串。在editext中将这些值设置为edittext.settext(username);与姓名和电子邮件相同。

标签: android json httpclient android-edittext


【解决方案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());

【讨论】:

  • 我认为这个用户需要将数据填充到edittext中......所以你的代码不完整你需要将这些值放入edittext,或者我可以为你做吗?......像这样:username.setText(username);请编辑您的答案
  • 嘿,我在return jobject 行收到错误消息。它是说将返回类型更改为 JSONObject。
  • 你能告诉我如何使用httpget吗??
  • @MarcOrtiz 请让他完成他的工作
猜你喜欢
  • 2020-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-28
  • 1970-01-01
  • 1970-01-01
  • 2011-12-09
  • 2016-09-02
相关资源
最近更新 更多