Android parse json from url

In this Android tip, I am going to show you how to parse a JSON string from a url. To achieve the goal you follow the steps below:
1. You need a PHP page on your remote server to return the JSON string. In my case, I have getoutlet.php file hosted at The getoutlet.php file fetches data from mysql database and returns a json string as you see in the picture below. The json string will be transferred to Android and parsed there.

parse json from url
2. Create a new project in Android Studio. Add id and orientation attributes to the layout of activity_main.xml file.

<LinearLayout xmlns:android= ""
   xmlns:tools= ""

In the MainActivity class, you create a class called BackTaskGetJSON that extends Asynctask. It represents a background process to connect to the remote server and get the json string. In the onCreate() method of the MainActivity class, you execute the background process. In the onPostExecute() method of BackTaskGetJSON class, you pass the response json string to the showResult() method to be parsed.

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.TextView;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends ActionBarActivity {
   Activity context;
   protected void onCreate(Bundle savedInstanceState) {
     // Execute background task to read json string from url
     BackTaskGetJSON backTaskGetJSON= new BackTaskGetJSON();

   private void showResult(String jsonString){
     // Reference to the parent layout
     LinearLayout layoutroot= (LinearLayout)findViewById(;
     //Parse Json and show output
     try {
        JSONArray jArr=new JSONArray(jsonString);
        for(int i=0;i<jArr.length();i++) {
           JSONObject obj=jArr.getJSONObject(i);
           // Create layout to display the outlet info (id and name)
           LinearLayout cust_layout = new LinearLayout(MainActivity.this);
           LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
                (LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
           params.setMargins(15, 5, 5, 5);
           cust_layout.setLayoutParams(params);           cust_layout.setOrientation( LinearLayout.HORIZONTAL);
           cust_layout.setPadding(10, 10, 5, 5);
           // Create textviews for id and name
           TextView tvId=new TextView(MainActivity.this);
           tvId.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT, 1f));
           TextView tvName=new TextView(MainActivity.this);
           tvId.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT, 1f));
           // Add textviews to layout
           // Add layout to its parent

     } catch (JSONException e) {

   //background process to get outlet info at remotee server
   private class BackTaskGetJSON extends AsyncTask<String,Void,Void> {
     String jsonString="";

     protected Void doInBackground(String...params){

           HttpClient httpclient=new DefaultHttpClient();
           HttpPost httppost= new HttpPost(""); //delivery linux
           ResponseHandler<String> responseHandler = new BasicResponseHandler();
           // Get json string from the url
           jsonString= httpclient.execute(httppost, responseHandler);
        }catch(Exception e){
        return null;
     protected void onPostExecute(Void result){
           if(!jsonString.equals("")) {
             // Parse json and show output
        }catch(Exception e){e.printStackTrace();}


Before you run the example application, do not forget to declare INTERNET person in AndroidManifest.xml file.

<uses-permission android:name="android.permission.INTERNET" />

parse json from url

Posted by: Dara | post date: 04-13-2015 | Subject: Android Apps Development

write programming tip

This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.