我只想用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();
}
};
答案 0 :(得分:0)
而不是Thread使用异步任务,
public class Get_User_Data extends AsyncTask<Void, Void, Void> {
private final ProgressDialog dialog = new ProgressDialog(
MyActivity.this);
protected void onPreExecute() {
this.dialog.setMessage("Loading...");
this.dialog.setCancelable(false);
this.dialog.show();
}
@Override
protected Void doInBackground(Void... params) {
uploadImage(); // inside the method paste your file uploading code
return null;
}
protected void onPostExecute(Void result) {
// Here if you wish to do future process for ex. move to another activity do here
if (dialog.isShowing()) {
dialog.dismiss();
}
}
}
有关详细信息,请参阅此链接http://vikaskanani.wordpress.com/2011/01/29/android-image-upload-activity/
答案 1 :(得分:0)
在您的代码中使用它作为参考,..
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML from URL
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_SONG);
// looping through all song nodes <song>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
// adding HashList to ArrayList
songsList.add(map);
它可以帮助你!..并尝试这个链接...
http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/
答案 2 :(得分:0)
@Aerrow上的解决方案很好。如果您不想在代码中进行太多更改,请执行
更换
Thread thread = new Thread(sendNumbers,"sendNumbers");
与HandlerThread thread = new HandlerThread("sendNumbers",sendNumbers);