httpclient interacts with server data: HttpPost and HttpGet correspond to post and get submissions, respectively. Because to do the Android client's sake, it is necessary to implement the client and the server to achieve data interaction, to ensure the smooth data chain, the realization of closed-loop data. Because of the previous access to the Web data to the Android client does not have permissions to access the system resources, but if it is the development of applications this way there is a great security risk, a host or get submit the past data is obtained, the web-side system is completely bare-Ben. So web-side rights management is necessary, there is no need to worry too much about the security of the Android client. Android Novice learning generally encounter problems, the first two days have encountered the Web-side set permissions and the Android client initiated access to get no data issues.
Get JSON encountered permissions issue: http://my.oschina.net/boonya/blog/317057 This article is somewhat related to what this article is about.
1.HttpPost and HttpGet for user login and list display
Write a Test activity,mainactivity.
Package Com.boonya.httpclienttest;import Java.io.bufferedreader;import Java.io.ioexception;import Java.io.inputstreamreader;import Java.io.unsupportedencodingexception;import Java.util.ArrayList;import Java.util.hashmap;import Java.util.list;import Org.apache.http.httpresponse;import Org.apache.http.client.clientprotocolexception;import Org.apache.http.client.httpclient;import Org.apache.http.client.entity.urlencodedformentity;import Org.apache.http.client.methods.httpget;import Org.apache.http.client.methods.httppost;import Org.apache.http.message.basicnamevaluepair;import Org.apache.http.protocol.http;import Org.json.jsonarray;import Org.json.jsonobject;import Com.boonya.httpclienttest.utils.htttpclientutil;import Android.app.activity;import Android.os.Bundle;import Android.util.log;import Android.widget.listview;import Android.widget.simpleadapter;import android.widget.Toast; public class Mainactivity extends activity{private static final String TAG = "mainactivity";p rivate list2. Implement client and server cookie sharingThe singleton mode is used to realize the uniqueness of the HttpClient object in the Android client, when the user logs on to the system, the object will record the user's cookie, and once the client and server authentication is established, the HTTP request can be sent to the server request or Operation resource.
Package Com.boonya.httpclienttest.utils;import Org.apache.http.client.httpclient;import Org.apache.http.impl.client.defaulthttpclient;import Org.apache.http.params.basichttpparams;import Org.apache.http.params.httpconnectionparams;public class htttpclientutil{/** Set request timeout 10 seconds */private static final int Request_timeout = 10 * 1000;/** Set wait data time-out time 10 seconds */private static final int so_timeout = ten * 1000;private static HttpClient instance;/** Remember cookie string */private static string cookie;/** * Custom method: Initialize HttpClient and set timeout * * @return return: HttpClient object */p Rivate Htttpclientutil () {}public static String GetCookie () {return cookie;} public static void Setcookie (String cookie) {Htttpclientutil.cookie = cookie;} public static HttpClient getinstance () {if (instance = = null) {Basichttpparams httpparams = new Basichttpparams (); Httpconnectionparams.setconnectiontimeout (Httpparams, request_timeout); Httpconnectionparams.setsotimeout (Httpparams, so_timeout); instance = new Defaulthttpclient (httpparams);} return instance;}}
The above cookie field is not required, and if HttpClient is not a singleton, then the user needs to remember the login cookie in the static variable. Optimization of Httpclientutil Cookie acquisition method:
Package Com.boonya.httpclienttest.utils;import Java.util.list;import Org.apache.http.client.httpclient;import Org.apache.http.cookie.cookie;import Org.apache.http.impl.client.abstracthttpclient;import Org.apache.http.impl.client.defaulthttpclient;import Org.apache.http.params.basichttpparams;import Org.apache.http.params.httpconnectionparams;public class htttpclientutil{/** Set request timeout 10 seconds */private static final int Request_timeout = 10 * 1000;/** Set wait data time-out time 10 seconds */private static final int so_timeout = ten * 1000;private static HttpClient instance;/** Remember cookie string */private static string cookie = null;/** * Custom method: Initialize HttpClient and set timeout * * @return return: HttpClient Object */private Htttpclientutil () {}public static String GetCookie () {//ensures that the instance exists htttpclientutil.getinstance ();// Get cookielist<cookie> cookies = ((abstracthttpclient) instance). Getcookiestore (). GetCookies (); if (cookies! = Null && cookies.size () > 0) {for (int i = 0; i < cookies.size (); i++) {cookie = Cookies.get (i). GetValue ();}}return cookies;} public static HttpClient getinstance () {if (instance = = null) {Basichttpparams httpparams = new Basichttpparams (); Httpconnectionparams.setconnectiontimeout (Httpparams, request_timeout); Httpconnectionparams.setsotimeout (Httpparams, so_timeout); instance = new Defaulthttpclient (httpparams);} return instance;}}
Android Client uses HttpClient to initiate web data access