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

时间:2013-05-05 16:20:56

标签: 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.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();
}

  }

这就停留在第222行 形式低于第三声明。      pd = ProgressDialog.show(this,“Working ..”,“Connect To Server”,true,false);

0 个答案:

没有答案