java - 为什么我的 API 和 JSON 应用无法在 Android Studio 中运行
问题描述
我正在使用 API 和 JSON 开发天气频道应用程序。我编写了所有代码,但它不起作用,任何帮助将不胜感激。我的应用程序的 API 来自https://openweathermap.org/,我通过 Postman 运行它。
这是我的activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/t1_temp"
android:layout_width="124dp"
android:layout_height="133dp"
android:text="30"
android:textSize="100dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.055"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.17" />
<TextView
android:id="@+id/t2_city"
android:layout_width="130dp"
android:layout_height="25dp"
android:text="My City"
android:textSize="15dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.069" />
<TextView
android:id="@+id/t3_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sunny"
android:textSize="15dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.043"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.33" />
<TextView
android:id="@+id/t4_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date"
android:textSize="15dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.041"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.075" />
<TextView
android:id="@+id/t5_C"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C"
android:textSize="30dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.349"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.143" />
</androidx.constraintlayout.widget.ConstraintLayout>
这是我的MainActivity.java:
package com.example.weatherchannel;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
TextView t1_temp,t2_city,t3_description,t4_date;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1_temp = findViewById(R.id.t1_temp);
t2_city = findViewById(R.id.t2_city);
t3_description = findViewById(R.id.t3_description);
t4_date = findViewById(R.id.t4_date);
find_weather();
}
public void find_weather() {
String url = "http://api.openweathermap.org/data/2.5/weather?q=kirkkonummi,finland&appid=48c0eb71736c9d90039d04e5c83581a0&units=metric";
JsonObjectRequest jor = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject main_object = response.getJSONObject("main");
JSONArray array = response.getJSONArray("weather");
JSONObject object = array.getJSONObject(0);
String temp = String.valueOf(main_object.getDouble("temp"));
String description = object.getString("description");
String city = response.getString("name");
//t1_temp.setText(temp);
t2_city.setText(city);
t3_description.setText(description);
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("EEEE=MM-dd");
String formatted_date = sdf.format(calendar.getTime());
t4_date.setText(formatted_date);
double temp_int = Double.parseDouble(temp);
double centi = (temp_int - 32) / 1.8000;
centi = Math.round(centi);
int i = (int) centi;
t1_temp.setText(String.valueOf(i));
}catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}
);
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(jor);
}
}
这是我的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.weatherchannel">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
如果需要更多信息,请询问。
解决方案
我找到了答案,我的 API url 不起作用,所以我将其更改为
https://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=48c0eb71736c9d90039d04e5c83581a0
虽然这不是正确的 API,但现在我只知道我的应用程序可以正常工作,我只需要为我的位置获取正确的 API。
推荐阅读
- networking - Openstack:如何给管理网络Internet?
- javascript - 如何从 React 中的项目列表中找到总价?
- ios - 过渡到特定场景时,Unity 应用程序在 iOS14 上崩溃
- android - 使用 NavController 时,如何将数据从目标 Fragment 的 FloatingActionButton 的 OnClickListener 传递到起始 Fragment?
- c++ - C++ 返回值存储在堆栈帧中
- laravel - Laravel 多态多对多与 Pivot - 同步
- python - 遍历数据框中的列并在同一张图上绘图(Python)
- reactjs - 如何解决 react create app 'sh' 不是有效命令?
- reactjs - 如何使用 useState 挂钩更新对象状态
- python - Flask-login 在带有 @login_required 的页面上引发 401 状态