Skip to content

Instantly share code, notes, and snippets.

@alexreyes
Created January 13, 2016 00:33
Show Gist options
  • Save alexreyes/826b0dc06c01b7cad0d8 to your computer and use it in GitHub Desktop.
Save alexreyes/826b0dc06c01b7cad0d8 to your computer and use it in GitHub Desktop.
public class MainActivity extends AppCompatActivity {
TextView text = (TextView) findViewById(R.id.text);
private void loadWeather(String zipCode) {
String apiUrl = "http://api.openweathermap.org/data/2.5/forecast/city?id=" + zipCode + "&APPID=780f5669dd2bbeb78315e948dbf8d99b";
RequestQueue mRequestQueue;
// Instantiate the cache
Cache cache = new DiskBasedCache(getCacheDir(), 1024 * 1024);
// Setup the network to use the HTTPURLConnection client
Network network = new BasicNetwork(new HurlStack());
// Instantiate the requestqueue
mRequestQueue = new RequestQueue(cache, network);
// Start the queue
mRequestQueue.start();
JsonObjectRequest jsObjRequest = new JsonObjectRequest
(Request.Method.GET, apiUrl, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// Load the initial JSON request
try {
JSONObject mainWeather = response.getJSONObject("main");
int temp = mainWeather.getInt("temperature");
text.setText("The temperature is " + temp);
Toast.makeText(this, "" ,Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
Response.ErrorListener errorListener = new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
//Get response code here
VolleyLog.e("Error: ", error.toString());
VolleyLog.e("Error: ", error.getLocalizedMessage());
}
};
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment