如何使用用户输入更改 API 调用
How alter an API call with user input
我正在尝试更改以下代码,以便更改以下代码中的城市和测量单位(即华氏度和摄氏度)。我希望用户能够在 EditText 字段中键入一个城市名称,点击一个按钮,它会将天气位置更改为该城市。我还希望将首选项设置为以摄氏度或华氏度显示温度。我不确定该怎么做。以下是我当前的代码。字符串 url 是 API 我需要根据输入进行更改。
有人有什么想法吗?
String url = "http://api.openweathermap.org/data/2.5/weather?q=Indianapolis&units=imperial&appid=OpenWeatherMapAPIKey";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject responseObject) {
//tempTextView.setText("Response: " + response.toString());
Log.v("Weather", "Response: " + responseObject.toString());
try {
JSONObject mainJSONObject = `enter code here`responseObject.getJSONObject("main");
JSONArray weatherArray = responseObject.getJSONArray("weather");
JSONObject firstWeatherObject = weatherArray.getJSONObject(0);
String temp = Integer.toString((int) Math.round(mainJSONObject.getDouble("temp")));
String weatherDescription = firstWeatherObject.getString("description");
String city = responseObject.getString("name");
tempTextView.setText(temp);
weatherDescTextView.setText(weatherDescription);
cityTextView.setText(city);
int iconResourceId = getResources().getIdentifier("icon_" + weatherDescription.replace(" ", ""), "drawable", getPackageName());
weatherImageView.setImageResource(iconResourceId);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO: Handle error
}
});
// Access the RequestQueue through your singleton class.
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(jsonObjectRequest);
}
比如说,你有两个 EditText
,一个有城市名称,id 设置为 etCity
。
另一个将 id 设置为 etMetric
,具有选择的单位。
如果 etMetric
的值设置为 imperial
,则将使用 英制 单位,否则 公制单位。
现在您有一个用于发送请求的按钮。单击按钮时,将调用以下函数:
private void weatherRequest(){
EditText etCity,etMetric;
etCity= findViewById(R.id.etCity);
etMetric= findViewById(R.id.etMetric);
String city= etCity.getText().toString();
String unit= etCity.getText().toString();
if(!unit.equals("imperial"))
unit="metric";
String url = "http://api.openweathermap.org/data/2.5/weather?q="+etCity+"&units="+etMetric+"&appid=OpenWeatherMapAPIKey";
//now request the server with this url
}
我正在尝试更改以下代码,以便更改以下代码中的城市和测量单位(即华氏度和摄氏度)。我希望用户能够在 EditText 字段中键入一个城市名称,点击一个按钮,它会将天气位置更改为该城市。我还希望将首选项设置为以摄氏度或华氏度显示温度。我不确定该怎么做。以下是我当前的代码。字符串 url 是 API 我需要根据输入进行更改。
有人有什么想法吗?
String url = "http://api.openweathermap.org/data/2.5/weather?q=Indianapolis&units=imperial&appid=OpenWeatherMapAPIKey";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject responseObject) {
//tempTextView.setText("Response: " + response.toString());
Log.v("Weather", "Response: " + responseObject.toString());
try {
JSONObject mainJSONObject = `enter code here`responseObject.getJSONObject("main");
JSONArray weatherArray = responseObject.getJSONArray("weather");
JSONObject firstWeatherObject = weatherArray.getJSONObject(0);
String temp = Integer.toString((int) Math.round(mainJSONObject.getDouble("temp")));
String weatherDescription = firstWeatherObject.getString("description");
String city = responseObject.getString("name");
tempTextView.setText(temp);
weatherDescTextView.setText(weatherDescription);
cityTextView.setText(city);
int iconResourceId = getResources().getIdentifier("icon_" + weatherDescription.replace(" ", ""), "drawable", getPackageName());
weatherImageView.setImageResource(iconResourceId);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO: Handle error
}
});
// Access the RequestQueue through your singleton class.
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(jsonObjectRequest);
}
比如说,你有两个 EditText
,一个有城市名称,id 设置为 etCity
。
另一个将 id 设置为 etMetric
,具有选择的单位。
如果 etMetric
的值设置为 imperial
,则将使用 英制 单位,否则 公制单位。
现在您有一个用于发送请求的按钮。单击按钮时,将调用以下函数:
private void weatherRequest(){
EditText etCity,etMetric;
etCity= findViewById(R.id.etCity);
etMetric= findViewById(R.id.etMetric);
String city= etCity.getText().toString();
String unit= etCity.getText().toString();
if(!unit.equals("imperial"))
unit="metric";
String url = "http://api.openweathermap.org/data/2.5/weather?q="+etCity+"&units="+etMetric+"&appid=OpenWeatherMapAPIKey";
//now request the server with this url
}