Home > Backend Development > PHP Tutorial > Weibo development 1 client http get and post packaging

Weibo development 1 client http get and post packaging

WBOY
Release: 2016-08-08 09:31:59
Original
909 people have browsed it

This blog talks about how the client encapsulates the Http protocol, how the client uses the post and get methods, which is one of the most core codes

The following is excerpted from Huang Shijun'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 timeoutConnection = 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; Built it myself Inside a 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/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= "/notify/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 blogView= 1007;
public static final int blogCreate= 1008;
public static final int commentList= 1009;
public static final int commentCreate= 1010;
public static final int customerView= 1011;
public static final int customerEdit= 1012;
public static final int fansAdd= 1013;
public static final int fansDel= 1014;
public static final int notice= 1015;
}

public static final class err {
public static final String network= "网络错误";
public static final String message= "消息错误";
public static final String jsonFormat= "消息格式错误";
}

////////////////////////////////////////////////////////////////////////////////////////////////
// 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"; apiSid =
AppUtil.getSessionId(); null && apiSid.length() > 0) {
this.apiUrl += "?sid=" + apiSid;

}

// set client timeouthttpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams, timeoutConnection);
HttpConnectionParams.setSoTimeout(httpParams, timeoutSocket);
// init client
httpClient = new DefaultHttp Client(httpParams);
                                                                                                               It is simple to get Httpclient
}

public void useWap () {                                 //Here is the way to choose wap to access the Internet

HttpHost proxy = new HttpHost("10.0.0.172", 80, "http");

httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy ; ;

Log.w("AppClient.get.url", this.apiUrl);

// send get request
HttpResponse httpResponse = httpClient.execute(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 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 .execute(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 request to send get data to see if it is in 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 request to send post data to see if it is Compressed format iswitch (this.com.compress) {
case CS_GZIP:
Httppost.addheader ("Accept-Entoding", "gzip");
Default:
break;
}
return httpPost;
}

private String resultFilter(HttpEntity entity){ Process the obtained data to see if it is in compressed format

Str ing result = null; try {
switch (this.compress) {
case CS_GZIP:
result = AppUtil.gzipToString(entity);
break;
default :
result = EntityUtils. toString(entity);
break;
}
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
} The above introduces the HTTP get and post packages of Weibo Development 1 client, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template