如果网络连接缓慢或无连接,进度对话框永远不会结束

时间:2015-06-23 16:54:33

标签: android service android-asynctask

我的应用程序从服务器获取数据并使用asyntask保存到sqlite,但如果有缓慢或没有互联网连接,进度对话框永远不会完成并继续搜索互联网。我的应用程序还运行服务将记录上传到服务器所以我不想在代码中使用另一个计时器。请建议我任何解决方案。

 public class DoctorsCallPlannigOperation extends AsyncTask<String, Void,  String> {

ProgressDialog pd;
Context cxt;
public static List<String> listplannum;
public static List<String> listrefnum;
public static List<String> listcreateddate;
public static List<String> listdrcode;
public static List<String> listplandate;
public static List<String> listffmgr;
public static List<String> listffcode;
public static List<String> listmon;
public static List<String> listterrcode;
public static List<String> listmorn_even;

public static String tmp,TerrCode;

// String Response = "{\"Successful\":true}";

public DoctorsCallPlannigOperation(Context context,String terrcode) {
    // TODO Auto-generated constructor stub
    cxt = context;
    pd = new ProgressDialog(context);
    pd.setTitle("Please wait");
    pd.setMessage("Loading...");
    pd.setCancelable(false);
    cxt= context;
    TerrCode=terrcode;
}

@Override
protected String doInBackground(String... urls) {
    RestAPI restAPI = new RestAPI();
    JSONObject jsonObj = new JSONObject();

    try {

        jsonObj = restAPI.getDocCallsPlanning(TerrCode);        
        tmp = jsonObj.toString();


        JSONObject jo = new JSONObject(tmp);
        JSONArray ja = jo.getJSONArray("Value");

        listplannum = new ArrayList<String>();
        listrefnum = new ArrayList<String>();
        listcreateddate = new ArrayList<String>();
        listdrcode = new ArrayList<String>();
        listplandate = new ArrayList<String>();
        listffmgr = new ArrayList<String>();
        listffcode = new ArrayList<String>();
        listmon = new ArrayList<String>();
        listterrcode = new ArrayList<String>();
        listmorn_even = new ArrayList<String>();


        for (int i = 0; i < ja.length(); i++) {

            jo = ja.getJSONObject(i);
            listplannum.add(jo.getString("PLAN_NO"));
            listrefnum.add(jo.getString("REF_NO"));
            listcreateddate.add(jo.getString("CREATED_DATE"));
            listdrcode.add(jo.getString("DR_CODE"));
            listplandate.add(jo.getString("PLAN_DATE"));
            listffmgr.add(jo.getString("FF_MGR"));
            listffcode.add(jo.getString("FF_CODE"));
            listmon.add(jo.getString("MON"));
            listterrcode.add(jo.getString("TERR_CODE"));
            listmorn_even.add(jo.getString("MORN_EVEN"));


        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        Log.d("AsyncGetSpecialities", e.getMessage());

    }

    return tmp;
}

@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);
    pd.dismiss();
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
    pd.show();
}

}

1 个答案:

答案 0 :(得分:0)

这需要在onPreExecute:

pd = new ProgressDialog(context);
pd.setTitle("Please wait");
pd.setMessage("Loading...");
pd.setCancelable(false);
pd.show();

这需要在onPostExecute:

if(pd != null)
pd.dismiss();