it is basically a beginner-level weather app. this is my code in the main activity of the app.
every library is imported.i checked in various places .i have implemented exactly as they said but still, nothing seems to be displaying in the app Also, dependency was implemented which was ---> 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);
}
}
my json data
{
"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
}