Android Json Parse Volley Library Use
The most useful structure to access a json file on your host or remotely accessed from your android program and display the data in a list is as follows.
If your Json file is different, you may need to edit it.
Json File
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
{ "fruits":[ "Apple", "Apricot", "Avocado", "Banana", "Bilberry", "Blackberry", "Blackcurrant", "Blueberry", "Boysenberry", "Currant", "Cherry", "Cherimoya", "Cloudberry", "Coconut", "Cranberry", "Custard apple", "Damson", "Date", "Dragonfruit", "Durian", "Elderberry", "Feijoa", "Fig", "Goji berry", "Gooseberry", "Grape", "Raisin", "Grapefruit", "Guava" ] } |
- File:src/MainActivity.java:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
import android.app.ProgressDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.StringRequest; import com.android.volley.toolbox.Volley; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { ListView fruitsList; String url = "http://www.thecrazyprogrammer.com/example_data/fruits_array.json"; ProgressDialog dialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); fruitsList = (ListView)findViewById(R.id.fruitsList); dialog = new ProgressDialog(this); dialog.setMessage("Loading...."); dialog.show(); StringRequest request = new StringRequest(url, new Response.Listener<String>() { @Override public void onResponse(String string) { parseJsonData(string); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError volleyError) { Toast.makeText(getApplicationContext(), "Some error occurred!!", Toast.LENGTH_SHORT).show(); dialog.dismiss(); } }); RequestQueue rQueue = Volley.newRequestQueue(MainActivity.this); rQueue.add(request); } void parseJsonData(String jsonString) { try { JSONObject object = new JSONObject(jsonString); JSONArray fruitsArray = object.getJSONArray("fruits"); ArrayList al = new ArrayList(); for(int i = 0; i < fruitsArray.length(); ++i) { al.add(fruitsArray.getString(i)); } ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, al); fruitsList.setAdapter(adapter); } catch (JSONException e) { e.printStackTrace(); } dialog.dismiss(); } } |
- File:layout/activity_main.xmlSimple interface file with listview:
-
234567891011121314151617<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="com.jsonparsing.MainActivity"><ListViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/fruitsList"/></RelativeLayout>
The Internet permission you need to add to your AndroidManifest.xml file.
2 |
<uses-permission android:name="android.permission.INTERNET" /> |
volley library that you need to add to the dependencies section of your build.gradle (Module: app) file
2 |
<span class="crayon-i">compile</span> <span class="crayon-s">'com.android.volley:volley:1.0.0'</span> |
Instead of showing in a list, it may be desirable to access certain items. Transportation to the 3rd element in the Array will be as follows.
2 |
<span class="crayon-v">fruitsArray</span><span class="crayon-sy">.</span><span class="crayon-e">getString</span><span class="crayon-sy">(</span><span class="crayon-cn">3</span><span class="crayon-sy">)</span> |
You can also use the variable in other places or in a TextView that you define.
2 |
show.setText(fruitsArray.getString(3)); |
if you have question do not forget to write from the chat button next to it or from the comment
Recent Comments