[英]Why doesn't my API and JSON app work in Android Studio
I working on a weather channel app with API and JSON.我正在使用 API 和 JSON 开发天气频道应用程序。 I wrote all the code but it doesn't work, any help would be appreciated.
我写了所有的代码,但它不起作用,任何帮助将不胜感激。 The API for my app is from https://openweathermap.org/ and I ran it through Postman.
我的应用程序的 API 来自https://openweathermap.org/ ,我通过 Postman 运行它。
Here is my activity_main.xml :这是我的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>
Here is my MainActivity.java :这是我的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);
}
}
and here is my AndroidManifest.xml这是我的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>
If more information is needed just ask.如果需要更多信息,请询问。
I found the answer, my API url wasn't working so I changed it to https://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=48c0eb71736c9d90039d04e5c83581a0
我找到了答案,我的 API 网址不起作用,所以我将其更改为
https://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=48c0eb71736c9d90039d04e5c83581a0
although this is not the correct API, now I just know that my app works correctly, I just have to get the correct API for my location.虽然这不是正确的 API,但现在我只知道我的应用程序正常工作,我只需要为我的位置获取正确的 API。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.