微博開發1客戶端的http的get與post封裝

WBOY
發布: 2016-08-08 09:31:59
原創
846 人瀏覽過

這篇部落格講的是客戶端如何封裝Http協議,客戶端如何使用post,get方法,是最最核心的程式碼之一

以下摘自黃石君的《android與PHP開發》

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; util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatimache. http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpacheGet; http.client.methods.HttpPost;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.params.ConnRoaultPNames;
import org.apache.http.impl. org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
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@SuppressWarnings("rawtypes")
public@SuppressWarnings("rawtypes")
public@SuppressWarnings("rawtypes")
public@SuppressWarnings("rawtypes")
public@SuppressWarnings("rawtypes")
public@SuppressWarnings("rawtypes") class AppClient {

// compress strategy
final private static int CS_NONE = 0;finalfinal private stat int
// logic variables
private String apiUrl;
private HttpParams httpParams;
private HttpClient httpClient;
private int timeoutConnection = 100000; private int compress = CS_NONE;

/ / charset default utf8
private String charset = HTTP.UTF_8;

public AppClient (String Cl
public AppClient (String url, String charset, int compress) {
initClient(url);
this.charset = charset; // set charset
this。
private void initClient (String url) {
// initialize API URL
this.apiUrl = C.api.base + url;  
this.apiUrl = C.api.base + url;   this.apiUrl = C.api.base + url;  
this.apiUrl = C.api.base + url; ? c.api.base   c是自己建造的一個類別裡面

            package com.app.demos.base;
           公開最終課程C {

   ////////////////////////// / ///////////////////////////////////////////////// ////////// ////////////////////
    // 核心設定(重要)

   publicstatstatic Final class dir {
public static final String base = "/sdcard/demos";
公共靜態最終字串面孔= 基礎+ "/faces";公共靜態最終字符串面孔
= 基礎+ "/faces"; = 基礎+ "/images";}

公共靜態最終類API {
公共靜態最終字串基礎
=“http://192.168. 公用靜態最終字串索引=“/index/ index";
public static final String 登入= "/index/login";
public static Final Final String Final Final. logout";
public static final String faceView = "/image/faceView";
公共靜態最終字符串faceList=“/image/ListList”;公共靜態最終字符串faceList=“/image/ListList”;
=“/blog/blogList”;公共靜態最終字串blogView
= "/blog/blogView";public static final String blogCreate
public static final String blogCreate public static final String commentList
= "/comment/commentList"; public static final String commentCreate
= "/comment/commentCreate"; " ;public static final String customerEdit
= "/customer/customerEdit" ;公共靜態最終字串fansAdd
=“/customer/fansAdds”; “/customer/fansDel”;公共靜態最終字串通知
=“/通知/通知";}

= 1001;公共靜態最終int登入
= 1002; 公共靜態最終int 註銷面
= 1003;公共靜態最終int 註銷面= 1003;
公共靜態最終int 面列表
= 1005;
公共靜態最終int blogList= 1006 ;
public static final int blog = 1008;
public static final int commentList= 1009;
public static final int commentCreate =1010 ;
= 1012; 公共靜態最終粉絲添加
= 1013;公共靜態最終粉絲D埃爾
= 1014;
公用靜態最終類別錯誤{公用靜態最終字串網路
= "網路錯誤"; public static final String message
= "訊息錯誤" 是public static final String message= "訊息錯誤"; = "訊息格式錯誤";
}
////////// //////////////////////// //////////////////////////////////// /////////////// ///////////////////////
//意圖和操作設置
公共靜態最終類意圖{
公共靜態最終類別操作{公共靜態最終字串EDITTEXT
=“com.app.demos.EDITTEXT”;
公共靜態最終字串EDITBLOG=“ com.app.demos.de. }
}

public static tinal Class Action {公共靜態最終類EDITTEXT {inal final f MMENT
= 2002;}
}

//////////////////////////////////////////// ////////////////////////////////////////////////// //
// additional settings

public static final class web {
public static final String public. static final String index
= base + "/index.php";public static final String gomap
= base + "/gomap.php";}
String apiSid = AppUtil
.getSessionId(); null && apiSid.length() > 0) {

this.apiUrl += "?sid=" + apiSid;

}

// set client timeout HttpConnectionParams.setConnectionTimeout(httpParams, timeoutConnection);HttpConnectionParams.setSoTimeout(httpParams, timeoutSocket);
s);               //
此處是簡單的取得Httpclient
}

public void useWap () {                //此處是選擇wap的上網方式

HttpHost proxy = new HttpHost("10.0.0.172", 80, "http");set
httpClient.getParames(). );
}

public String get () throws Exception {  //封裝Http的get方法

(this.apiUrl)) ;
Log.w("AppClient.get.url", this.apiUrl);
// send get request
HttpResponse httpResponse = httpCl
HttpResponse httpResponse = httpCl​​nient. ().getStatusCode() == HttpStatus.SC_OK) {
String httpResult = resultFilter(httpResponse.getEntity());
Log.w("AppClient.get.result", httpRes7); httpResult;
} else {return null;
}
} catch (ConnectTimeoutException e. ception e ) {
e.printStackTrace();
}
return null;
}
ttp的post方法
try {
HttpPost httpPost = headerFilter(new HttpPost(this.apiUrl));
List postParams = new
Iterator it = urlParams.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();Map.Entry entry = (Map.Entry) it.next();
(new BasicNameValuePair(entry.getKey().toString(), entry.getValue().toString()));
}
// set data charset
{
httpPost.setEntity(new UrlEncodedFormEntity(postParams, this.charset));
} else {
posthttpPost.setEntity(new Urla); Log.w( "AppClient.post.url", this.apiUrl);
Log.w("AppClient.post.data", postParams.toString());// send post request🟜 .execute(httpPost);
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String httpResult = resultFilter(httpResponse.Ent AppClient.post.result", httpResult);
return httpResult;
} else {
return null;? new Exception(C .err.network);
} catch (Exception e) {
e.printStackTrace();
}
return null;private HttpGet headerFilter (HttpGet httpGet) {   處理發送get資料請求,看是否是壓縮格式
switch (this.compress) {
case CS_GZIP:
httpGet.addHeader("Accept-ipco), break;
default :
break;
}
return httpGet;
) {   處理發送post資料請求,看是否是壓縮格式

switch (this.compress) {
case CS_GZIP:httpPost.addHeader("Accept-Encoding", "gzip");httpPost.addHeader("Accept-Encoding", "gzip"); break;
}
return httpPost;
}

private String resultFilter(

String result = null;
try {
switch (this.compress) {
case CS_GZIP:
result = AppUtil.gzipToString(oity); result = EntityUtils. toString(entity);break;
}
} catch (IOException e) {
e.printStackTrace();
}
以上就介紹了微博開發1客戶端的http的get和post封裝,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!