我在一个应该连接到 servlet 的 android 应用程序中有一个 Activity 。我有两个 EditText 字段并通过 editText_name.getText().toString 方法获取两个输入。但是应用程序无法捕获输入和在Log中打印出来。我的代码 fragment 如下
public class ServletActivity extends Activity {
Button button;
TextView outputText;
//public String URL = "http://10.0.2.2:8080/MyHL7Server /getPatientDetails?patientID";
public String PatientDataURL = "http://192.168.161.129:8080/MyHL7Server/getPatientDetails?patientID";
public String SensorListURL="http://192.168.161.129:8080/MyHL7Server/getSensorDetails?CenterID";
public String PatientInfo=new String();
public String SensorList=new String();
public String PatientIdInput=new String();
public String CenterIdInput=new String();
private ProgressDialog progressMessage;
int patientDataFlag,sensorDataFlag=0;
EditText PatientIdInputField;
EditText CenterIdInputField;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_servlet);
//final Button ConnectServerButton=(Button)findViewById(R.id.button2);
button = (Button) findViewById(R.id.button);
outputText = (TextView) findViewById(R.id.textView3);
PatientIdInputField=(EditText)findViewById(R.id.editText1);
CenterIdInputField=(EditText)findViewById(R.id.editText2);
PatientIdInput=PatientIdInputField.getText().toString();
CenterIdInput=CenterIdInputField.getText().toString();
Log.e("PatientIdInput is",PatientIdInput);
Log.e("CenterIdInput is",CenterIdInput);
System.out.println("Inside Servlet Activity");
Log.e("Testing","Servlet Activity");
;
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.e("Patient Id Input",PatientIdInput);
Log.e("CenterId Input",CenterIdInput);
PatientDataURL.concat("=").concat(PatientIdInput);
Log.e("Debug PatientDataURL created",PatientDataURL);
Log.e("Debug","Calling GetPatientDataTask");
GetPatientDataTask patientTask = new GetPatientDataTask();
patientTask.execute(new String[] { PatientDataURL });
Log.e("PatientData",PatientInfo);
Toast.makeText(getApplicationContext(),
"Patient data is : "+PatientInfo, Toast.LENGTH_LONG).show();
System.out.println("Patient Data Obtained "+PatientInfo);
}
});
}
该应用程序能够通过 AsyncTask 连接到 servlet。但我担心即使我提供了一些输入,userinput 字段仍然为空。
任何人都可以阐明这个问题吗?我是否必须更改我的 AndroidManifest.xml 文件中的任何内容???
我们将不胜感激。
最佳答案
您正在 onCreate()
中获取数据。
在按钮的 onClick 事件中移动这些行。
PatientIdInput=PatientIdInputField.getText().toString();
CenterIdInput=CenterIdInputField.getText().toString();
希望您能理解其中的区别。
关于Android EditText 字段无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17359179/