This chapter aims to implement the WCF Data Service created in the previous article on Android client requests.
This part is divided into creating an Http request and accepting data returned by WCF.
1. Create an Http Request Method
Protected String getRequest (String url, DefaultHttpClient client) <br/> throws Exception {<br/> String result = null; <br/> int statusCode = 0; <br/> HttpGet getMethod = new HttpGet (url); <br/> Log. d (TAG, "do the getRequest, url =" + url + ""); <br/> try {<br/> getMethod. setHeader ("User-Agent", USER_AGENT); <br/> // HttpParams params = new HttpParams (); </p> <p> // Add User Password Verification Information <br/> // client. getCredentialsProvider (). setCredentials (<br/> // new AuthScope (null,-1), <br/> // new UsernamePasswordCredentials (mUsername, mPassword )); </p> <p> HttpResponse httpResponse = client.exe cute (getMethod); <br/> // normal statusCode = 200 <br/> statusCode = httpResponse. getStatusLine (). getStatusCode (); <br/> Log. d (TAG, "statuscode =" + statusCode); <br/> // process returned httpResponse Information <br/> result = retrieveInputStream (httpResponse. getEntity (); <br/>}catch (Exception e) {<br/> Log. e (TAG, e. getMessage (); <br/> throw new Exception (e); <br/>}finally {<br/> getMethod. abort (); <br/>}< br/> return result; <br/>}< br/>
Parameter URL: The address we want to request
Client: You can use new DefaultHttpClient (new BasicHttpParams () to initialize the Client.
In this method, pay attention to the RetrieveInputStream method. This method is used to process the data returned by the server after the Http request is complete,
Ii. Accept data transmitted from the WCF end
Protected String retrieveInputStream (HttpEntity httpEntity) {<br/> int length = (int) httpEntity. getContentLength (); <br/> if (length <0) <br/> length = 10000; <br/> StringBuffer stringBuffer = new StringBuffer (length ); <br/> try {<br/> InputStreamReader inputStreamReader = new InputStreamReader (<br/> httpEntity. getContent (), HTTP. UTF_8); <br/> char buffer [] = new char [length]; <br/> int count; <br/> while (count = inputStreamReader. read (buffer, 0, length-1)> 0) {<br/> stringBuffer. append (buffer, 0, count); <br/>}< br/>} catch (UnsupportedEncodingException e) {<br/> Log. e (TAG, e. getMessage (); <br/>} catch (IllegalStateException e) {<br/> Log. e (TAG, e. getMessage (); <br/>}catch (IOException e) {<br/> Log. e (TAG, e. getMessage (); <br/>}< br/> return stringBuffer. toString (); <br/>}< br/>
This method returns the String type after receiving the data returned by the WCF server.
Additional content:
Private static final String BASE_URL = "http: // 10.0.2.2: 82/BlogCategoryService/"; <br/> private static final String EXTENSION = "Json /";; <br/> private static final String TAG = "API"; <br/> private static final String USER_AGENT = "Mozilla/4.5 "; </p> <p> public JSONObject getObject (String sbj) throws JSONException, Exception {<br/> return new JSONObject (getRequest (BASE_URL + EXTENSION + sbj )); <br/>}</p> <p> public JSONArray getArray (String sbj) throws JSONException, <br/> Exception {<br/> return new JSONArray (getRequest (BASE_URL + EXTENSION + sbj )); <br/>}</p> <p> protected String getRequest (String url) throws Exception {<br/> return getRequest (url, new DefaultHttpClient (new BasicHttpParams (); <br/>}< br/>
Encapsulation Method before data request:
Summary: This article mainly describes the two phases of the Http request, establishing a request and receiving the data returned by the server, and then describes how to process the JSON data returned by the server and display the data on the UI.
This chapter aims to implement the WCF Data Service created in the previous article on Android client requests.
This part is divided into creating an Http request and accepting data returned by WCF.
1. Create an Http Request Method
Protected String getRequest (String url, DefaultHttpClient client) <br/> throws Exception {<br/> String result = null; <br/> int statusCode = 0; <br/> HttpGet getMethod = new HttpGet (url); <br/> Log. d (TAG, "do the getRequest, url =" + url + ""); <br/> try {<br/> getMethod. setHeader ("User-Agent", USER_AGENT); <br/> // HttpParams params = new HttpParams (); </p> <p> // Add User Password Verification Information <br/> // client. getCredentialsProvider (). setCredentials (<br/> // new AuthScope (null,-1), <br/> // new UsernamePasswordCredentials (mUsername, mPassword )); </p> <p> HttpResponse httpResponse = client.exe cute (getMethod); <br/> // normal statusCode = 200 <br/> statusCode = httpResponse. getStatusLine (). getStatusCode (); <br/> Log. d (TAG, "statuscode =" + statusCode); <br/> // process returned httpResponse Information <br/> result = retrieveInputStream (httpResponse. getEntity (); <br/>}catch (Exception e) {<br/> Log. e (TAG, e. getMessage (); <br/> throw new Exception (e); <br/>}finally {<br/> getMethod. abort (); <br/>}< br/> return result; <br/>}< br/>
Parameter URL: The address we want to request
Client: You can use new DefaultHttpClient (new BasicHttpParams () to initialize the Client.
In this method, pay attention to the RetrieveInputStream method. This method is used to process the data returned by the server after the Http request is complete,
Ii. Accept data transmitted from the WCF end
Protected String retrieveInputStream (HttpEntity httpEntity) {<br/> int length = (int) httpEntity. getContentLength (); <br/> if (length <0) <br/> length = 10000; <br/> StringBuffer stringBuffer = new StringBuffer (length ); <br/> try {<br/> InputStreamReader inputStreamReader = new InputStreamReader (<br/> httpEntity. getContent (), HTTP. UTF_8); <br/> char buffer [] = new char [length]; <br/> int count; <br/> while (count = inputStreamReader. read (buffer, 0, length-1)> 0) {<br/> stringBuffer. append (buffer, 0, count); <br/>}< br/>} catch (UnsupportedEncodingException e) {<br/> Log. e (TAG, e. getMessage (); <br/>} catch (IllegalStateException e) {<br/> Log. e (TAG, e. getMessage (); <br/>}catch (IOException e) {<br/> Log. e (TAG, e. getMessage (); <br/>}< br/> return stringBuffer. toString (); <br/>}< br/>
This method returns the String type after receiving the data returned by the WCF server.
Additional content:
Private static final String BASE_URL = "http: // 10.0.2.2: 82/BlogCategoryService/"; <br/> private static final String EXTENSION = "Json /";; <br/> private static final String TAG = "API"; <br/> private static final String USER_AGENT = "Mozilla/4.5 "; </p> <p> public JSONObject getObject (String sbj) throws JSONException, Exception {<br/> return new JSONObject (getRequest (BASE_URL + EXTENSION + sbj )); <br/>}</p> <p> public JSONArray getArray (String sbj) throws JSONException, <br/> Exception {<br/> return new JSONArray (getRequest (BASE_URL + EXTENSION + sbj )); <br/>}</p> <p> protected String getRequest (String url) throws Exception {<br/> return getRequest (url, new DefaultHttpClient (new BasicHttpParams (); <br/>}< br/>
Encapsulation Method before data request:
Summary: This article mainly describes the two phases of the Http request, establishing a request and receiving the data returned by the server, and then describes how to process the JSON data returned by the server and display the data on the UI.