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.

["hello, this is my", 2, "tut on android", true]

1

["hello, this is my", 2, "tut on android", true]

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

[["hello", "this", "is", "my"], 2, ["tut", "on", "android"], true]

1

[["hello", "this", "is", "my"], 2, ["tut", "on", "android"], true]

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

{"A": "first object", "B": 2, "C": "third object"}

1

{"A": "first object", "B": 2, "C": "third object"}

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

{"A": {1:"A1", 2:"B1"}, "B" :{ "abc":123, "xyz":789}, "C": "finish"}

1

{"A": {1:"A1", 2:"B1"}, "B" :{ "abc":123, "xyz":789}, "C": "finish"}

or

{
"A":{
1:"A1",
2:"B1"
},
"B" :{
"abc":123,
"xyz":789
},
"C": "finish"
}

1

2

3

4

5

6

7

8

9

10

11

{

"A":{

1:"A1",

2:"B1"

},

"B" :{

"abc":123,

"xyz":789

},

"C": "finish"

}

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

JSONObject jsonobj; // declared locally so that it destroys after serving its purpose
jsonobj = new JSONObject();
try {
// adding some keys
jsonobj.put("key", "value");
jsonobj.put("weburl", "hashincludetechnology.com");

// lets add some headers (nested JSON object)
JSONObject header = new JSONObject();
header.put("devicemodel", android.os.Build.MODEL); // Device model
header.put("deviceVersion", android.os.Build.VERSION.RELEASE); // Device OS version
header.put("language", Locale.getDefault().getISO3Language()); // Language
jsonobj.put("header", header);
// Display the contents of the JSON objects
buildref.setText(jsonobj.toString(2));
} catch (JSONException ex) {
buildref.setText("Error Occurred while building JSON");
ex.printStackTrace();
}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

JSONObject jsonobj; // declared locally so that it destroys after serving its purpose

jsonobj = new JSONObject();

try {

// adding some keys

jsonobj.put("key", "value");

jsonobj.put("weburl", "hashincludetechnology.com");

// lets add some headers (nested JSON object)

JSONObject header = new JSONObject();

header.put("devicemodel", android.os.Build.MODEL); // Device model

header.put("deviceVersion", android.os.Build.VERSION.RELEASE); // Device OS version

header.put("language", Locale.getDefault().getISO3Language()); // Language

jsonobj.put("header", header);

// Display the contents of the JSON objects

buildref.setText(jsonobj.toString(2));

} catch (JSONException ex) {

buildref.setText("Error Occurred while building JSON");

ex.printStackTrace();

}

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.

DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppostreq = new HttpPost(wurl);
StringEntity se = new StringEntity(jsonobj.toString());
se.setContentType("application/json;charset=UTF-8");
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json;charset=UTF-8"));
httppostreq.setEntity(se);
HttpResponse httpresponse = httpclient.execute(httppostreq);

1

2

3

4

5

6

7

DefaultHttpClient httpclient = new DefaultHttpClient();

HttpPost httppostreq = new HttpPost(wurl);

StringEntity se = new StringEntity(jsonobj.toString());

se.setContentType("application/json;charset=UTF-8");

se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json;charset=UTF-8"));

httppostreq.setEntity(se);

HttpResponse httpresponse = httpclient.execute(httppostreq);

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.

HttpEntity resultentity = httpresponse.getEntity();
InputStream inputstream = resultentity.getContent();
Header contentencoding = httpresponse.getFirstHeader("Content-Encoding");
if(contentencoding != null && contentencoding.getValue().equalsIgnoreCase("gzip")) {
inputstream = new GZIPInputStream(inputstream);
}
String resultstring = convertStreamToString(inputstream);
inputstream.close();
resultstring = resultstring.substring(1,resultstring.length()-1);
recvdref.setText(resultstring + "\n\n" + httppostreq.toString().getBytes());
JSONObject recvdjson = new JSONObject(resultstring);
recvdref.setText(recvdjson.toString(2));

1

2

3

4

5

6

7

8

9

10

11

12

HttpEntity resultentity = httpresponse.getEntity();

InputStream inputstream = resultentity.getContent();

Header contentencoding = httpresponse.getFirstHeader("Content-Encoding");

if(contentencoding != null && contentencoding.getValue().equalsIgnoreCase("gzip")) {

inputstream = new GZIPInputStream(inputstream);

}

String resultstring = convertStreamToString(inputstream);

inputstream.close();

resultstring = resultstring.substring(1,resultstring.length()-1);

recvdref.setText(resultstring + "\n\n" + httppostreq.toString().getBytes());

JSONObject recvdjson = new JSONObject(resultstring);

recvdref.setText(recvdjson.toString(2));

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?

private String convertStreamToString(InputStream is) {
String line = "";
StringBuilder total = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
while ((line = rd.readLine()) != null) {
total.append(line);
}
} catch (Exception e) {
Toast.makeText(this, "Stream Exception", Toast.LENGTH_SHORT).show();
}
return total.toString();
}

1

2

3

4

5

6

7

8

9

10

11

12

13

private String convertStreamToString(InputStream is) {

String line = "";

StringBuilder total = new StringBuilder();

BufferedReader rd = new BufferedReader(new InputStreamReader(is));

try {

while ((line = rd.readLine()) != null) {

total.append(line);

}

} catch (Exception e) {

Toast.makeText(this, "Stream Exception", Toast.LENGTH_SHORT).show();

}

return total.toString();

}

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!

时间: 2024-11-03 03:28:08

Android: Client-Server communication by JSON的相关文章

Android—Camera Client/Server的binder IPC机制

本文首先参考Android Binder IPC分析一文分析了Android Binder IPC通信机制过程及涉及到的各个子元素相关概念,从代码细节脱离出来,因而整体上把握Android binder IPC通信机制,是能够理解文章最后Camera Framework进程间通信实现的基础.参考Android 4.4版本源码. Binder通信概述 Android进程间通信(Inter-Process Communication, IPC)采用binder通信机制,是一种client/serve

Android Client 与 C# Server 的Socket通信

C# Socket Server 1.建立C# SocketServer 1 private void ServerStart() 2          { 3              //创建IPEndPoint 4              IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1234); 5              //创建Socket实例 6              server_S

【Android】与服务器实现JSON数据通信

一.前言 作为一名移动端开发人员,具备一定的服务端开发能力也是非常必要的,本篇博客讲述如何在Android和服务器之间实现JSON数据通信交互,博客内容基于另外一篇博客:[Web]Eclipse + Maven + Struts搭建服务器. 二.服务器端改造 在博客[Web]Eclipse + Maven + Struts搭建服务器中,我们实现了服务器的搭建,现在要做的事情就是让它返回的数据是一个JSON格式的,这样在获得请求的时候,我们才可以得到JSON数据,其配置改变如下. 首先我们新建一个

1. Getting Started and Create an Android Client

1. Getting Started and Create an Android Client Retrofit tutorial 什么是 Retrofit 如何申明请求 准备 Android 项目 使用 Gradle 或 Maven 定义依赖 Retrofit 1.9 的依赖定义 pom.xml build.gradle Retrofit 2.0 的依赖定义 pom.xml build.gradle 持续发展的 Android 客户端 服务生成器 Retrofit 1.9 Retrofit 2

使用Volley来写一个List列表(Valley可以解决很大一部分android请求server的问题)

先上效果图: 先写一个Volley的请求的类: public void fetchData() { String url = "http://2.novelread.sinaapp.com/framework-sae/index.php"; // String body = ""; // try { // mEntity = new StringEntity(body); // } catch (UnsupportedEncodingException e1) {

Client–server model

Client–server model From Wikipedia, the free encyclopedia The client–server model of computing is a distributed application structure that partitions tasks or workloads between the providers of a resource or service, called servers, and service reque

Android与Server端的传输加密

1.必须找一个在Android和JDK上通用的加密算法,后面发现了http://www.cnblogs.com/hjtdlx/p/3926141.html这篇文章,试了一下,是可以用的. 2.Android和Server端的传输采用JSON格式,除了加密还要校验是否被修改.传输格式: {params:xxx,sign:xxx} 其中,params为经过DES3加密的json数据,sign为原json数据md5值.这样,在收到数据后先进行DES解密,然后将解密后的json数据进行MD5,和传过来的

android通过HttpClient与服务器JSON交互

通过昨天对HttpClient的学习,今天封装了HttpClient类 代码如下: package com.tp.soft.util; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.apach

QDjango,tufao,C++ websocket client/server

QDjango, a Qt-based C++ web frameworkhttps://github.com/jlaine/qdjango/ An asynchronous web framework for C++ built on top of Qt http://vinipsmaker.github.io/tufao/https://github.com/vinipsmaker/tufao C++ websocket client/server library http://www.za