Created
June 17, 2015 20:15
-
-
Save EricZeiberg/314f4b8fe79537057f07 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams()); | |
HttpPost httppost = new HttpPost(PARK_ENDPOINT); | |
// Depends on your web service | |
httppost.setHeader("Content-type", "application/json"); | |
InputStream inputStream = null; | |
String result = null; | |
try { | |
HttpResponse response = httpclient.execute(httppost); | |
HttpEntity entity = response.getEntity(); | |
inputStream = entity.getContent(); | |
// json is UTF-8 by default | |
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8); | |
StringBuilder sb = new StringBuilder(); | |
String line = null; | |
while ((line = reader.readLine()) != null) | |
{ | |
sb.append(line + "\n"); | |
} | |
result = sb.toString(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
finally { | |
try{if(inputStream != null)inputStream.close();}catch(Exception squish){} | |
} | |
List<Park> parks = new ArrayList<>(); | |
Log.e("[INFO]", result); | |
try { | |
JSONArray jArray = new JSONArray(result); | |
for(int i=0; i < jArray.length(); i++) { | |
JSONObject jObject = jArray.getJSONObject(i); | |
String name = jObject.getString("name"); | |
String description = jObject.getString("description"); | |
String country = jObject.getString("country"); | |
String uri = jObject.getString("uri"); | |
int ID = jObject.getInt("id"); | |
Park newPark = new Park(name, description, uri, country, ID); | |
parks.add(newPark); | |
} // End Loop | |
} catch (JSONException e) { | |
Log.e("JSONException", "Error: " + e.toString()); | |
} // catch (JSONException e) | |
return parks; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment