将用户输入的值传递给网址并显示结果

时间:2013-03-13 14:01:18

标签: android rest

基本上我试图将用户输入的值传递给网址并显示基于该结果的结果....因为我没有错误我不太确定什么是错的。非常感谢任何帮助

当我点击按钮时没有任何反应......我添加了eventlistener等等

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);

    }
}

1 个答案:

答案 0 :(得分:0)

正如njzk2所提到的,你过早地评估ET2。

你在onCreate()中这样做,这意味着在用户有机会在ET1中输入任何东西之前已经分配了ET2,所以它总是为空。

您需要做的是将ET2 = ET1.getText().toString();向下移动,作为button1OnClick中的第一行。

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

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