Android AsyncTask with JSON Parsing – Example


You may also like...

  • Alex

    Thank you very much. You helped me allot.

    • Raj Amal

      Thank you, Keep visiting :)

  • ikki

    when you mean that we need a json folder in the root server, are you refering where is the android-sdks??? adb command?
    Thanks for your replay

    • Raj Amal

      Yes you need. Or change the URL to “” to test.

  • shouba

    What if my JSON Array does not have a name “user”, How to do that ?

    • Raj Amal

      Rename “user” to the name you used in your array.

  • Zahid
  • Khairul

    What if the content has more layers of array in it. How do i extract them out.

    “directions”: [
    “features”: [
    “attributes” : {

    Tried doing this way, but there’s no output in my textview.

    directions = json.getJSONArray(TAG_DIRECTIONS);
    JSONObject c = directions.getJSONObject(0);

    JSONObject features = c.getJSONObject(TAG_FEATURES);
    String attributes = features.getString(TAG_ATTRIBUTES);

  • Benny Margalit

    thanks a lot.
    in the example i can aslo define JSONParse as just return JSONObject and 2 other parameters can be made Void

    • Raj Amal

      Yes you can use void

  • Arijit Chakraborty

    Really helped me a lot man :-D…u rock m/

  • tj

    can you give some examples for http post,like login…can register the username and password..

  • Muhammad Umair

    Sir its giving me error tat string cannot be converted to JSONARRAY

    • Raj Amal

      Post your JSON format.

      • Sunil

        Raj Sir.. It is giving me error Null pointer exception. I have tried alot. Please tell me what to do ? Thanks in Advance.

        Code is same as u have given. Please help me.

        • daddeldu

          I’ve got the same error :/ any solution?

        • darr

          i get the same error!!!!

      • Aeronn Guillermo

        me too im having a hard time on that error please HELP me . thanks

      • Aeronn Guillermo


  • Allah

    Just a little mistake there

    sb.append(line + “n”);

    It should be a “n”

    • Raj Amal

      Ya. Fixed

      • David

        is still: sb.append(line + “n”)
        i have changed in: sb.append(line + “/n”)
        and works.
        thanks to both of you!

  • Gopala Palanee

    i want to pass those data to the JSONParser

    ArrayList nameValuePairs = new ArrayList();
    nameValuePairs.add(new BasicNameValuePair(“usernamejoiner”, username));
    nameValuePairs.add(new BasicNameValuePair(“departuretime”, departure_t));
    nameValuePairs.add(new BasicNameValuePair(“endtime”, end_t));
    nameValuePairs.add(new BasicNameValuePair(“departureaddress”, deparure_a));
    nameValuePairs.add(new BasicNameValuePair(“destinationaddress”, destination_a));
    nameValuePairs.add(new BasicNameValuePair(“date”, date_t));

    the data shall be obtain from sharedpreferences

    base on the data send the url , the php file will send a valid form of json data
    how to do that ? a little help please …

    • Raj Amal

      Add this to your JSON Parser and add parameter to the function.
      httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

  • Anas Sam

    I used your project and test it and i have an issue in :

    user = json.getJSONArray(TAG_USER);

    Can you help me with this?

    • Raj Amal

      What issue ?

      • shs

        my app crashed

        in the logcat this line has problem

        please help me

  • Tasos Ombashis

    Is this possible to do it for a widget? so its can read data from a php file and shows them to a textview in widget? thanks

  • folo ouzouma

    Good morning can anybody please help to know how to store the json object recieve into an android sqlite database

  • Tolgay


    I want to ask a question about this.Can you send me an email ? My email is

  • prachi


    I want to know that I am using php api and want that same code use for post data on server and same code use for get data from server is this possible

    • Rose Caz

      me too !!

  • Archana Sarang

    Thank you Raj Sir, Now I can understand JSON Parsing in android….

  • gonza28

    I do not want listview. May want TextView

  • f5

    this error when I click GetData

  • Dar

    Hello I tried the same code and I get a fatal run time exception .”NetworkOnMainThreadException”. How can I fix this?

    • Arno Turelinckx

      Make sure you dont forget to add this line to your manifest file.

  • bilal

    i am using the same code but i need to get data from database after certain time say after every 5 mins and reload list view

  • Batal Sunat

    Hello, I am trying to change the coding , but I found the error when the application is run.

    The following error appears

    Error Parsing data org.json.JSONExcpection: Unterminated array at character 14 of {n”radio”:[n{n”server”:”ssjsjsjsj”,n”rj”:jhon”,n”pesan”:”message”n}n]n}n

    My json file

    “radio”: [
    “server”: “ssjsjsjsj”,
    “rj”: “jhon”,
    “pesan”: “message”

  • Rose Caz

    Hello sir , there is a plenty of contacts and i want to show only one, meaning i will show contacts by ID on textview???

  • Evan Dix

    Why do i keep getting a null pointer exception????

  • Rakesh Kumar

    Please help me ………………………. nullPointerException occur……………..

    02-07 14:57:11.218: E/AndroidRuntime(25163): FATAL EXCEPTION: main

    02-07 14:57:11.218: E/AndroidRuntime(25163): Process: learn2crack.asynctask, PID: 25163

    02-07 14:57:11.218: E/AndroidRuntime(25163): java.lang.NullPointerException

    02-07 14:57:11.218: E/AndroidRuntime(25163): at learn2crack.asynctask.MainActivity$JSONParse.onPostExecute(

    02-07 14:57:11.218: E/AndroidRuntime(25163): at learn2crack.asynctask.MainActivity$JSONParse.onPostExecute(

    02-07 14:57:11.218: E/AndroidRuntime(25163): at android.os.AsyncTask.finish(

    02-07 14:57:11.218: E/AndroidRuntime(25163): at android.os.AsyncTask.access$600(

    02-07 14:57:11.218: E/AndroidRuntime(25163): at android.os.AsyncTask$InternalHandler.handleMessage(

    02-07 14:57:11.218: E/AndroidRuntime(25163): at android.os.Handler.dispatchMessage(

    02-07 14:57:11.218: E/AndroidRuntime(25163): at android.os.Looper.loop(

    02-07 14:57:11.218: E/AndroidRuntime(25163): at

    02-07 14:57:11.218: E/AndroidRuntime(25163): at java.lang.reflect.Method.invokeNative(Native Method)

    02-07 14:57:11.218: E/AndroidRuntime(25163): at java.lang.reflect.Method.invoke(

    02-07 14:57:11.218: E/AndroidRuntime(25163): at$

    02-07 14:57:11.218: E/AndroidRuntime(25163): at

    02-07 14:57:11.218: E/AndroidRuntime(25163): at dalvik.system.NativeStart.main(Native Method)

  • Rakesh Kumar

    nullPointerException!!!! how to solve?

  • Maha lakshmi

    How to add another data into that php file at runtime?

  • Isai


    BufferedReader lector = new BufferedReader(new InputStreamReader(inputStream,”iso-8859″),8);

    StringBuilder stringBuilder = new StringBuilder();

    String linea = null;

    while((linea = lector.readLine())!= null){




    json = stringBuilder.toString();

    }catch(IOException e){

    Log.e(“Erro de lectura(Buffer)”,”Error al convertir el resultado: “+e.getMessage());


    Buffer Error:﹕ Error al convertir el resultado: iso-8859