我正在将数据发送到我的".php"脚本文件,该文件将该数据存储到我的数据库中。
我为此将Map<String, String>
与getParams()
一起使用。
但是,我对 Java 很陌生,不知道如何从 Params 返回Float
。
我需要在这里更改什么才能返回Floats
?
我不断收到必须<String, String>
Map
的错误.
我尝试使用各种语句更正 Map 和 getParams,但这不起作用。我也尝试删除修复了任何错误,但应用程序只是崩溃。
protected Map<String, String> getParams() throws AuthFailureError {
//Here is what I think i need to change
Map <String, String>params = new HashMap();
params.put("tire", tire);
params.put("tire2", tire2);
// tire and tire 2 are floats
return params;
}
我希望返回params
将第一个选项作为String
返回,将第二个选项作为Float
返回。
地图的值类型是 String
,因此您需要将值转换/格式化为该值。你可以只使用Float.toString(float)
:
params.put("tire", FLoat.toString(tire));
或者,也许您想用特定的小数位数来格式化您的数字(此处四舍五入到两点(:
params.put("tire", String.format("%.2f", tire));
您需要更改方法签名才能返回其他对象/类型。所以把Map<String, String>
改成Map<String, Float>
.
protected Map<String, Float> getParams() throws AuthFailureError {
Map <String, Float>params = new HashMap<>();
params.put("tire", 1.0f);
params.put("tire2", 2.0f);
return params;
}