Skip to content

Instantly share code, notes, and snippets.

@omegachien
Created March 25, 2015 08:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omegachien/04bcf18af6f9f0472865 to your computer and use it in GitHub Desktop.
Save omegachien/04bcf18af6f9f0472865 to your computer and use it in GitHub Desktop.
Sample Volley Code
//Remember to put the line below into your build.gradle's dependencies
//compile 'com.mcxiaoke.volley:library:1.0.15'
private void refreshWeather() {
RequestQueue queue = Volley.newRequestQueue(getActivity());
String url = "http://api.openweathermap.org/data/2.5/forecast/daily?q=94043&mode=json&units=metric&cnt=7";
JsonObjectRequest req = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
if (getActivity() == null) {
return;
}
try {
String cod = response.getString("cod");
JSONArray list = response.getJSONArray("list");
for (int i = 0; i < list.length(); i++) {
JSONObject jsonObj = list.getJSONObject(i);
JSONObject temp = jsonObj.getJSONObject("temp");
Log.d("Day " + i, temp.getString("day"));
}
} catch (Exception e) {
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (getActivity() == null) {
return;
}
NetworkResponse response = error.networkResponse;
JSONObject obj;
String message = "";
try {
obj = new JSONObject(new String(response.data));
JSONObject errorObj = obj.getJSONObject("error");
message = errorObj.getString("message");
Log.d("error", obj.toString());
} catch (JSONException e) {
}
}
});
queue.add(req);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment