Android 线程和处理程序每次调用不同的方法

Android 线程和处理程序每次调用不同的方法,android,multithreading,Android,Multithreading,我正在开发与web服务器通信的应用程序。我需要两个通过线程调用的方法,还需要两个用于更新UI的处理程序方法。1线程方法和处理程序方法需要在活动开始时获取数据并更新UI。2线程方法和处理程序方法需要在服务器上回发数据。 我如何实现这一点 我的这个类的代码 package com.edoc.doctor; import java.util.ArrayList; import java.util.HashMap; import org.json.JSONArray; import org

我正在开发与web服务器通信的应用程序。我需要两个通过线程调用的方法,还需要两个用于更新UI的处理程序方法。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();
}

  }
package com.edoc.doctor;
导入java.util.ArrayList;
导入java.util.HashMap;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入com.edoc.libraries.FetchMedicines;
导入com.edoc.libraries.Functions;
导入android.app.Activity;
导入android.app.ProgressDialog;
导入android.content.SharedReferences;
导入android.content.SharedReferences.Editor;
导入android.os.Bundle;
导入android.os.Handler;
导入android.os.Message;
导入android.util.Log;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.ArrayAdapter;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.Spinner;
导入android.widget.Toast;
公共类AddPatient extends活动实现可运行的OnClickListener
{
公共静态最终字符串appDoctor=“appDoctor”;
公共字符串密钥\u SUCCESS=“SUCCESS”;
私人字符串成功;
私营部门;
私有JSONObject JSONObject;
私家医院;新病人;
私人文本电子邮件;
私人文本电话;
私有文本名;
私人编辑文本dob;
私人编辑文本和药品说明书;
私人编辑文本highBPMedicineInstructions;
私人编辑文本hyperLowBPMedicineInstructions;
私人编辑文本hyperHighBPMedicineInstructions;
私人纺纱机;
私人纺纱机;
私人纺纱机;
私人纺纱机;
私人病人;
私密邮件;
私人电话;
私有字符串strName;
私有字符串strDob;
私有字符串strLowBPMedicineInstructions;
私有字符串strHighBPMedicineInstructions;
私有字符串strHyperLowBPMedicineInstructions;
私有字符串strHyperHighBPMedicineInstructions;
私人字符串strLowBPMedicine;
私人医疗;
私人字符串strHyperLowBPMedicine;
私家字符串标准型;
专用阵列适配器数据适配器;
私人ArrayList药品;
私人ArrayList medicineIDs;
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(右布局,添加患者);
药物=新ArrayList();
MedicineId=新的ArrayList();
addPatient=(按钮)findViewById(R.id.ib\u add\u patient);
addPatient.setOnClickListener(此);
电子邮件=(EditText)findViewById(R.id.iet\u add\u patient\u email);
phone=(EditText)findViewById(R.id.iet\u add\u patient\u phone);
name=(EditText)findViewById(R.id.iet\u add\u patient\u name);
dob=(EditText)findViewById(R.id.iet\u添加患者\u日期\u出生日期);
lowBPMedicineInstructions=(EditText)findViewById(R.id.iet\u low\u描述);
highBPMedicineInstructions=(EditText)findViewById(R.id.iet\u high\u描述);
hyperLowBPMedicineInstructions=(EditText)findViewById(R.id.iet\u hyper\u low\u描述);
hyperHighBPMedicineInstructions=(EditText)findViewById(R.id.iet\u hyper\u high\u描述);
lowBPMedicine=(微调器)findViewById(R.id.isp\u low\u medicine);
highBPMedicine=(微调器)findViewById(R.id.high_medicine);
hyperLowBPMedicine=(微调器)findViewById(R.id.hyper\u low\u medicine);
hyperHighBPMedicine=(微调器)findViewById(R.id.hyper\u high\u medicine);
dataAdapter=new ArrayAdapter(此,
android.R.layout.simple\u下拉列表\u项目线,药品);
final int did=getID();
//pd=ProgressDialog.show(getApplicationContext(),“正在工作…”,连接到服务器,true,false);
新线程(newrunnable())
{ 
公开募捐
{
函数fun=新函数();
jsonObj=fun.fetch(did);
handler.sendEmptyMessage(0);
}
}).start();
}
私有处理程序=新处理程序()
{
@凌驾
公共无效handleMessage(消息消息消息)
{
试一试{
Log.e(“try”、“try”);
success=jsonObj.getString(“success”);
Log.e(“成功”,成功);
Log.e(“添加”、“添加新患者”);
if(success.equals(“1”)&&&!addNewPatient)
{
JSONArray m=jsonObj.getJSONArray(“药物”);
//通过所有触点循环
对于(int i=0;i