Package com.ch.myutils; Import java.io.IOException; Import java.io.UnsupportedEncodingException; Import java.util.ArrayList; Import java.util.List; Import org.apache.http.HttpEntity; Import Org.apache.http.HttpResponse; Import Org.apache.http.NameValuePair; 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; Package com.ch.myutils; Import java.io.IOException; Import java.io.UnsupportedEncodingException; Import java.util.ArrayList; Import java.util.List; Import org.apache.http.HttpEntity; Import Org.apache.http.HttpResponse; Import Org.apache.http.NameValuePair; 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.impl.client.DefaultHttpClient; Import Org.apache.http.message.BasicNameValuePair; Import Org.apache.http.params.BasicHttpParams; Import Org.apache.http.params.HttpConnectionParams; Import Org.apache.http.params.HttpParams; Import Org.apache.http.util.EntityUtils; Import Android.content.Context; Import Android.net.ConnectivityManager; Import Android.net.NetworkInfo; public class Networkutil { public static final String Login_url = "Http://101.200.142.201:8080/tqyb/login"; public static Boolean isnetavailable (context context) { Get network Manager Connectivitymanager connm = (Connectivitymanager) Context.getsystemservice (Context.connectivity_service); Networkinfo netInfo = Connm.getactivenetworkinfo ();//Get network details if (NetInfo = = NULL | |!netinfo.isavailable ()) return false; return true; } public static string Logincheck_get_httpclient (string name,string pass,string URL) { Processing URLs via StringBuffer StringBuffer sb = new StringBuffer (URL); Sb.append ("Username="); Sb.append (name); Sb.append ("&userpass="); Sb.append (pass); String result = ""; HttpGet get = new HttpGet (sb.tostring ());//Create a HttpClient Get Request object Set Request parameters Httpparams params = new Basichttpparams (); Httpconnectionparams.setconnectiontimeout (params, 5*1000); Httpconnectionparams.setsotimeout (params, 5*1000); HttpClient client = new Defaulthttpclient (params);//Create HttpClient Object try { HttpResponse res = Client.execute (get);//execute request, get results if (Res.getstatusline (). Getstatuscode () = = 200) { httpentity entity = res.getentity ();//obtain the corresponding result, is a Httpentity object result = entityutils.tostring (entity, "utf-8");//Convert to String } } catch (Clientprotocolexception e) { TODO auto-generated Catch block E.printstacktrace (); } catch (IOException e) { TODO auto-generated Catch block E.printstacktrace (); } return result; } public static string Logincheck_post_httpclient (string name,string pass,string URL) { String result = ""; HttpPost post = new HttpPost (URL);//Create a httpclient Post request object Set Request parameters Httpparams params = new Basichttpparams (); Httpconnectionparams.setconnectiontimeout (params, 5*1000); Httpconnectionparams.setsotimeout (params, 5*1000); Pass Value Create a list collection that holds the data passed to the server list<namevaluepair> reqdata = new arraylist<namevaluepair> (); Namevaluepair P1 = new Basicnamevaluepair ("username", name); Namevaluepair P2 = new Basicnamevaluepair ("Userpass", pass); Reqdata.add (p1); Reqdata.add (p2); try { httpentity entity = new Urlencodedformentity (reqdata, "utf-8"); Post.setentity (entity);//to the POST request object, set the uploaded data HttpClient client = new Defaulthttpclient (params);//Create HttpClient Object HttpResponse res = Client.execute (post);//perform POST request to get results if (Res.getstatusline (). Getstatuscode () = = 200) { Httpentity resentity = res.getentity ();//obtain the corresponding result, is a Httpentity object result = Entityutils.tostring (resentity, "utf-8");//Convert to String } } catch (Unsupportedencodingexception E1) { TODO auto-generated Catch block E1.printstacktrace (); } catch (Clientprotocolexception e) { TODO auto-generated Catch block E.printstacktrace (); } catch (IOException e) { TODO auto-generated Catch block E.printstacktrace (); } return result; } } |