0

Webサーバーと通信するアプリに取り組んでいます。スレッドごとに呼び出す 2 つのメソッドが必要であり、UI を更新するために使用される 2 つのハンドラー メソッドも必要です。1 つのスレッド メソッドとハンドラー メソッドは、アクティビティの開始時にデータを取得して UI を更新する必要があります。2 スレッド メソッドとハンドラ メソッドは、サーバーにデータをポストバックする必要があります。これを実装する方法。

このクラスの私のコード...

 package com.edoc.doctor;

 import java.util.ArrayList;
 import java.util.HashMap;

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

 import com.edoc.libraries.FetchMedicines;
 import com.edoc.libraries.Functions;

 import android.app.Activity;
 import android.app.ProgressDialog;
 import android.content.SharedPreferences;
 import android.content.SharedPreferences.Editor;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.Message;
 import android.util.Log;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.widget.ArrayAdapter;
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.Spinner;
 import android.widget.Toast;

 public class AddPatient extends Activity implements Runnable, OnClickListener
 {
    public static final String appDoctor = "AppDoctor";

    public String KEY_SUCCESS = "success";

     private String success;

    private ProgressDialog pd;
     private JSONObject jsonObj;

    private boolean addNewPatient;

    private EditText email;
    private EditText phone;
    private EditText name;
    private EditText dob;
    private EditText lowBPMedicineInstructions;
    private EditText highBPMedicineInstructions;
    private EditText hyperLowBPMedicineInstructions;
    private EditText hyperHighBPMedicineInstructions;

    private Spinner lowBPMedicine;
    private Spinner highBPMedicine;
    private Spinner hyperLowBPMedicine;
    private Spinner hyperHighBPMedicine;

    private Button addPatient;


    private String strEmail;
    private String strPhone;
    private String strName;
    private String strDob;
    private String strLowBPMedicineInstructions;
    private String strHighBPMedicineInstructions;
    private String strHyperLowBPMedicineInstructions;
    private String strHyperHighBPMedicineInstructions;

     private String strLowBPMedicine;
private String strHighBPMedicine;
private String strHyperLowBPMedicine;
private String strHyperHighBPMedicine;

private ArrayAdapter<String> dataAdapter;
private ArrayList<String> medicines;
private ArrayList<Integer> medicineIDs;

protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_patient);

    medicines = new ArrayList<String>();
    medicineIDs = new ArrayList<Integer>();

    addPatient = (Button) findViewById(R.id.ib_add_patient);
    addPatient.setOnClickListener(this);

    email = (EditText) findViewById(R.id.iet_add_patient_email);
    phone = (EditText) findViewById(R.id.iet_add_patient_phone);
    name = (EditText) findViewById(R.id.iet_add_patient_name);
    dob = (EditText) findViewById(R.id.iet_add_patient_date_of_birth);

    lowBPMedicineInstructions = (EditText) findViewById(R.id.iet_low_description);
    highBPMedicineInstructions = (EditText) findViewById(R.id.iet_high_description);
    hyperLowBPMedicineInstructions = (EditText) findViewById(R.id.iet_hyper_low_description);
    hyperHighBPMedicineInstructions = (EditText) findViewById(R.id.iet_hyper_high_description);

    lowBPMedicine = (Spinner) findViewById(R.id.isp_low_medicine);
    highBPMedicine = (Spinner) findViewById(R.id.high_medicine);
    hyperLowBPMedicine = (Spinner) findViewById(R.id.hyper_low_medicine);
    hyperHighBPMedicine = (Spinner) findViewById(R.id.hyper_high_medicine);

    dataAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_dropdown_item_1line, medicines);

    final int did = getID();
//  pd = ProgressDialog.show(getApplicationContext(), "Working..", "Connect To Server", true,false);

    new Thread(new Runnable() 
    { 
        public void run()
        {
            Functions fun = new Functions();
            jsonObj = fun.fetchMedicines(did);
            handler.sendEmptyMessage(0);
        }
}).start();


}
private Handler handler = new Handler() 
{


    @Override
    public void handleMessage(Message msg) 
    {

        try {
            Log.e("try","try");
            success = jsonObj.getString("success");

            Log.e("success",success);
            Log.e("add",""+addNewPatient);
            if(success.equals("1") && !addNewPatient)
            {
                JSONArray m = jsonObj.getJSONArray("medicines");

                // looping through All Contacts
                for(int i = 0; i < m.length(); i++)
                {
                    JSONObject c = m.getJSONObject(i);
                    String name = c.getString("medicine");
                    int id = c.getInt("id");
                    Log.e("m name",name+" "+id);
                    medicines.add(name);
                    medicineIDs.add(id);
                }


                dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                lowBPMedicine.setAdapter(dataAdapter);

                dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                highBPMedicine.setAdapter(dataAdapter);

                dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                hyperLowBPMedicine.setAdapter(dataAdapter);

                dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                hyperHighBPMedicine.setAdapter(dataAdapter);

            }
            else if(success.equals("1") && addNewPatient)
            {
                Log.e("success", ""+addNewPatient);
                pd.dismiss();
                Toast.makeText(getApplicationContext(), "Patient Add Successfully",Toast.LENGTH_LONG).show();
                finish();
            }
            else
            {
                Toast.makeText(getApplicationContext(), "Server down",Toast.LENGTH_LONG).show();
                finish();
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
};


public int getID()
{
    int mode = Activity.MODE_PRIVATE;
    SharedPreferences mySharedPreferences = getSharedPreferences(appDoctor,mode);
    Editor editor = mySharedPreferences.edit();
    return mySharedPreferences.getInt("id",0);
}
@Override
public void run() 
{
    Functions function = new Functions();   
    jsonObj = function.addPatient(strEmail,strPhone,strName,strDob,strLowBPMedicine
            ,strHighBPMedicine,strHyperHighBPMedicine,strHyperLowBPMedicine,strLowBPMedicineInstructions
            , strHighBPMedicineInstructions, strHyperLowBPMedicineInstructions, strHyperHighBPMedicineInstructions, getID());
    addNewPatient = true;
    Log.e("run", ""+addNewPatient);
    handler.sendEmptyMessage(0);
}
@Override
public void onClick(View v) 
{
    Log.e("onclick","on click");

    strEmail = email.getText().toString();
    strPhone = phone.getText().toString();
    strName = name.getText().toString();
    strDob = dob.getText().toString();

    strLowBPMedicine = ""+lowBPMedicine.getSelectedItemPosition();
    strHighBPMedicine = ""+highBPMedicine.getSelectedItemPosition();
    strHyperLowBPMedicine = ""+hyperLowBPMedicine.getSelectedItemPosition();
    strHyperLowBPMedicine = ""+hyperLowBPMedicine.getSelectedItemPosition();

    strLowBPMedicineInstructions = lowBPMedicineInstructions.getText().toString();
    strHighBPMedicineInstructions = highBPMedicineInstructions.getText().toString();
    strHyperLowBPMedicineInstructions = hyperLowBPMedicineInstructions.getText().toString();
    strHyperHighBPMedicineInstructions = hyperHighBPMedicineInstructions.getText().toString();

    pd = ProgressDialog.show(this, "Working..", "Connect To Server", true,false);
    Thread t = new Thread();
    t.start();
}

  }

これは、3 番目のステートメントの下の 222 行目に貼り付けられました。pd = ProgressDialog.show(this, "Working..", "Connect To Server", true,false);

4

0 に答える 0