public class MainActivity extends AppCompatActivity {
private TextView textView;
private RequestQueue requestQueue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView_result);
Button button = findViewById(R.id.button_parse);
requestQueue = Volley.newRequestQueue(this);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
jsonParse();
}
});
}
private void jsonParse(){
String url = "https://api.myjson.com/bins/iogw3";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("employees");
for (int i = 0; i <jsonArray.length() ; i++) {
JSONObject employee = jsonArray.getJSONObject(i);
String firstName = employee.getString("firstname");
int age = employee.getInt("age");
String mail = employee.getString("mail");
textView.append(firstName +" , "+ String.valueOf(age) +" , "+mail+"\n\n");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
requestQueue.add(request);
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:gravity="center">
<TextView
android:id="@+id/textView_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/button_parse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView_result"
android:layout_marginTop="30dp"
android:text="Parse" />
</RelativeLayout>
implementation 'com.android.volley:volley:1.1.1'