Android JSON Parsing from URL – Example

droid

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" } ] }

  • 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

  • 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=0×41465700)

    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)