我目前在android上做一个项目,当我测试函数doInBackground,并将值返回到字符串时,程序执行成功。
但是,在我的项目中,我想从服务器获取多个东西。所以我想用HashMap来帮助我实现这个目标。但它会引发错误。谁能告诉我为什么?另外,当我将String转换为HashMap时,onPostExecute方法的覆盖也有错误[Method不会覆盖其超类中的方法。
有人知道发生了什么吗? 我的代码如下所示。
class retrieveData extends AsyncTask<String,String,String> {
@Override
protected void onPreExecute(){
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Please Wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected HashMap<String, String> doInBackground(String... args){
// new retrieveData().execute();
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
jsonObject = jsonParser.getJSONFromUrl(REG_URL, params);
try {
a = jsonObject.getString("key");
b = jsonObject.getString("name");
c = jsonObject.getString("gender");
// creating new HashMap
map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put("key", a);
map.put("name", b);
map.put("gender", c);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return map;
}
// SuperClass does not have HashMap imposed...
@Override
protected void onPostExecute(HashMap<String, String> map){
pDialog.dismiss();
String aReceived = map.get("key");
String bReceived = map.get("name");
String cReceived = map.get("gender");
tv.setText(aReceived + '\n' + bReceived + '\n' + cReceived);
}
}
答案 0 :(得分:2)
就像,
一样简单 class A extends AsyncTask<String, String, HashMap<String, String>>
{
@Override
protected void onPreExecute()
{
super.onPreExecute();
// TODO Auto-generated method stub
}
@Override
protected HashMap<String, String> doInBackground(String... params)
{
// TODO Auto-generated method stub
return null;
}
@Override
protected void onPostExecute(HashMap<String, String> result)
{
super.onPostExecute(result);
// TODO Auto-generated method stub
}
}
答案 1 :(得分:1)
试试这个让我知道
class Loaddata extends AsyncTask<HashMap, HashMap, HashMap> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
protected HashMap<String, String> doInBackground(HashMap... args) {
// Building Parameters
HashMap<String, String> map = new HashMap<String, String>();
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
jsonObject = jsonParser.getJSONFromUrl(REG_URL, params);
try {
a = jsonObject.getString("key");
b = jsonObject.getString("name");
c = jsonObject.getString("gender");
// creating new HashMap
map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put("key", a);
map.put("name", b);
map.put("gender", c);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (Exception e) {
}
return map;
}
@Override
protected void onPostExecute(HashMap map){
pDialog.dismiss();
String aReceived = map.get("key");
String bReceived = map.get("name");
String cReceived = map.get("gender");
tv.setText(aReceived + '\n' + bReceived + '\n' + cReceived);
}
}