API输出在文本视图中不显示
我试图制作一个非常简单的天气应用,以显示某个城市的温度。
但是,当我按下按钮以获取温度时,文本不会改变。
我的代码:
public class MainActivity extends AppCompatActivity {
TextView textView;
RequestQueue requestQueue;
Button button;
EditText etCityName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
button = findViewById(R.id.button);
etCityName = findViewById(R.id.etCityName);
requestQueue = Volley.newRequestQueue(this);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
jsonParse();
}
});
}
public void jsonParse() {
String url = "https://api.openweathermap.org/data/2.5/weather?q=" + etCityName.getText() + "&appid=token";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) { ;
try {
JSONArray jsonArray = response.getJSONArray("main");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject url = jsonArray.getJSONObject(i);
String temp = url.getString("temp");
textView.setText(temp);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
requestQueue.add(request);
}
}
I tried to make a very simple weather app that shows the temperature of a certain city.
But when I press the button to get the temperature, the text doesn't change.
Here is what happens if I try to get the temperature
My code:
public class MainActivity extends AppCompatActivity {
TextView textView;
RequestQueue requestQueue;
Button button;
EditText etCityName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
button = findViewById(R.id.button);
etCityName = findViewById(R.id.etCityName);
requestQueue = Volley.newRequestQueue(this);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
jsonParse();
}
});
}
public void jsonParse() {
String url = "https://api.openweathermap.org/data/2.5/weather?q=" + etCityName.getText() + "&appid=token";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) { ;
try {
JSONArray jsonArray = response.getJSONArray("main");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject url = jsonArray.getJSONObject(i);
String temp = url.getString("temp");
textView.setText(temp);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
requestQueue.add(request);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
etcityname.getText()。toString
而不是
etcityname.getText()
在您的jsonparse()方法中use
etCityName.getText().toString
instead of
etCityName.getText()
in your jsonparse() method