textview没有显示我需要的值。我正在尝试在其中显示天气预报。但是,当我尝试这样做时,该应用程序根本不显示任何内容。
<TextView
android:id="@+id/degree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Loading..."
android:textSize="60sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
下来是我的主要活动java类
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = findViewById(R.id.degree);
WeatherTask task = new WeatherTask(tv);
task.execute("moscow");
}
}
这是获取选定城市的天气预报的主要Java类
public class WeatherTask extends AsyncTask<String, Void, String> {
private static final String API_KEY = "06c7d6512a914b3029937dd444283ff0";
private final TextView textView;
public WeatherTask(TextView tv){
this.textView = tv;
}
@Override
protected String doInBackground(String... cities) {
String weather = "";
try {
String urlString = String.format(
"http://api.openweathermap.org/data/2.5/weather?q=%s,uz&units=%s&appid=%s",
cities[0], "metric", API_KEY
);
URL url = new URL(urlString);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
InputStream stream = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
StringBuilder builder = new StringBuilder();
String inputString;
while ((inputString = bufferedReader.readLine()) != null) {
builder.append(inputString);
}
String response = builder.toString();
JSONObject topLevel = new JSONObject(response);
JSONObject main = topLevel.getJSONObject("main");
weather = String.valueOf(main.getDouble("temp"));
urlConnection.disconnect();
} catch (IOException | JSONException e) {
e.printStackTrace();
}
return weather;
}
@Override
protected void onPostExecute(String weather)
{
this.textView.setText(weather);
}
}
有人可以解释为什么在主活动中不显示textview吗?
谢谢
答案 0 :(得分:0)
我尝试了您的URL,它返回的JSON响应具有不同的结构-表示未知城市。也许您想从查询中删除,uz
:
"http://api.openweathermap.org/data/2.5/weather?q=%s&units=%s&appid=%s"
您还应该检查日志,e.printStacktrace
会提供明确的错误消息。我还检查了uz
是乌兹别克斯坦的国家/地区代码,但我确定莫斯科在俄罗斯,所以也许这是失败的原因,并解释了为什么您的TextView
没有收到任何东西。
答案 1 :(得分:0)
A.A A.B A.C B.A B.B B.C C.A C.B C.C
1 1 0.38056317 0.4701251 2.6276847 1 1.2353406 2.1270933 0.8094934 1
2 1 -1.07851585 -1.0793270 -0.9272001 1 1.0007521 -0.9265032 0.9992484 1
3 1 -2.44512434 -4.7467554 -0.4089772 1 1.9413145 -0.2106702 0.5151149 1
4 1 -1.41765991 -2.3908820 -0.7053878 1 1.6864990 -0.4182557 0.5929443 1
5 1 -0.10640354 0.2382367 -9.3981837 1 -2.2389920 4.1975066 -0.4466296 1
6 1 2.32474492 0.3879095 0.4301547 1 0.1668611 2.5779212 5.9930092 1
7 1 0.95122898 1.6102188 1.0512716 1 1.6927773 0.6210336 0.5907452 1
8 1 -0.15996970 -0.1422422 -6.2511840 1 0.8891823 -7.0302611 1.1246287 1
9 1 -0.04431519 0.2121086 -22.5656275 1 -4.7863640 4.7145657 -0.2089269 1
10 1 0.26662240 -0.3524232 3.7506226 1 -1.3218065 -2.8374974 -0.7565404 1
应为:
http://api.openweathermap.org/data/2.5/weather?q=%s,uz&units=%s&appid=%s"
如果您使用第一个请求,应该有以下响应:
http://api.openweathermap.org/data/2.5/weather?q=%s&units=%s&appid=%s"
因为它将{
"cod": "404",
"message": "city not found"
}
和'cities[0]'
连接为第一个参数。