DEV Community

Cover image for How to parse JSON data from server to Android

Posted on

How to parse JSON data from server to Android

Hi Everyone!!!, My Name is Mathesh a student and part time Mobile and Web App Developer. In this tutorial, I am going to fetch data from server through android app and print the same in android app

Getting a data from server and and parsing it into applications is a necessity for dynamic mobile applications.
So we are going to see a basic step on how to get data from database through server and show it to the user who is using mobile app. On this tutorial we are going to use MYSQL as a database, PHP as a server language and Java as a android language.

The first step of the process is to code the server side files. On this example we are looking at a scenario where we have to show the salary of the specific employee in a company. So we start by getting the value from database and printing the value in JSON format so as to communicate with the mobile application.

header('Content-Type: application/json');




$conn = mysqli_connect($servername,$username,$password,$dbname);

//Checking weather the values is passed in the URL. 
    $logid = $_GET['logid'];

    $sql = "SELECT p_amt FROM package  WHERE c_uname = '$logid'";

    $res = mysqli_query($conn,$sql);

    $packages = array();

    while($row = mysqli_fetch_array($res))



    echo json_encode(array("packages"=>$packages));


Enter fullscreen mode Exit fullscreen mode

So in the above code we are first defining the values needed for connection of database and further connecting to the MYSQL database. We are using GET method to get the login id of the employee through URL and fetching the related salary from the database. At the last step we are printing the fetched data from database in JSON format.

With the above step the server side of things is over, now we can concentrate on fetching the data through Android.

Since we are fetching the details in JSON format, we are going to use Volley Library to fetch the data from the database.

implementation ''
Enter fullscreen mode Exit fullscreen mode

Next step is to access internet permission since we are using Internet to communicate with the server. In the Android Manifest file fill the below permission to proceed further.

<uses-permission android:name="android.permission.INTERNET" />
Enter fullscreen mode Exit fullscreen mode

Now, let us the design the activity_main.xml with a single text view to display the amount.

<LinearLayout xmlns:android=""
            android:textSize="25sp" />
Enter fullscreen mode Exit fullscreen mode

Now lets start coding the file by first declaring the necessary variables.

TextView amt;
private RequestQueue mQueue;
Enter fullscreen mode Exit fullscreen mode

Now refer the same variables in the oncreate method

amt=(TextView) findViewById(;
mQueue = Volley.newRequestQueue(this);
Enter fullscreen mode Exit fullscreen mode

Let's now write a function to get JSON data from the String URL. This function should be placed within MainActivity Class.

 public static JSONObject getJSONObjectFromURL(String urlString) throws IOException, JSONException {

        HttpURLConnection urlConnection = null;

        URL url = new URL(urlString);

        urlConnection = (HttpURLConnection) url.openConnection();

        urlConnection.setReadTimeout(10000 /* milliseconds */);
        urlConnection.setConnectTimeout(15000 /* milliseconds */);



        BufferedReader br=new BufferedReader(new InputStreamReader(url.openStream()));

        char[] buffer = new char[1024];

        String jsonString = new String();

        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = br.readLine()) != null) {

        jsonString = sb.toString();

        System.out.println("JSON: " + jsonString);

        return new JSONObject(jsonString);
Enter fullscreen mode Exit fullscreen mode

The above method gets string(URL) as a parameter and return the JSON values it retrieved.

The final step is to use this method, apply the same to get the salary amount and display it.

String Channel="1201MAN02" //This is the Employee Id sent through GET method.
String URL = new StringBuilder().append("").append(channel).toString();
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, URL, null,
                new Response.Listener<JSONObject>() {
                    public void onResponse(JSONObject response) {
                            JSONArray jsonArray = response.getJSONArray("packages");
                                JSONObject employee = jsonArray.getJSONObject(0);
                                int totalamt = employee.getInt("totamt");
                                totamt.append( "Total Amount Collected By "+channel + "\n\n" );
                                amt.append(" "+String.valueOf(totalamt) + "\n\n");
                        catch (JSONException e)
                }, new Response.ErrorListener() {
            public void onErrorResponse(VolleyError error) {
Enter fullscreen mode Exit fullscreen mode

In the above code snippet we are declaring the employee id to be parsed along with the URL and fetching the amount from the data.

Note:- Here I am using my firm QuasarTechSolutions as a URL. It won't work if you are trying with the same URL. Pls change the URL to your desired URL to make the application error free.

So that's it, Whoooooo!!! You have learned how to get data from Server through JSON and use it to your android app.

If you have any further doubts, please contact me through:

LinkedIn : Connect me through LinkedIn
Email :

Top comments (0)