Android JSON Parsing from URL – Example

You may also like...

  • Thanks

    Can it get more than one user ? If so how can I add another user in index.php ?

  • Zahid

    Once I get the data from the JSON file from the web. I would like to put it into an ARRAY based on “TYPE”. Any idea how do I go on about doing it?

    Here’s my question: http://stackoverflow.com/questions/19868439/how-to-put-json-information-in-an-array-to-use-it-for-displaying-in-listview

    • http://128.199.224.11/ Raj Amal

      Here it is Follow this tutorial.
      128.199.224.11/2013/11/listview-from-json-example.html

  • Muhammadibn

    Hi, Nice tutorial, I wanted to know if its possible to pull a date which is in json format and display it as a widget on the homepage?

  • Wasim Memon

    hi really nice tutorial
    but you know how i get all links from my website in json format.
    i want to create app for my site in android so i want it.

    • http://128.199.224.11/ Raj Amal

      Use plugins to get JSON feed instead of XML feed. So you can parse it in your app.

  • haji

    Hi! i am new in android, i follow all the steps but every time when i run the emulator i got error message “Unfortunately, JSONParsing has stopped” i am sure i did the all steps correctly.. please help

    • Srini Vasan

      Logcat report ?

      • haji

        Yes! When i run emulator

  • haji

    hi Srini and all this is error message!

    • http://128.199.224.11/ Raj Amal

      Open DDMS in eclipse and copy the Logcat and paste here.

      • Adam Alkouri

        11-27 22:55:35.485: E/AndroidRuntime(3185): FATAL EXCEPTION: main

        11-27 22:55:35.485: E/AndroidRuntime(3185): Process: com.example.testphpjson, PID: 3185

        11-27 22:55:35.485: E/AndroidRuntime(3185): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testphpjson/com.example.testphpjson.MainActivity}: android.os.NetworkOnMainThreadException

        11-27 22:55:35.485: E/AndroidRuntime(3185): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)

        11-27 22:55:35.485: E/AndroidRuntime(3185): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)

        11-27 22:55:35.485: E/AndroidRuntime(3185): at android.app.ActivityThread.access$700(ActivityThread.java:135)

        11-27 22:55:35.485: E/AndroidRuntime(3185): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)

        11-27 22:55:35.485: E/AndroidRuntime(3185): at android.os.Handler.dispatchMessage(Handler.java:102)

        11-27 22:55:35.485: E/AndroidRuntime(3185): at android.os.Looper.loop(Looper.java:137)

        11-27 22:55:35.485: E/AndroidRuntime(3185): at android.app.ActivityThread.main(ActivityThread.java:4998)

        11-27 22:55:35.485: E/AndroidRuntime(3185): at java.lang.reflect.Method.invokeNative(Native Method)

        11-27 22:55:35.485: E/AndroidRuntime(3185): at java.lang.reflect.Method.invoke(Method.java:515)

        11-27 22:55:35.485: E/AndroidRuntime(3185): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)

        11-27 22:55:35.485: E/AndroidRuntime(3185): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)

        11-27 22:55:35.485: E/AndroidRuntime(3185): at dalvik.system.NativeStart.main(Native Method)

        11-27 22:55:35.485: E/AndroidRuntime(3185): Caused by: android.os.NetworkOnMainThreadException

        11-27 22:55:35.485: E/AndroidRuntime(3185): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)

        11-27 22:55:35.485: E/AndroidRuntime(3185): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)

        11-27 22:55:35.485: E/AndroidRuntime(3185): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)

        11-27 22:55:35.485: E/AndroidRuntime(3185): at libcore.io.IoBridge.connect(IoBridge.java:112)

        11-27 22:55:35.485: E/AndroidRuntime(3185): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)

        11-27 22:55:35.485: E/AndroidRuntime(3185): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)

        11-27 22:55:35.485: E/AndroidRuntime(3185): at java.net.Socket.connect(Socket.java:843)

        11-27 22:55:35.485: E/AndroidRuntime(3185): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)

        11-27 22:55:35.485: E/AndroidRuntime(3185): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)

        11-27 22:55:35.485: E/AndroidRuntime(3185): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)

        11-27 22:55:35.485: E/AndroidRuntime(3185): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)

        11-27 22:55:35.485: E/AndroidRuntime(3185): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)

        11-27 22:55:35.485: E/AndroidRuntime(3185): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)

        11-27 22:55:35.485: E/AndroidRuntime(3185): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)

        11-27 22:55:35.485: E/AndroidRuntime(3185): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)

        11-27 22:55:35.485: E/AndroidRuntime(3185): at learn2crack.jsonparsing.library.JSONParser.getJSONFromUrl(JSONParser.java:38)

        11-27 22:55:35.485: E/AndroidRuntime(3185): at com.example.testphpjson.MainActivity.onCreate(MainActivity.java:35)

        11-27 22:55:35.485: E/AndroidRuntime(3185): at android.app.Activity.performCreate(Activity.java:5243)

        11-27 22:55:35.485: E/AndroidRuntime(3185): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)

        11-27 22:55:35.485: E/AndroidRuntime(3185): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)

        11-27 22:55:35.485: E/AndroidRuntime(3185): … 11 more

        11-27 22:55:37.825: I/Process(3185): Sending signal. PID: 3185 SIG: 9

        • http://128.199.224.11/ Raj Amal

          Just Use android:minSdkVersion=”8″
          in your Manifest. Remove Target SDK from the Manifest. It will fix the error.

          • Adam Alkouri

            Thank you for the response, I got it working. Here is my question. I have a different PHP file that I am working with that also uses your JSON encode script. The problem is, the output is different.

            Here is my PHP script:

            $con=mysql_connect(“$host”, “$username”, “$password”)or die(“cannot connect”);

            mysql_select_db(“$db_name”)or die(“cannot select DB”);

            $sql = “select * from users”;
            $result = mysql_query($sql);
            $json = array();

            if(mysql_num_rows($result)){
            while($row=mysql_fetch_assoc($result)){
            $json[]=$row;
            }
            }
            mysql_close($con);
            echo json_encode($json);
            ?>

            The output I get for this php file for only 1 row in my SQL database is:

            {“emp_info”:[{“uid”:”1″,”unique_id”:””,”fullname”:”Joshua”,”displayname”:””,”email”:”1234″,”password”:”1234″,”salt”:””,”created_at”:null,”updated_at”:null}

            How can I tweak this so it can be displayed in Android?

            This is what the output looks with your PHP file, its similar so I know I can get this to work, but I cant figure it out:

            { “user”: [ { “id”: “001”, “name”: “Raj Amal”, “email”: “raj.amalw@gmail.com” } ] }

        • ilham

          hai raj i’m download your project and check out the android:minSdkVersion is already “8” but i still have error like that

  • eron

    {
    “user”: [
    {
    “id”: “001”,
    “name”: “Raj Amal”,
    “email”: “raj.amalw@gmail.com”
    }
    ]
    }

    how can u generate your php code to generate that kind of json?
    i have problem in my php…
    because my php code is incomplete because it has no “user”..

  • 0fffspring

    I have “android.os.NetworkOnMainThreadException”
    Do not understand why you don’t have it

    • http://128.199.224.11/ Raj Amal

      Just Use android:minSdkVersion=”8″
      in your Manifest. Remove Target SDK from the Manifest. It will fix the error.

      • DocRockulus

        It IS bad practice to do any potentially long running processes on the main thread. Rather than simply hiding the issue, you should run the JSONParser in an async task or in it’s own thread.

        • http://128.199.224.11/ Raj Amal

          I have already written a article to use Async Task with JSON parsing.

  • Ravindra

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
    .permitAll().build();
    StrictMode.setThreadPolicy(policy);

    • Ravindra

      Put above code in your code this work for me

      • Bishal

        we can use thread too

  • droidcoder

    12-03 18:15:08.408: E/Trace(18869): error opening trace file: No such file or directory (2)

    • http://128.199.224.11/ Raj Amal

      Post the complete logcat report

  • tommy

    Hi Raj Amal,

    I have a little problem. When i run it, it give me some errors. Please help. I modified some code. When the button click, it take the url from the editText. and then …

    03-19 01:56:40.127: E/JSON Parser(1259): Error parsing data org.json.JSONException: End of input at character 0 of

    03-19 01:56:40.127: D/AndroidRuntime(1259): Shutting down VM

    03-19 01:56:40.137: W/dalvikvm(1259): threadid=1: thread exiting with uncaught exception (group=0x41465700)

    03-19 01:56:40.157: E/AndroidRuntime(1259): FATAL EXCEPTION: main

    03-19 01:56:40.157: E/AndroidRuntime(1259): java.lang.NullPointerException

    03-19 01:56:40.157: E/AndroidRuntime(1259): at com.frankie.arduinodetector.MainActivity.setData(MainActivity.java:106)

    03-19 01:56:40.157: E/AndroidRuntime(1259): at com.frankie.arduinodetector.MainActivity$1.onClick(MainActivity.java:47)

    03-19 01:56:40.157: E/AndroidRuntime(1259): at android.view.View.performClick(View.java:4240)

    03-19 01:56:40.157: E/AndroidRuntime(1259): at android.view.View$PerformClick.run(View.java:17721)

    03-19 01:56:40.157: E/AndroidRuntime(1259): at android.os.Handler.handleCallback(Handler.java:730)

    03-19 01:56:40.157: E/AndroidRuntime(1259): at android.os.Handler.dispatchMessage(Handler.java:92)

    03-19 01:56:40.157: E/AndroidRuntime(1259): at android.os.Looper.loop(Looper.java:137)

    03-19 01:56:40.157: E/AndroidRuntime(1259): at android.app.ActivityThread.main(ActivityThread.java:5103)

    03-19 01:56:40.157: E/AndroidRuntime(1259): at java.lang.reflect.Method.invokeNative(Native Method)

    03-19 01:56:40.157: E/AndroidRuntime(1259): at java.lang.reflect.Method.invoke(Method.java:525)

    03-19 01:56:40.157: E/AndroidRuntime(1259): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)

    03-19 01:56:40.157: E/AndroidRuntime(1259): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)

    03-19 01:56:40.157: E/AndroidRuntime(1259): at dalvik.system.NativeStart.main(Native Method)

    • camsson

      In the code recipe:
      while ((line = reader.readLine()) != null) {
      sb.append(line + “n”);
      }
      try to delect:+ “n” may resolve your problem.

  • Julian K

    This is working without AsyncTaskt ?

  • Nauna IMY

    how to make json webservices in asp.net and bind it with android listview

  • ahmad

    when im trying to run the application, Im getting a black screen with nothing happening..

    • ahmad

      and after 2 minutes it says “the application has stopped unexpectedly. please try again.” :/

  • Ayu Kusumaningtyas

    Hi, Raj..
    Is it possible to make multiple jsonparser in 1 android project?

  • Giovanni Ciavarelli

    Hello! I am a NewBie in Java and programming …. Sorry. I am using “Android Studio” and I can’t understand how to: …

    “In your Project create a new folder library in the src folder such that the package is “learn2crack.jsonparsing.library”. Create the JSONParser.java in the library folder.”

    When I do NEW->FOLDER I can’t see “Folder Library” … what I should do? Thanks!

  • carlos

    my app stop working, i already changed the target SDK and the minSdkVersion is 8, th logcat is:
    09-16 11:33:14.000: E/WifiHW(1508): wifi_connect_to_supplicant: ctrl_conn(0x4a6d78)
    09-16 11:33:14.156: E/libnetutils(1508): dhcp start cmd interface : [dhcpcd:-ABK eth0]
    09-16 11:33:14.515: E/ClockWidget(12484): clock_bg portrait
    09-16 11:33:14.515: E/ClockWidget(12484): drawDayText
    09-16 11:33:14.515: E/ClockWidget(12484): width= 50
    09-16 11:33:14.515: E/ClockWidget(12484): widthText= 52.0
    09-16 11:33:14.515: E/ClockWidget(12484): RIGHT
    09-16 11:33:14.546: E/StatusBarPolicy(1561): ecio: 33
    09-16 11:33:14.546: E/StatusBarPolicy(1561): iconLevel: 4
    09-16 11:33:20.742: E/StatusBarPolicy(1561): ecio: 34
    09-16 11:33:20.742: E/StatusBarPolicy(1561): iconLevel: 4
    09-16 11:33:46.265: E/Buffer Error(12657): Error converting result java.lang.NullPointerException
    09-16 11:33:46.265: E/JSON Parser(12657): Error parsing data org.json.JSONException: End of input at character 0 of
    09-16 11:33:46.273: E/liblog(1508): failed to call dumpstate
    09-16 11:33:46.273: E/AndroidRuntime(12657): FATAL EXCEPTION: main
    09-16 11:33:46.273: E/AndroidRuntime(12657): java.lang.RuntimeException: Unable to start activity ComponentInfo{learn2crack.jsonparsing/learn2crack.jsonparsing.MainActivity}: java.lang.NullPointerException
    09-16 11:33:46.273: E/AndroidRuntime(12657): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
    09-16 11:33:46.273: E/AndroidRuntime(12657): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
    09-16 11:33:46.273: E/AndroidRuntime(12657): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
    09-16 11:33:46.273: E/AndroidRuntime(12657): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
    09-16 11:33:46.273: E/AndroidRuntime(12657): at android.os.Handler.dispatchMessage(Handler.java:99)
    09-16 11:33:46.273: E/AndroidRuntime(12657): at android.os.Looper.loop(Looper.java:130)
    09-16 11:33:46.273: E/AndroidRuntime(12657): at android.app.ActivityThread.main(ActivityThread.java:3687)
    09-16 11:33:46.273: E/AndroidRuntime(12657): at java.lang.reflect.Method.invokeNative(Native Method)
    09-16 11:33:46.273: E/AndroidRuntime(12657): at java.lang.reflect.Method.invoke(Method.java:507)
    09-16 11:33:46.273: E/AndroidRuntime(12657): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
    09-16 11:33:46.273: E/AndroidRuntime(12657): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)

  • cypo

    while ((line = reader.readLine()) != null) {
    sb.append(line + “n”);
    }

    It should be: line + “n”

  • mahendra

    above code is throwing exception as null and application stopped

  • Boon Chew

    Hi, Raj Amal.
    I want to fetch data from localhost to my android app.
    I had follow your code. However, i won’t be able to run my application.
    Is’t my run configuration got problem ?

  • Guest

    Here are the error. I m new in android. Thank !

  • aswin

    hi…… i downloaded your code n run it ……but it is not running.. it is showing unfortunately,app has stopped…. i need correct solution for this error…

  • aswin

    when i try your code it is shown this error … can u tell me what is the problem is ???

  • Kirill Varivoda

    Hi, is there is a way to post comments (or even posts) in WordPress blog using Json, for exmaple?
    It would be great!