我必须从服务器检索文本并将其显示在编辑文本中。这是我的代码。我将此代码放在 onCreate 方法中。
EditText ed=(EditText)findViewById(R.id.edit_usr_nam);
EditText ed1=(EditText)findViewById(R.id.edit_name);
EditText ed2=(EditText)findViewById(R.id.edit_email);
EditText ed3=(EditText)findViewById(R.id.edit_age);
String UserName=ed.getText().toString();
String Name=ed1.getText().toString();
String Email=ed2.getText().toString();
String Age=ed3.getText().toString();
try{
HttpClient client=new DefaultHttpClient();
HttpPost post=new HttpPost("http://abcd");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
nameValuePairs.add(new BasicNameValuePair("username", UserName));
nameValuePairs.add(new BasicNameValuePair("form", "register"));
nameValuePairs.add(new BasicNameValuePair("name", Name));
nameValuePairs.add(new BasicNameValuePair("email", Email));
nameValuePairs.add(new BasicNameValuePair("age", Age));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=client.execute(post);
String responseText= inputStreamTOString(response.getEntity().getContent()).toString();
System.out.println("errrrooooorrrrrrr"+responseText);
JSONObject json = new JSONObject(responseText);
json.getString("response");
ed.setText(json.getString(UserName));
ed1.setText(json.getString(Name));
ed2.setText(json.getString(Email));
ed3.setText(json.getString(Age));
}
catch(Exception e){
Log.e("exvcx", "error getting data" +e.toString());
}
请告诉我哪里错了。谢谢