<font class="popmake-631">Subscribe</font>Subscribe 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..!

(14531)