【发布时间】:2020-12-11 18:53:24
【问题描述】:
它基本上是一个初学者级别的天气应用程序。这是我在应用程序主要活动中的代码。
每个库都是导入的。我在各个地方进行了检查。我已经完全按照他们所说的那样实现了,但是应用程序中似乎没有显示任何内容此外,还实现了依赖项,即 ---> implementation 'com.android.volley:volley: 1.1.1'。
package susinth.josh.weatherapp;
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 {
private TextView text_temp;
private TextView text_city;
private TextView text_description;
private TextView text_date;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text_temp=findViewById(R.id.txtTemp);
text_city=findViewById(R.id.txtCity);
text_description=findViewById(R.id.txtDesc);
text_date=findViewById(R.id.txtDate);
find_weather();
}
public void find_weather() {
String url="http://api.openweathermap.org/data/2.5/weather?q=London&appid=01138a7db759c13b7b0a1390ed862fff&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 weatherArray=response.getJSONArray("weather");
JSONObject object=weatherArray.getJSONObject(0);
String temp= String.valueOf(main_object.getDouble("temp"));
String description=object.getString("description");
String city=response.getString("name");
System.out.println(temp);
Calendar calendar=Calendar.getInstance();
SimpleDateFormat sdf=new SimpleDateFormat("EEEE-MM-dd");
String formattedDate=sdf.format(calendar.getTime());
text_date.append(formattedDate);
text_city.setText(city);
text_description.setText(description);
text_temp.setText(temp);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
RequestQueue queue= Volley.newRequestQueue(this);
queue.add(jor);
}
}
我的 json 数据
{
"coord": {
"lon": -0.13,
"lat": 51.51
},
"weather": [
{
"id": 300,
"main": "Drizzle",
"description": "light intensity drizzle",
"icon": "09d"
}
],
"base": "stations",
"main": {
"temp": 280.32,
"pressure": 1012,
"humidity": 81,
"temp_min": 279.15,
"temp_max": 281.15
},
"visibility": 10000,
"wind": {
"speed": 4.1,
"deg": 80
},
"clouds": {
"all": 90
},
"dt": 1485789600,
"sys": {
"type": 1,
"id": 5091,
"message": 0.0103,
"country": "GB",
"sunrise": 1485762037,
"sunset": 1485794875
},
"id": 2643743,
"name": "London",
"cod": 200
}
【问题讨论】:
标签: java json android-studio android-volley