Skip to content

Instantly share code, notes, and snippets.

@i3p9
Last active November 12, 2019 13:56
Show Gist options
  • Save i3p9/fe326a7e78c1fdeff9f6ec27602801b8 to your computer and use it in GitHub Desktop.
Save i3p9/fe326a7e78c1fdeff9f6ec27602801b8 to your computer and use it in GitHub Desktop.
Volley Parsing (ListView)
<ListView
android:id="@+id/listitems"
/>
<Button
android:id="@+id/button"
android:onClick="parseJSON"
/>
implementation 'com.android.volley:volley:1.0.0'
public class MainActivity extends AppCompatActivity {
//private TextView result;
private RequestQueue rqueue;
private ArrayList<String> items;
private ArrayAdapter<String> itemsAdapter;
private ListView lvItems;
protected void onCreate(Bundle savedInstanceState) {
lvItems = (ListView) findViewById(R.id.listitems);
//result = findViewById(R.id.view_parsed);
Button button = findViewById(R.id.button);
rqueue = Volley.newRequestQueue(this);
}
public void parseJSON(View view) {
String url = "http://anontech.info/courses/cse491/employees.json";
JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, url, null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
items = new ArrayList<String>();
itemsAdapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1, items);
if(lvItems!=null) {
lvItems.setAdapter(itemsAdapter);
}
for(int i=0;i<response.length();i++){
JSONObject jsonem = response.getJSONObject(i);
String name = jsonem.getString("name");
JSONObject loc = jsonem.getJSONObject("location");
String lat = loc.getString("latitude");
String lon = loc.getString("longitude");
//result.append(name+" Latitude/Longitude: "+lat+lon);
itemsAdapter.add(name);
}
} catch (JSONException e){
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
rqueue.add(request);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment