[英]Why doesn't my API and JSON app work in 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 網址不起作用,所以我將其更改為https://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=48c0eb71736c9d90039d04e5c83581a0
雖然這不是正確的 API,但現在我只知道我的應用程序正常工作,我只需要為我的位置獲取正確的 API。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.