Http get and post encapsulation of the client in Weibo Development 1

Source: Internet
Author: User
: This article mainly introduces the http get and post encapsulation of the Weibo development 1 client. For more information about PHP tutorials, see. This blog is about how the client encapsulates the Http protocol, and how the client uses the post and get methods, which is one of the core code.

The following is an excerpt from Huangshi Jun's android and PHP development.

Package com. app. demos. util;


Import java. io. IOException;
Import java. util. ArrayList;
Import java. util. HashMap;
Import java. util. Iterator;
Import java. util. List;
Import java. util. Map;
Import org. apache. http. HttpEntity;
Import org. apache. http. HttpHost;
Import org. apache. http. HttpResponse;
Import org. apache. http. HttpStatus;
Import org. apache. http. NameValuePair;
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. conn. ConnectTimeoutException;
Import org. apache. http. conn. params. ConnRoutePNames;
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. protocol. HTTP;
Import org. apache. http. util. EntityUtils;
Import com. app. demos. base. C;
Import android. util. Log;
@ SuppressWarnings ("rawtypes ")
Public class AppClient {

// Compress strategy
Final private static int CS_NONE = 0;
Final private static int CS_GZIP = 1;

// Logic variables
Private String apiUrl;
Private HttpParams httpParams;
Private HttpClient httpClient;
Private int timeoutconnections = 10000;
Private int timeoutSocket = 10000;
Private int compress = CS_NONE;

// Charset default utf8
Private String charset = HTTP. UTF_8;

Public AppClient (String url ){
InitClient (url );
}

Public AppClient (String url, String charset, int compress ){
InitClient (url );
This. charset = charset; // set charset
This. compress = compress; // set strategy
}

Private void initClient (String url ){
// Initialize API URL

This. apiUrl = C. api. base + url;

// C. api. base c is a self-built Class.

Package com. app. demos. base;
Public final class C {

//////////////////////////////////////// //////////////////////////////////////// ////////////////
// Core settings (important)

Public static final class dir {
Public static final String base = "/sdcard/demos ";
Public static final String faces = base + "/faces ";
Public static final String images = base + "/images ";
}

Public static final class api {
Public static final String base = "http: // 192.168.1.2: 8001 ";
Public static final String index = "/index ";
Public static final String login = "/index/login ";
Public static final String logout = "/index/logout ";
Public static final String faceView = "/image/faceView ";
Public static final String faceList = "/image/faceList ";
Public static final String blogList = "/blog/blogList ";
Public static final String blogView = "/blog/blogView ";
Public static final String blogCreate = "/blog/blogCreate ";
Public static final String commentList = "/comment/commentList ";
Public static final String commentCreate = "/comment/commentCreate ";
Public static final String customerView = "/customer/customerView ";
Public static final String customerEdit = "/customer/customerEdit ";
Public static final String fansAdd = "/customer/fansAdd ";
Public static final String fansDel = "/customer/fansDel ";
Public static final String notice = "/Y/notice ";
}

Public static final class task {
Public static final int index = 1001;
Public static final int login= 1002;
Public static final int logout = 1003;
Public static final int faceView = 1004;
Public static final int faceList = 1005;
Public static final int (blogList = 1006;
Public static final int blogviews = 1007;
Public static final int blogcreation = 1008;
Public static final int commentList = 1009;
Public static final int commentCreate = 1010;
Public static final int customerView = 1011;
Public static final int customeredits = 1012;
Public static final int maid = 1013;
Public static final int fansDel = 1014;
Public static final int notice = 1015;
}

Public static final class err {
Public static final String network = "network error ";
Public static final String message = "message Error ";
Public static final String jsonFormat = "Message Format error ";
}

//////////////////////////////////////// //////////////////////////////////////// ////////////////
// Intent & action settings

Public static final class intent {
Public static final class action {
Public static final String EDITTEXT = "com. app. demos. EDITTEXT ";
Public static final String EDITBLOG = "com. app. demos. EDITBLOG ";
}
}

Public static final class action {
Public static final class edittext {
Public static final int CONFIG = 2001;
Public static final int COMMENT = 2002;
}
}

//////////////////////////////////////// //////////////////////////////////////// ////////////////
// Additional settings

Public static final class web {
Public static final String base = "http: // 192.168.1.2: 8002 ";
Public static final String index = base + "/index. php ";
Public static final String gomap = base + "/gomap. php ";
}
}

String apiSid = AppUtil. getSessionId (); // AppUtil is a self-compiled tool class that encapsulates some basic usage such as getting user sessions, encryption, and gzip compression.
If (apiSid! = Null & apiSid. length ()> 0 ){
This. apiUrl + = "? Sid = "+ apiSid;
}
// Set client timeout
HttpParams = new BasicHttpParams ();
HttpConnectionParams. setConnectionTimeout (httpParams, timeoutConnection );
HttpConnectionParams. setSoTimeout (httpParams, timeoutSocket );
// Init client

HttpClient = new DefaultHttpClient (httpParams );

// Here is a simple way to get Httpclient

}

Public void useWap () {// select the wap method for accessing the Internet.
HttpHost proxy = new HttpHost ("10.0.0.172", 80, "http ");
HttpClient. getParams (). setParameter (ConnRoutePNames. DEFAULT_PROXY, proxy );
}

Public String get () throws Exception {// encapsulate the Http get method
Try {
HttpGet httpGet = headerFilter (new HttpGet (this. apiUrl ));
Log. w ("AppClient. get. url", this. apiUrl );
// Send get request
HttpResponse httpResponse = httpClient.exe cute (httpGet );
If (httpResponse. getStatusLine (). getStatusCode () = HttpStatus. SC _ OK ){
String httpResult = resultFilter (httpResponse. getEntity ());
Log. w ("AppClient. get. result", httpResult );
Return httpResult;
} Else {
Return null;
}
} Catch (ConnectTimeoutException e ){
Throw new Exception (C. err. network );
} Catch (Exception e ){
E. printStackTrace ();
}
Return null;
}

Public String post (HashMap urlParams) throws Exception {// encapsulate the Http post method
Try {
HttpPost httpPost = headerFilter (new HttpPost (this. apiUrl ));
List PostParams = new ArrayList ();
// Get post parameters
Iterator it = urlParams. entrySet (). iterator ();
While (it. hasNext ()){
Map. Entry entry = (Map. Entry) it. next ();
PostParams. add (new BasicNameValuePair (entry. getKey (). toString (), entry. getValue (). toString ()));
}
// Set data charset
If (this. charset! = Null ){
HttpPost. setEntity (new UrlEncodedFormEntity (postParams, this. charset ));
} Else {
HttpPost. setEntity (new UrlEncodedFormEntity (postParams ));
}
Log. w ("AppClient. post. url", this. apiUrl );
Log. w ("AppClient. post. data", postParams. toString ());
// Send post request
HttpResponse httpResponse = httpClient.exe cute (httpPost );
If (httpResponse. getStatusLine (). getStatusCode () = HttpStatus. SC _ OK ){
String httpResult = resultFilter (httpResponse. getEntity ());
Log. w ("AppClient. post. result", httpResult );
Return httpResult;
} Else {
Return null;
}
} Catch (ConnectTimeoutException e ){
Throw new Exception (C. err. network );
} Catch (Exception e ){
E. printStackTrace ();
}
Return null;
}

Private HttpGet headerFilter (HttpGet httpGet) {process the get data request to see if it is in a compressed format
Switch (this. compress ){
Case CS_GZIP:
HttpGet. addHeader ("Accept-Encoding", "gzip ");
Break;
Default:
Break;
}
Return httpGet;
}

Private HttpPost headerFilter (HttpPost httpPost) {process the post data request to see if it is in the compression format
Switch (this. compress ){
Case CS_GZIP:
HttpPost. addHeader ("Accept-Encoding", "gzip ");
Break;
Default:
Break;
}
Return httpPost;
}

Private String resultFilter (HttpEntity entity) {process the obtained data to see if it is in a compressed format
String result = null;
Try {
Switch (this. compress ){
Case CS_GZIP:
Result = AppUtil.gzip ToString (entity );
Break;
Default:
Result = EntityUtils. toString (entity );
Break;
}
} Catch (IOException e ){
E. printStackTrace ();
}
Return result;
}

}

The above describes the http get and post encapsulation of the Weibo development 1 client, including the content, and hope to be helpful to friends who are interested in PHP tutorials.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.