First, the classes necessary to access the network through the Apache interface
HttpClient
Overview
---HttpClient is a sub-project under Apache Jakarta Common that can be used to provide efficient, up-to-date, feature-rich client programming toolkits that support the HTTP protocol, and it supports the latest versions and recommendations of the HTTP protocol. (Excerpt from Baidu Encyclopedia)
Member variables
Construction method
---defaulthttpclient ()
Common methods
---execute ()//sends the request, requires an object representing the request method as a parameter, and returns a HttpResponse object that encapsulates the response information
Static methods
HttpGet
Overview
---If the object is used as a parameter when HttpClient sends a request, it indicates that a GET request is sent, which is typically used to get/query resource information
The data---GET request is appended to the URL (that is, placing the data in the HTTP protocol header), splitting the URL and transmitting the data, and connecting the parameters with &, such as: Login.action?name=hyddd&password =IDONTKNOW&VERIFY=%E4%BD%A0%E5%A5%BD. If the data is an English letter/number, sent as is, if it is a space, converted to +, if it is Chinese/other characters, the string is directly encrypted with BASE64, such as:%E4%BD%A0%E5%A5%BD, where the xx in%xx is the symbol in 16 binary notation ASCII.
Member variables
Construction method
---httpget ()//construct a GET request that requires a Web address as a parameter
Common methods
Static methods
HttpResponse
Overview
---The class encapsulates the information returned after initiating an HTTP request
Member variables
Construction method
---httpclient.execute ()//The client sends the request after the object instance is returned
Common methods
---getstatusline ()//Obtain an object that encapsulates the status information for the server response request
---getstatuscode ()//Get "Server response code" through the above object
---getentity ()//Get the Server response entity
Static methods
HttpPost
Overview
---If the object is used as a parameter when HttpClient sends a request, the POST request is sent, which is typically used to update resource information
--- Post puts the submitted data in the package body of the HTTP packet, which is the httpentity
Member variables
Construction method
---httppost ()//construct a GET request that requires a Web address as a parameter
Common methods
---setentity (Params)//Set Request parameters
Static methods
Namevaluepair
Overview
---Sending a httppost request requires encapsulating the request parameter through the object, a parameter that represents a key-value pair form
Member variables
Construction method
------New Basicnamevaluepair (Key,value)
Common methods
Static methods
Httpentity
Overview
---This class is used to encapsulate the data flow in the client-to-server communication process
Member variables
Construction method
---httpresponse.getentity ()
---New urlencodedformentity (params, "utf-8");//encapsulates the parameters and character set encoding of the POST request, and parameter 1 is the holder the container for the namevaluepair parameter object, which is the character set encoding
Common methods
---getcontent ()//get the data stream returned by the server
Static methods
Note
---entityutils.tostring (httpentity)//This method can directly parse the data stream encapsulated in a Httpentity object into string data
Summarize
---GET
----1, first we need a client to connect to the server, New Defaulthttpclient (), 2, and then we need to tell the client which file to access the server and in which way to access the new HttpGet (URL). 3, can now connect and access the server, Httpclient.execute (httpget), the server will return its response information, the request method will automatically encapsulate the information into HttpResponse and return. The data has been received, so there is no server and client eggs.
---POST
----Ibid, httpget nature is to change into HttpPost. If you need to pass parameters to the server: first construct the object that encapsulates the parameter, new Basicnamevaluepair (Key,value), an object can only hold a set of key-value pairs. The object is then saved to the container, new arraylist<namevaluepair> (). Add (Parameter object), then encapsulates the container into httpentity, new new Urlencodedformentity (Container object, "Utf-8"). Finally, httppost the appearance of them, Httppost.setentity (httpentity).
Second, access the network instance through the Apache interface
1, first put a Web file to be accessed in your server
<% @page language= "java" import= "java.util.*" pageencoding= "Utf-8"%>
<title>
Http Test
</title>
<body>
<%
String type = request.getparameter ("parameter");
string result = new String (Type.getbytes ("iso-8859-1"), "Utf-8");
Out.println ("%>
</body>
2,android-Side code
public class Mainactivity extends Activity {
Handler han=new Handler () {
public void Handlemessage (Android.os.Message msg) {
Switch (msg.what) {
Case 1:
Tv.settext (Msg.obj.toString ());
Break
Case 2:
Tv.settext (Msg.obj.toString ());
Break
}
}
};
TextView TV;
EditText et;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
tv= (TextView) Findviewbyid (R.ID.T1);
et= (EditText) Findviewbyid (r.id.et);
}
public void Get (view view) {
String Param=et.gettext (). toString ();
if (Textutils.isempty (param)) {
Param= "Empty";
}
New Th1 (param);
}
public void post (view view) {
String Param=et.gettext (). toString ();
if (Textutils.isempty (param)) {
Param= "Empty";
}
New Th2 (param);
}
Class Th1 extends thread{
String params= "";
Public Th1 (String PA) {
THIS.PARAMS=PA;
This.start ();
}
@Override
public void Run () {
TODO auto-generated Method Stub
Super.run ();
HttpClient client=new defaulthttpclient ();
HttpGet get=new httpget ("http://11.67.0.58:8080/nihao/map.jsp?parameter=" +params);
try {
Send request and get response information
HttpResponse Request=client.execute (GET);
Determine if the request was successful
if (Request.getstatusline (). Getstatuscode () ==HTTPSTATUS.SC_OK) {
Get response entity after successful request
Httpentity entity=request.getentity ();
Parsing the data flow returned by the server
InputStream is=entity.getcontent ();
BufferedReader br=new BufferedReader (New InputStreamReader (IS));
StringBuffer sb=new StringBuffer ();
String dataline= "";
while ((Dataline=br.readline ())!=null) {
Sb.append (Dataline);
}
Message Msg=han.obtainmessage ();
Msg.what=1;
Msg.obj=sb.tostring ();
Han.sendmessage (msg);
Br.close ();
}
} catch (Exception e) {
TODO auto-generated Catch block
System.out.println (E.getmessage ());
}
}
}
Class Th2 extends thread{
String param= "";
Public Th2 (String PA) {
PARAM=PA;
This.start ();
}
@Override
public void Run () {
TODO auto-generated Method Stub
Super.run ();
HttpClient client=new defaulthttpclient ();
HttpPost post=new httppost ("http://11.67.0.58:8080/nihao/map.jsp");
Constructing Parameter objects
List<namevaluepair> params=new arraylist<namevaluepair> ();
Params.add (New Basicnamevaluepair ("parameter", param));
Httpentity entity;
try {
entity = new Urlencodedformentity (params, "utf-8");
Post.setentity (entity);
HttpResponse Response=client.execute (POST);
if (Response.getstatusline (). Getstatuscode () ==HTTPSTATUS.SC_OK) {
InputStream is=entity.getcontent ();
BufferedReader br=new BufferedReader (New InputStreamReader (IS));
StringBuffer sb=new StringBuffer ();
String dataline= "";
while ((Dataline=br.readline ())!=null) {
Sb.append (Dataline);
}
Message Msg=han.obtainmessage ();
msg.what=2;
Msg.obj=sb.tostring ();
Han.sendmessage (msg);
Br.close ();
}
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
}
Apache Interface for Android network programming