Java 如何将多个数据类型传递给HashMap
我正在尝试将字符串和int数据(可能是其他数据类型,如时间)传递给HashMap,以便在Android中的doinbackground任务中用于修改URL。URL使用键值对更新mysql数据库 我读过关于使用对象传递多个变量类型的内容,但无法使其工作Java 如何将多个数据类型传递给HashMap,java,hashmap,Java,Hashmap,我正在尝试将字符串和int数据(可能是其他数据类型,如时间)传递给HashMap,以便在Android中的doinbackground任务中用于修改URL。URL使用键值对更新mysql数据库 我读过关于使用对象传递多个变量类型的内容,但无法使其工作 private void addChore(){ final String title2 = editTextTaskTitle.getText().toString().trim(); final String descript
private void addChore(){
final String title2 = editTextTaskTitle.getText().toString().trim();
final String description2 = editTextDescription.getText().toString().trim();
final String person2 = itemPerson.toString().trim();
final int monday2 = cbMon;
class NewChore1 {
String title1;
String description1;
String person1;
int monday1;
NewChore1(String title1, String description1, String person1, int monday1){
this.title1 = title1;
this.description1 = description1;
this.person1 = person1;
this.monday1 = monday1;
}
}
class AddChoreM extends AsyncTask<Void,Void,String> {
ProgressDialog loading;
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(AddChore.this,"Adding...","Wait...",false,false);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
Toast.makeText(AddChore.this,s,Toast.LENGTH_LONG).show();
}
@Override
protected String doInBackground(Void... v) {
HashMap<String, NewChore1> params1 = new HashMap<>();
params1.put(Config.KEY_CHORE_TASK_TITLE,?);
params1.put(Config.KEY_CHORE_DESCRIPTION,?);
params1.put(Config.KEY_CHORE_PERSON,?);
params1.put(Config.KEY_CHORE_MONDAY,?);
RequestHandler rh = new RequestHandler();
String res = rh.sendPostRequest(Config.URL_ADD, params1);
return res;
}
}
NewChore1 params = new NewChore1(title2, description2, person2, monday2);
AddChoreM addChoreM = new AddChoreM();
addChoreM.execute(params);
}
private void addChore(){
最后一个字符串title2=editTextTaskTitle.getText().toString().trim();
final String description2=editTextDescription.getText().toString().trim();
最终字符串person2=itemPerson.toString().trim();
最终int星期一2=cbMon;
第1类{
字符串标题1;
字符串描述1;
弦乐人1;
国际星期一1;
NewChore1(字符串标题1,字符串描述1,字符串人物1,int monday1){
this.title1=title1;
this.description1=description1;
this.person1=person1;
this.monday1=monday1;
}
}
类AddChoreM扩展了AsyncTask{
对话加载;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
loading=ProgressDialog.show(AddChore.this,“Adding…”,“Wait…”,false,false);
}
@凌驾
受保护的void onPostExecute(字符串s){
super.onPostExecute(s);
loading.dispose();
Toast.makeText(AddChore.this,s,Toast.LENGTH_LONG.show();
}
@凌驾
受保护字符串背景(无效…v){
HashMap params1=新的HashMap();
参数1.put(配置键\u杂务\u任务\u标题,?);
参数1.put(配置键\u杂务\u说明,?);
参数1.put(配置键\u杂务\u人员,?);
参数1.put(配置键“杂务”星期一,?);
RequestHandler rh=新的RequestHandler();
String res=rh.sendPostRequest(Config.URL\u ADD,params1);
返回res;
}
}
NewChore1参数=newnewchore1(标题2,描述2,人物2,星期一2);
AddChoreM AddChoreM=新的AddChoreM();
addChoreM.execute(参数);
}
在RequestHandler中,我使用了以下内容
private String getPostDataString(HashMap<String, Object> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for (Map.Entry<String, Object> entry : params.entrySet()) {
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
return result.toString();
}
private String getPostDataString(HashMap参数)引发UnsupportedEncodingException{
StringBuilder结果=新建StringBuilder();
布尔值优先=真;
对于(Map.Entry:params.entrySet()){
如果(第一)
第一个=假;
其他的
结果。追加(&);
append(URLEncoder.encode(entry.getKey(),“UTF-8”);
结果。追加(“=”);
append(URLEncoder.encode(entry.getValue(),“UTF-8”);
}
返回result.toString();
}
为AddChoreM
类创建一个构造函数,并通过它设置NewChore1
对象。现在,您可以在doInBackground
中轻松提取NewChore1
的属性
class AddChoreM extends AsyncTask<Void,Void,String> {
ProgressDialog loading;
NewChore1 newChore1Obj;
public AddChoreM(NewChore1 newChore1Obj){
this.newChore1Obj = newChore1Obj;
}
@Override
protected String doInBackground(Void...v) {
HashMap<String, NewChore1> params1 = new HashMap<>();
String res = "";
if(newChore1Obj != null) {
params1.put(Config.KEY_CHORE_TASK_TITLE, newChore1Obj.title1);
params1.put(Config.KEY_CHORE_DESCRIPTION, newChore1Obj.description1);
params1.put(Config.KEY_CHORE_PERSON,newChore1Obj.person1);
params1.put(Config.KEY_CHORE_MONDAY,newChore1Obj.monday1);
RequestHandler rh = new RequestHandler();
res = rh.sendPostRequest(Config.URL_ADD, params1);
}
return res;
}
// Other methods of AsyncTask
//
}
如果你使用
Map<String, Object> params1 = new HashMap<>();
Map params1=newhashmap();
然后,您可以将任何类型存储为地图中的值。编辑:我不够快,因此已经有其他答案,但下面的更改应该可以使用。您可以将NewChore1对象传递给任务,并提取doInBackground中的参数:
class AddChoreM extends AsyncTask<NewChore1,Void,String> {
更新:
由于sendPostRequest只接受HashMap
,因此需要更改为:HashMap params1=new HashMap()代码>
并将NewChore1类更改为仅接受字符串。我得到“错误的第二个参数类型。找到字符串,必需的NewChore1..对于从外观上传递给params1的每个项,我认为您希望HashMap params1=new HashMap();
为HashMap params1=new HashMap();
。你能再检查一遍吗?谢谢,是的,我将NewChore1改为使用Object,这解决了将项目放入HashMap的问题,但在使用rh.sendPostRequest(Config.URL\u ADD,params1)时,我仍然在params1上遇到错误,以及在返回res时,我想这是有意义的更新:我将RequestHandler中的HashMap数据类型更改为Object,这也对params1上的数据不兼容错误进行了排序。我现在得到的唯一错误是在返回res时,表示Expression expectedthanks@fluffyBatman for code,Raffe81建议使用哪个已解决的第二个参数t将数据对放入HashMap时出现ype错误。在使用rh.sendPostRequest(Config.URL_ADD,params1)时,字符串res(params1)上仍然出现第二个参数错误。可能是sendPostRequest只接受HashMap。在这种情况下,您必须将int更改为String。作为快速测试,您只需执行以下操作:HashMap params1=new HashMap();和:Integer.toString(chore[0].monday1)您是对的。我已经更新了它(以及上面的代码),得到的唯一错误是“Expression expected”“返回doInBackground中的res时。。。我在@fluffyBatman上发布了相同的消息,所以我会继续在那里发表评论,以避免重复,因为我不知道你在使用哪种代码。但是,如果您将fluffyBatmans代码与“if(newChore1Obj!=null){”表达式一起使用,那么res仅在if内创建。请尝试将“String res”放在if之前。然后在if内just res=rh.sendPostRequest(Config.URL\u ADD,params1);此外,由于上述方法有效,您不应使用“Integer.toString”,这只是出于测试目的。相反,将您的NewChore1类更改为采用所有StringsYip,这就成功了。谢谢!已经为此工作了一段时间了
class AddChoreM extends AsyncTask<NewChore1,Void,String> {
@Override
protected String doInBackground(NewChore1...chore) {
HashMap<String, String> params1 = new HashMap<>();
params1.put(Config.KEY_CHORE_TASK_TITLE, chore[0].title1);
params1.put(Config.KEY_CHORE_DESCRIPTION, chore[0].description1);
params1.put(Config.KEY_CHORE_PERSON,chore[0].person1);
params1.put(Config.KEY_CHORE_MONDAY,chore[0].monday1);
RequestHandler rh = new RequestHandler();
String res = rh.sendPostRequest(Config.URL_ADD, params1);
return res;
}
NewChore1 params = new NewChore1(title2, description2, person2, monday2);
new addChoreM.execute(params);