您好,现在是 2024年11月17日 15点30分 星期日

Android: Client-Server communication by JSON

Refer to: http://osamashabrez.com/client-server-communication-android-json/

This is a sequel to my last blog post about Client Server Communication In Android in which I discussed how to implement client server communication with default HTTPClient with GET and POST request. In this tutorial we’ll be covering the next part doing communication with the server through JSON.

Based on JavaScript object literals JSON is a textual representation of complex data instances. It has 4 basic types:

  • Strings
  • Numbers
  • Boolean
  • Null

Out of these four types objects or arrays can be constructed where object in terms of JSON is used for key-value pairs. The entire specification can be read at RFC 4627 just in case. If you are new to this name and never heard of JSON class before or don’t want to go over all the PFC details, here are a few JSON examples that will help you clear you mind about how this works.

JSON Numbers: 47, 2.5e15, 0, -7.2e8. These are all JSON numbers whereas 0005 or 02 are not, as there must be no leading zero.

JSON Strings: JSON Strings are declared with double quotes (“This is a JSON String”) and string literals with single quotes are an error. JSON also supports hexa digits with strings.

JSON Literals: Literals are true, false and null.

JSON Array: Array in JSON can be declared via square parenthesis.

Two declare array inside an array as an element we’ll write as:

JSON Objects: Objects or key-value pairs are declared with curly brackets as:

And just as we declared arrays inside another array we can declare objects inside an object.

or

Why JSON over XML

JSON is more appropriate to use specially in case of mobile communication because it’s not cluttered with the “Well Designed XML Tag Names” and hence is not less bandwidth consuming yet very powerful. So using JSON objects to communicate with your server will be more appropriate then transmitting a XML file and then getting back a XML result.

 

Before we begin with the code, you can download the source from:

So lets begin with the project explanation:

Build JSON Object

 

So here we declared a JSON object jsonobj, added some key-value pairs in it named key and weburl. Values of those keys were value and hashincludetechnology.com. Next to demonstrate the nesting JSON Objects, another JSON object headers and added some information about the device test will be tested from including device model, device version and language in ISO3.

If you haven’t downloaded the source you need not to worry the buildref is nothing except a reference to the EditText box on screen.

Send JSON To The Server

In the next part we’ll send our object over the network to our server side script to make things clear I’ll break the code into smaller chunks.

 

Here we declared a httpclient and httppostreq object pretty same as we did in our last tutorial and then inside a StringEntity we decoded our JSON object into string. With adding a few required headers to mimic the POST behavior we are now ready to send our JSON object over the network by adding this StringEntity into our POST Req object and getting a response from the server.

 

 

Here we received a reply from the server back and displayed the returned data into another on screen EditText field. If you take a closer look you’ll see that we are extracting contents from a gzip, this is so if your server has gzip enabled then the application will extract the contents and will save them back to the same InputStream object. I have removed some basic error checks in this code snippet please refer to the project source to stay with the best practices.

 

Something missing, isn’t it? the convertStreamToString() function?

 

This code was tested on Samsung GT-i9100 with wamp server (v2.1, apache 2.2.1.7 and php 5.3.4) on my local machine.

Application screenshots

Internet Permissions

To get access to internet at Android, following field must be included to AndroidManifest.xml file of the project:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

Hope this tutorial will server your purpose of understanding the basics of JSON Object transfers. In case of any difficulties, queries or questions you can post a comment and I’ll get back to you as early as possible. The code might be mess as I was doing a project along (will be releasing my first app soon) and keep going back and forth, bare with that and happy coding!

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。