The first part of this series WCF and Android Part I describes how to setup a JSON REST WCF service. This post shows how to consume this service using Android.
Android already comes with a ready to use HTTP-client located in the package org.apache.http.client.
Collapse | Copy Code HttpParams httpParams = new BasicHttpParams(); ThreadSafeClientConnManager connMgr = new ThreadSafeClientConnManager(httpParams, new SchemeRegistry()); HttpClient client = new DefaultHttpClient(connMgr, httpParams);Therefore a simple webservice invocation looks as follows:
Collapse | Copy Code HttpGet get = new HttpGet("http://requesturi/method"); HttpResponse response = client.execute(get); HttpEntity entity = response.getEntity();Simple parameters can be passed in the URL but more complex objects are likely passed in the http body using the POST method. For modern and fast webservices it is common to use JSON for data serialization. Android has some JSON features built in but Google GSON provides a very flexible way to serialize and deserialize JSON objects, even for arbitrary object types. Download the library and include it in your build-path. The first step is to create a GsonBuilder, where some de-/serialization options can be specified.
Collapse | Copy Code GsonBuilder gson = new GsonBuilder() .registerTypeAdapter(Date.class, new DateSerializer()) .serializeNulls() .setDateFormat(DateFormat.LONG) .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE) .setPrettyPrinting() .create();Beside some others this example attaches a custom DateSerializer (thanks to Benjii Me) to be able to read WCF serialized DateTime values. That's all you need to do. Serialization now looks as follows:
Collapse | Copy Code String jsonString = gson.toJson(myObject);and deserialization very similar:
Collapse | Copy Code MyObjectType myObj = gson.fromJson(jsonString, MyObjectType.class);Every webservice requests only allows a single JSON object in the HTTP-body, therefore if multiple arguments are required they need to be wrapped in a single JSON object and the WCF service contract needs to be aware of it (BodyStyle=WebMessageBodyStyle.WrappedRequest).
Once the webservice is aware of the BodyStyle the client side is quite straight forward. Create a Map from name to object, add all objects with the name of the webservice parameter and serialize the whole map:
Collapse | Copy Code HashMap<String, Object> postParameters = new HashMap<String, Object>(); postParameters.put("param1", objParam1); postParameters.put("param2", objParam2); postParameters.put("param3", objParam3); gson.toJson(postParameters);A complete webservice call looks as follows:
Collapse | Copy Code HttpPost post = new HttpPost(requestUri); post.setHeader("Accept", "application/json"); post.setHeader("Content-type", "application/json"); String jsonParameters = gson.toJson(postParameters); post.setEntity(new StringEntity(jsonParameters)); HttpResponse response = client.execute(post); HttpEntity entity = response.getEntity();Assuming that the webservice was called successful a HttpEntity is returned. This entity can be easily converted to the corresponding object as shown above. The only difficulty is to convert the HttpEntity to a JSON string:
Collapse | Copy Code gson.fromJson(convertStreamToString(entity.getContent()), MyObjectType.class); public static String convertStreamToString(InputStream is) { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; try { while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } } catch (IOException e) { e.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); }原文:http://www.codeproject.com/Articles/361107/WCF-and-Android-Part-II?msg=4358846#xx4358846xx转载于:https://www.cnblogs.com/ttssrs/archive/2012/09/06/2674045.html
相关资源:JAVA上百实例源码以及开源项目