SubscribeSubscribe with Androidlift to get new post on mail.

Android Web Service Example With JSON Insert Data MYSQL Database

Android Web Service Example With JSON Insert Data MYSQL Database

Android Web Service Example With JSON Insert Data MYSQL Database. Web services make it easy for mobile devices to communicate with applications running on a web or application server. I am sharing my findings of how to implement an Android client for services implemented according to the Representational State Transfer (REST) approach. This little turorial shows how you send HTTP requests, parse JSON string response and display the results , showing a progress dialog while loading.

android web service example Required Permissions

Well, your App will need to connect to the internet in order to request your web services. This requires the permission android.permission.INTERNET.

Update your manifest XML file so that it registers the permission, like:

JavaScript Object Notation (JSON)

JSON is a specified format for the exchange of structured data. The advantage of JSON, compared to XML, is the ability to not only provide primitive types, such as strings or numbers, but also objects and arrays. Some people consider JSON also as more leightweight and easier to read than XML. The syntax is the same like defining JavaScript objects as it is specified in ECMAScript (3rd edition).

This would be a valid JSON object:

“By contrast with JSON, especially with a dynamic programming language, you can get a reasonable in-memory representation just by calling a library function”. You will see later what that means.

Requesting the Service

Android provides already two APIs with that you can send requests to web servers and handle their responses: Apache HTTP Client and HttpUrlConnection. According to the Android developers, HttpUrlConnection should be preferred due to more speed and less energy consumption. So we will use HttpUrlConnection Class.

Android web service example Output :

android web service example
Android RESTful WEB-Service

Android web service example Database Output :

android web service example
RESTfull webservice database output

Create new Project in studio as WebServiceDemo. And follow the below coding.

activity_main.xml

MainActivity.java

HTTPURLConnection.java

AndroidManifest.xml

Optimizing Web Services for Mobile Devices

Web service producers should keep the payload as small as possible in order to speed up clients, in particular mobile devices. They will appreciate every KByte they do not have to down- or upload. If you are designing your own web services for mobile devices, the following tips might help you.

  • Keep URLs short. Utilize URL-rewriting or use short proxy domains.
  • Provide only data which is actually required. You might provide different versions of the same service with more or less attributes.
  • Use attribute names of maximum two characters. You can map these names in your Java code to more descriptive constant names.
  • Remove unnecessary HTTP headers. For instance, PHP offers the function header_remove().
  • Do not render white spaces.
  • When delivering collections: Do not forget to set a maximum number of records. That is a little parameter which gets forgotten very often in real-world applications…
  • Follow some best practices when introducing new services.

Code for PHP WEB-Service is as follows.add_employee.php

add_employee.php

connection.php

Best luck..!

(2004)

  • What’s Happening i’m new to this, I stumbled upon this I have found It positively helpful and it has helped me out loads. I’m hoping to give a contribution & assist other customers like its helped me. Good job.|

  • I don’t even know how I ended up here, but I thought this post was good. I don’t know who you are but definitely you’re going to a famous blogger if you are not already 😉 Cheers!|
    marinir seo

    • richest man alive

      I agree with you

  • Hey very nice site!! Man .. Excellent .. Amazing .. I’ll bookmark your site and take the feeds additionally? I’m satisfied to search out so many helpful info right here in the put up, we’d like develop extra techniques on this regard, thank you for sharing. . . . . .|

  • I have been exploring for a little bit for any high-quality articles or weblog posts in this sort of house . Exploring in Yahoo I eventually stumbled upon this website. Reading this information So i am satisfied to convey that I’ve an incredibly excellent uncanny feeling I discovered just what I needed. I so much undoubtedly will make certain to do not overlook this website and give it a look a relentless basis|regularly}.|

    • Thanks for your timely support, we appreciate your response…!

  • Harshad

    Can i use this code to send data to the Server without writing php code ?

    • No dear you have to get that data in any server side language(PHP, .Net, Java), so you can put it in server database and users can use that centrally.
      This way you can deal with the above posted code.

  • I visit everyday a few blogs and sites to read content, however this
    wepage offers feature based posts.

  • richest man alive

    This is just amazing! Dude your tutorial is the best. I just tried it and it worked perfectly. The app I’m building stores data in the SQLiteDB. Can I use this same method to send data from my SQLite tables to the SQL web server?

  • Anthony

    Thanks man, it works like charm….How can i send the data selected from spinner

  • janak raj

    Sir I am new Developer
    any one know where to place add_employee.php and connection.php in Android studio i dont uderstand its role

    • Hi Pankaj,
      you can place those file on your local server like wamp, xamp or on live server if you have just google for that..?

  • janak raj

    whats path /?????