xml HttpGet出错

时间:2013-03-22 12:11:27

标签: java android

我有一个应用程序,它使用了一个用HttpGet获得的xml。在xml请求的url中有一个每小时更改一次的密钥,这个密钥是通过另一个url获得的。

示例:www.test.com/xml?hr = 123456

该应用尝试获取xml。如果发生错误,请在另一个HttpGet中获取密钥并在xml URL中进行更新。

在模拟器中运行良好,但不在设备上。已经存在跟踪并且密钥已成功更新。但是xml请求错误仍在继续。

我试图在HttpGet中加入“no cache”

HttpClient client = new DefaultHttpClient (httpParams);
HttpGet get = new HttpGet (url);
get.addHeader ("Cache-Control", "no-cache"); / / HTTP/1.1
get.addHeader ("Expires", "Sat, 26 Jul 1997 05:00:00 GMT");

但它不起作用,有什么可以做的吗?

我的完整活动:

public class ShowActivity extends AdModctivity implements
  bbbAndroidConstantes, OnItemClickListener, OnCheckedChangeListener {
  private ProgressDialog dialog;
  private Retf p;
  private Handler handler = new Handler();
  private Exception ex;
  private CheckBox check;
  private ParametrosTO parTO;
  ParametrosDataBase parDB;
  String ref;
  String pId;
  String logR;
  String kId;
  String ldesc;
  TextView txtRef;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listaprevisoeslayout);
    Bundle bundle = getIntent().getExtras();

    parDB = new ParametrosDataBase(this);
    parTO = parDB.listParametros();

    pId = bundle.getString(VAR_P_ID);
    ref = bundle.getString(VAR_P_REF);
    logR = bundle.getString(VAR_LOGR);
    kId = bundle.getString(VAR_KID);
    ldesc = bundle.getString(VAR_LDESC);
    txtRef = (TextView) findViewById(R.id.txtRef);
    check = (CheckBox) findViewById(R.id.checkWidget);

    WidgetDataBase wDb = new WidgetDataBase(this);
    if (wDb.searchWidget(pId, kId) != null)
        check.setChecked(true);

    check.setOnCheckedChangeListener(this);

    dialog = ProgressDialog.show(ShowActivity.this, "",
            "Wait...", true);

    if (!Utilities.chkStatus(ShowActivity.this))
        return;
    downloadInfo();
    fillAdMod();
}

@Override
public void onItemClick(AdapterView parent, View v, int position, long id) {

}

private void downloadInfo() {
    new Thread() {
        @Override
        public void run() {
            boolean flag = false;
            try {
                p = Controller.getInstance().getRetf(parTO.getUrl(), pId, kId);
                if ((p.getInfoP() == null) || (p.getInfoP().size() == 0)) {
                    flag = true;
                    String newUrl = getNewUrl();
                    Utilities.updateUrl(ShowActivity.this, newUrl);
                    parTO = parDB.listParametros();
                    p = Controller.getInstance().getRetf(newUrl, pId, kId);
                }
                refreshScreen();
            } catch (Exception e) {
                if (!flag) {
                    try {
                        String newUrl = getNewUrl();
                        Utilities.updateUrl(ShowActivity.this, newUrl);
                        parTO = parDB.listParametros();
                        p = Controller.getInstance().getRetf(newUrl, pId, kId);
                        refreshScreen();
                    } catch (Exception e1) {
                        ex = e1;
                        refreshScreenException();
                    }
                }
                else
                {
                    ex = e;
                    refreshScreenException();
                }
            }
        }
    }.start();
}

/**
 * Thread to close dialog and show information
 */
private void refreshScreen() {
    handler.post(new Runnable() {
        @Override
        public void run() {
            dialog.dismiss();
            fillInfo();
        }
    });
}

/**
 * Thread to close dialog and show exception
 */
private void refreshScreenException() {
    handler.post(new Runnable() {
        @Override
        public void run() {
            dialog.dismiss();
            fillInfoExcecao();
        }
    });
}

/**
 * Fill information on screen
 */
private void fillInfo() {

    if (p == null) {
        Toast.makeText(ShowActivity.this, MSG_SEM_Retf,
                MSG_TIME_MILIS).show();
        this.finish();
    }

    if ((p.getErro() != null) && (p.getErro().trim().length() > 0)) {
        Toast.makeText(ShowActivity.this, p.getErro(),
                MSG_TIME_MILIS).show();
        this.finish();
    }

    if ((p.getPonto() == null) || (p.getPonto().size() == 0)) {
        Toast.makeText(ShowActivity.this, MSG_SEM_Retf,
                MSG_TIME_MILIS).show();
        this.finish();
    }

    SimpleDateFormat spf = new SimpleDateFormat("HH:mm:ss");
    long localTime = Long.parseLong(p.getLocalTime());
    txtRef.setText(txtRef.getText() + " "
            + spf.format(new Date(localTime)));

    RetfAdapter RetfAdapter = new RetfAdapter(this, p);
    ListView lista = (ListView) findViewById(R.id.listView1);
    lista.setAdapter(RetfAdapter);
    lista.setOnItemClickListener(this);
}

/**
 * Apresenta mensagem em caso de exceo
 */
private void fillInfoExcecao() {
    if (ex instanceof bbbException) {
        Toast.makeText(ShowActivity.this,
                ((bbbException) ex).getMessage(), MSG_TIME_MILIS).show();
        ShowActivity.this.finish();
    } else {
        Toast.makeText(ShowActivity.this,
                getString(R.string.msg_erroSistema), MSG_TIME_MILIS).show();
        ShowActivity.this.finish();
    }
}

@Override
public void onCheckedChanged(CompoundButton cb, boolean isChecked) {
    PBean PBean = new PBean(pId, logR,
            ref, kId, ldesc);

    WidgetDataBase wDb = new WidgetDataBase(this);
    if (isChecked)
        wDb.addWidget(PBean);
    else
        wDb.deleteWidget(PBean);
}

private String getNewUrl() throws Exception
{
    String newUrl = Utilities.getHttp(bbbAndroidConstantes.URL_HOST_HORARIOS);
    return newUrl;
}

}

0 个答案:

没有答案