我只想用 httpconnection 提取数据并用 ResponseHandler 和一些操作操作获取值。然后我只想把它们放到listview中。但我做不到。
首先,我在一个线程中取值。然后我在主线程上进行操作并向适配器添加值,但是,我要么接受异常,要么接受空白列表视图。
我的示例代码如下;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_x);
m_results = new ArrayList<Type>();
/*
rs.setDate("22.09.2012");
rs.setOne("5");
rs.setTwo("12");
rs.setThree("18");
rs.setFour("21");
rs.setFive("44");
rs.setSix("48");
*/
this.m_adapter = new Adapter(this, R.layout.resultrow, m_results);
setListAdapter(this.m_adapter);
sendNumbers = new Runnable() {
@Override
public void run() {
httpclnt = new DefaultHttpClient();
httppst = new HttpPost("url");
try {
ResponseHandler<String> responseHandler = new BasicResponseHandler();
response = httpclnt.execute(httppst, responseHandler);
message = response;
handler.sendEmptyMessage(0);
}catch (ClientProtocolException e) {
Toast.makeText(getApplicationContext(), "Client protocol exception ", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "IO exception "+e.getMessage(), Toast.LENGTH_LONG).show();
}
}
};
Thread thread = new Thread(sendNumbers,"sendNumbers");
thread.start();
handler = new Handler() {
public void handleMessage(Message msg) {
firstResult = message.substring(0,message.indexOf(")"));
firstDate = firstResult.substring(firstResult.indexOf(">")+2,firstResult.indexOf(">")+12);
firstNumbers=firstResult.substring(firstResult.lastIndexOf(">")+2,firstResult.lastIndexOf(">")+19);
rs.setDate(firstDate.substring(firstDate.lastIndexOf("-")+1,firstDate.lastIndexOf("-")+3)+"."
+firstDate.substring(firstDate.indexOf("-")+1,firstDate.indexOf("-")+3)+"."+firstDate.substring(0,4));
m_results.add(rs);
m_adapter.notifyDataSetChanged();
}
};