如何在 API 中将变量作为值传递。.我正在使用地理编码 API。但是我无法获得位置


double lat = 13.630754;
double longi = 123.18484;
JsonObjectRequest myRequest = new JsonObjectRequest(
Request.Method.POST,
// Here is where i used the variables for latitude and longitude
String.format(" https://maps.googleapis.com/maps/api/geocode/json?latlng=",lat,",",longi,"&key=my key"),
new JSONObject(jsonParams),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Toast.makeText(getApplicationContext(),response.toString(),Toast.LENGTH_SHORT).show();
//                              t.setText(response.toString());
String address = null;
try {
address = response.getJSONArray("results").getJSONObject(0).getString("formatted_address");
} catch (JSONException e) {
e.printStackTrace();
}
t.setText(address);
}
},

String.format(" https://maps.googleapis.com/maps/api/geocode/json?latlng=",lat,",",longi,"&key=my key")

没有任何意义。

使用字符串串联

"https://maps.googleapis.com/maps/api/geocode/json?latlng=" + lat + "," + longi + "&key=my key"

或正确的格式字符串

String.format("https://maps.googleapis.com/maps/api/geocode/json?latlng=%1$s,%2$s&key=my key", lat, longi )

请参阅文档:https://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html

相关内容

  • 没有找到相关文章

最新更新