0

0

微博开发1客户端的http的get和post封装

php中文网

php中文网

发布时间:2016-08-08 09:31:59

|

1022人浏览过

|

来源于php中文网

原创

这篇博客讲的是客户端如何封装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;
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;        

              // c.api.base   c是自己建的一个类里面

            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";
}
       }

幸福女淘宝客
幸福女淘宝客

幸福女淘宝客是一款用php来开发的淘宝分享购物网站,它包含文章、产品系统、金币兑换、金币抢拍、团购、店铺管理系统、订单系统等功能。没有最强大 只有更强大 目前功能最强大的淘宝客系统支持qq 微博登陆 支持关键词搜索淘宝商品支持绑定微博账号发布支持手机访问客户端可以从appcan直接生成安装http://localhot/install 想重新安装 请删除 /config/install.lock安

下载

String apiSid = AppUtil.getSessionId();                //AppUtil是自己编写的工具类里面封装了一些基本用法比如获取用户session,加密,gzip压缩等等后面介绍
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);

               //此处是简单的获取Httpclient

}

public void useWap () {                               //此处是选择wap的上网方式
HttpHost proxy = new HttpHost("10.0.0.172", 80, "http");
httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
}

public String get () throws Exception {  //封装Http的get方法
try {
HttpGet httpGet = headerFilter(new HttpGet(this.apiUrl));
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 {//封装Http的post方法
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) {   处理发送get数据请求,看是否是压缩格式
switch (this.compress) {
case CS_GZIP:
httpGet.addHeader("Accept-Encoding", "gzip");
break;
default :
break;
}
return httpGet;
}

private HttpPost headerFilter (HttpPost httpPost) {   处理发送post数据请求,看是否是压缩格式
switch (this.compress) {
case CS_GZIP:
httpPost.addHeader("Accept-Encoding", "gzip");
break;
default :
break;
}
return httpPost;
}

private String resultFilter(HttpEntity entity){      处理获得的数据,看是否是压缩格式
String 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;
}

}

以上就介绍了微博开发1客户端的http的get和post封装,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
pixiv网页版官网登录与阅读指南_pixiv官网直达入口与在线访问方法
pixiv网页版官网登录与阅读指南_pixiv官网直达入口与在线访问方法

本专题系统整理pixiv网页版官网入口及登录访问方式,涵盖官网登录页面直达路径、在线阅读入口及快速进入方法说明,帮助用户高效找到pixiv官方网站,实现便捷、安全的网页端浏览与账号登录体验。

705

2026.02.13

微博网页版主页入口与登录指南_官方网页端快速访问方法
微博网页版主页入口与登录指南_官方网页端快速访问方法

本专题系统整理微博网页版官方入口及网页端登录方式,涵盖首页直达地址、账号登录流程与常见访问问题说明,帮助用户快速找到微博官网主页,实现便捷、安全的网页端登录与内容浏览体验。

233

2026.02.13

Flutter跨平台开发与状态管理实战
Flutter跨平台开发与状态管理实战

本专题围绕Flutter框架展开,系统讲解跨平台UI构建原理与状态管理方案。内容涵盖Widget生命周期、路由管理、Provider与Bloc状态管理模式、网络请求封装及性能优化技巧。通过实战项目演示,帮助开发者构建流畅、可维护的跨平台移动应用。

117

2026.02.13

TypeScript工程化开发与Vite构建优化实践
TypeScript工程化开发与Vite构建优化实践

本专题面向前端开发者,深入讲解 TypeScript 类型系统与大型项目结构设计方法,并结合 Vite 构建工具优化前端工程化流程。内容包括模块化设计、类型声明管理、代码分割、热更新原理以及构建性能调优。通过完整项目示例,帮助开发者提升代码可维护性与开发效率。

22

2026.02.13

Redis高可用架构与分布式缓存实战
Redis高可用架构与分布式缓存实战

本专题围绕 Redis 在高并发系统中的应用展开,系统讲解主从复制、哨兵机制、Cluster 集群模式及数据分片原理。内容涵盖缓存穿透与雪崩解决方案、分布式锁实现、热点数据优化及持久化策略。通过真实业务场景演示,帮助开发者构建高可用、可扩展的分布式缓存系统。

61

2026.02.13

c语言 数据类型
c语言 数据类型

本专题整合了c语言数据类型相关内容,阅读专题下面的文章了解更多详细内容。

30

2026.02.12

雨课堂网页版登录入口与使用指南_官方在线教学平台访问方法
雨课堂网页版登录入口与使用指南_官方在线教学平台访问方法

本专题系统整理雨课堂网页版官方入口及在线登录方式,涵盖账号登录流程、官方直连入口及平台访问方法说明,帮助师生用户快速进入雨课堂在线教学平台,实现便捷、高效的课程学习与教学管理体验。

15

2026.02.12

豆包AI网页版入口与智能创作指南_官方在线写作与图片生成使用方法
豆包AI网页版入口与智能创作指南_官方在线写作与图片生成使用方法

本专题汇总豆包AI官方网页版入口及在线使用方式,涵盖智能写作工具、图片生成体验入口和官网登录方法,帮助用户快速直达豆包AI平台,高效完成文本创作与AI生图任务,实现便捷智能创作体验。

669

2026.02.12

PostgreSQL性能优化与索引调优实战
PostgreSQL性能优化与索引调优实战

本专题面向后端开发与数据库工程师,深入讲解 PostgreSQL 查询优化原理与索引机制。内容包括执行计划分析、常见索引类型对比、慢查询优化策略、事务隔离级别以及高并发场景下的性能调优技巧。通过实战案例解析,帮助开发者提升数据库响应速度与系统稳定性。

58

2026.02.12

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

相关下载

更多

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Excel 教程
Excel 教程

共162课时 | 18.1万人学习

Pandas 教程
Pandas 教程

共15课时 | 1.1万人学习

C# 教程
C# 教程

共94课时 | 9.8万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号