This is My JSON Array with Different Quotes along with their Author Name.
{
"quotes": [
{
"id": "4",
"quote": "whatsapp is bad",
"author": "William W. Purkey "
},
{
"id": "3",
"quote": "yahoo is a company",
"author": "William W. Purkey "
},
{
"id": "1",
"quote": "Google is a search engine",
"author": "William W. Purkey "
}
]
}
I want to Fetch a Single Random JSON Object inside android. Instead of whole things.
Example :
{
"id": "4",
"quote": "whatsapp is bad",
"author": "William W. Purkey "
}
This is What I'm Getting Now in my Android App:

Here is my MainActivity.java
public class MainActivity extends AppCompatActivity {
private TextView quoteTV, authorTV, startReading;
// private RequestQueue mQueue;
private ProgressDialog progress;
public RequestQueue mQueue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startReading = findViewById(R.id.startReading);
quoteTV = findViewById(R.id.quoteTV);
quoteTV.setVerticalScrollBarEnabled(true);
quoteTV.setMovementMethod(ScrollingMovementMethod.getInstance());
quoteTV.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);
authorTV = findViewById(R.id.authorTV);
final Button buttonParse = findViewById(R.id.button_parse);
mQueue = Volley.newRequestQueue(this);
CardView editProfileLayout = (CardView) findViewById(R.id.authorTVBG);
editProfileLayout.setVisibility(View.GONE);
buttonParse.setText("Start Reading");
buttonParse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
buttonParse.setText("Next Quote");
CardView editProfileLayout = (CardView) findViewById(R.id.authorTVBG);
editProfileLayout.setVisibility(View.VISIBLE);
quoteTV.setText("");
authorTV.setText("");
progress = new ProgressDialog(MainActivity.this);
progress.setMessage("Loading.. Please Wait");
progress.setCancelable(true);
progress.show();
jsonParse();
startReading.setText("");
}
});
}
private void jsonParse() {
String url = "http://myjson.com/q3wr4";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("quotes");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject quotes = jsonArray.getJSONObject(i);
int id = quotes.getInt("id");
String quote = quotes.getString("quote");
String author = quotes.getString("author");
progress.hide();
quoteTV.append("“ " + String.valueOf(quote) + " ”");
authorTV.append("Author " + ": " + String.valueOf(author));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
mQueue.add(request);
}
}
I Need to Show Only One Quote in my android app instead of showing all Quotes from JSON Array. Need help. Thanks in Advance
Randomclass and get element at this position. That's it!Randomclass. so, random class will generate random number every time from the new range you have given to it. ex. if initially you have 3 objects in array thenRandomclass will generate random number between 0 to 2. Now, if you increase array size from your service, your app will find new length (suppose 4) andRandomclass will simply generate number between 0 to 3.