0

基本上我试图将用户输入的值传递给一个 url 并基于它显示结果....因为我没有收到任何错误,我不太确定出了什么问题。任何帮助是极大的赞赏

当我单击按钮时,什么也没有发生...我添加了事件监听器等

package com.example.jsonrestclient;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.EditText;

public class MainActivity extends Activity {

protected TextView tv1;
protected TextView tv2;
protected TextView tv3;
protected TextView tv4;
protected EditText ET1;
protected String ET2;



 @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv1 = (TextView) findViewById(R.id.textView1);
        tv2 = (TextView) findViewById(R.id.textView2);
        tv3 = (TextView) findViewById(R.id.textView3);
        tv4 = (TextView) findViewById(R.id.textView4);
        ET1 = (EditText) findViewById(R.id.editText1);
        ET2 = ET1.getText().toString();
    }

    public void button1OnClick(View v) {
        String patientString = HttpHandler.HttpGetExec(HttpHandler.baseURI + ET2);

        String patientFName = "NOT FOUND",
                patientLName = "NOT FOUND",
                DOB = "NOT FOUND",
                patientGender = "NOT FOUND",
                hospitalNumber = "NOT FOUND";
        JSONObject patientObj = null;

        try {
            patientObj = new JSONObject(patientString);
            patientFName = (String) patientObj.get("PatientFName");
            patientLName = (String) patientObj.get("PatientLName");
            DOB = (String) patientObj.get("DOB");
            patientGender = (String) patientObj.get("PatientGender");
            hospitalNumber = (String) patientObj.get("HospitalNumber");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        tv1.setText(patientFName + " " + patientLName);
        tv2.setText("DOB" + "  " + DOB);
        tv3.setText("Gender" + " " + patientGender);
        tv4.setText("Hospital" + " " + hospitalNumber);

    }
}
4

1 回答 1

0

正如 njzk2 提到的,您对 ET2 的评估为时过早。

您在 onCreate() 中执行此操作,这意味着在用户有机会在 ET1 中键入任何内容之前已经分配了 ET2,因此它始终为空。

您需要做的是ET2 = ET1.getText().toString();向下移动,作为button1OnClick.

大概button1OnClick是绑定到android:onClick你视图xml中的参数吧?

另外,什么是HttpHandler?我无法从你的进口中看出。我不确定它是什么,但可能值得考虑将其余调用移到 AsyncTask 中。

于 2013-12-26T14:19:07.080 回答