I want to fetch two fields of data from the api.Namely:"description" and "temp".Than set the textView res to display thoes two parameters for a city the user enters.But when i hit the Button that is supposed to trigger the jsoup object nothing happens in the app.The textView is not visible.So i dont know if the execution even goes inside the jsonObjectRequest.(BTW: I have added internet permission in the manifest file)
MainAcitivity class:
Button button;
EditText city;
TextView res, debug;
String baseURL = "http://api.openweathermap.org/data/2.5/weather?q=";
String API = "&appid=some_key";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button_go);
city = findViewById(R.id.editText_city);
res = findViewById(R.id.result);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (city.getText().toString() == null) {
city.setError("Enter city");
return;
} else {
String myURL = baseURL + city.getText().toString() + API;
//Log.i("URL","URL : "+myURL);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, myURL, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
String weather_main = "",id = "";
String main_info = response.getString("weather");
String temp = response.getString("main");
JSONArray jsonArray = new JSONArray(main_info);
JSONObject jsonObject = new JSONObject(temp);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject weatherObj = jsonArray.getJSONObject(i);
//Log.i("ID", "ID:" + weatherObj.getString("id"));
//id = weatherObj.getString("id");
weather_main = weatherObj.getString("description");
}
Log.i("Desc", "Description: " + weather_main);
Double tempObj = jsonObject.getDouble("temp");
res.setText("Weather: " + weather_main+"Temp: "+tempObj);//THIS DOSENT WORK "res" IS INVISIBLE
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
VolleySingleton.getInstance(MainActivity.this).addToqueue(jsonObjectRequest);
}
}
});
}
VolleySingleton class:
public VolleySingleton(Context context) {
mcX = context;
mRequestQueue = getRequestQueue();
//mRequestQueue = Volley.newRequestQueue(context.getApplicationContext());
}
public RequestQueue getRequestQueue(){
if(mRequestQueue == null){
mRequestQueue = Volley.newRequestQueue(mcX.getApplicationContext());
}
return mRequestQueue;
}
public static synchronized VolleySingleton getInstance(Context context){
if (mInstance == null){
mInstance = new VolleySingleton(context);
}
return mInstance;
}
public void addToqueue(Request req){
mRequestQueue.add(req);
}
Json:
{
"coord": {
"lon": -0.13,
"lat": 51.51
},
"weather": [
{
"id": 721,enter code here
"main": "Haze",
"description": "haze",
"icon": "50d"
}
],
"base": "stations",
"main": {
"temp": 285.18,
"feels_like": 282.69,
"temp_min": 284.82,
"temp_max": 285.37,
"pressure": 1011,
"humidity": 87
},
"visibility": 2900,
"wind": {
"speed": 3.6,
"deg": 170
},
"clouds": {
"all": 75
},
"dt": 1606298043,
"sys": {
"type": 1,
"id": 1414,
"country": "GB",
"sunrise": 1606289744,
"sunset": 1606319994
},
"timezone": 0,
"id": 2643743,
"name": "London",
"cod": 200
}