快捷搜索:

实战Android仿人人客户端之通过HTTPS协议的POST方式

一、扩展之前的收集模块

基于 实战Android仿各人客户端之收集模块处置惩罚的架构 这篇进行扩展,添加经由过程HTTPS协议的POST要领造访收集的处置惩罚。自定义类(AsyncHttpsPost)让其承袭AsyncBaseRequest类,代码如下:

package com.everyone.android.net;

import java.io.IOException;

import java.io.InputStream;import java.util.ArrayList;

import java.util.List;import java.util.Map;

import org.apache.http.HttpResponse;

import org.apache.http.NameValuePair;import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.message.BasicNameValuePair;import org.apache.http.protocol.HTTP;

import com.everyone.android.callback.ParseCallback;

import com.everyone.android.callback.ResultCallback;

/奸淫 功能描述:经由过程HTTPS协议发送POST收集哀求

* @author android_ls*

*/ public class AsyncHttpsPost extends AsyncBaseRequest {

/**

**/

private static final long serialVersionUID = 4L;

public AsyncHttpsPost(String url, Map parameter,ParseCallback handler, ResultCallback requestCallback) {

super(url, parameter, handler, requestCallback);}

@Override

protected InputStream getRequestResult() throws IOException {List paramPairs = new ArrayList();

if(parameter != null && !parameter.isEmpty()){for(Map.Entry entry : parameter.entrySet()){

paramPairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));}

}

UrlEncodedFormEntity entitydata = new UrlEncodedFormEntity(paramPairs, HTTP.UTF_8);HttpPost post = new HttpPost(requestUrl);

post.setEntity(entitydata);DefaultHttpClient client = new DefaultHttpClient();

// 履行POST要领哀求HttpResponse response = client.execute(post);

if(response.getStatusLine().getStatusCode()==200){return response.getEntity().getContent();

}

return null;}

}

二、获取用户的基础信息

向办事器端发送哀求,获取用户的基础信息。注:为什么要应用HTTPS协议发送哀求?官方给出的来由,应用HTTPS协议调用API接口可以免去SIG认证。(各人开放平台的API:http://wiki.dev.renren.com/wiki/API)

1、获取用户信息所需的参数

String url = "https://api.renren.com/restserver.do";

Map parameter = new HashMap();parameter.put("v", "1.0"); // API的版本号,固定值为1.0

parameter.put("access_token", accessToken); // OAuth2.0验证授权后得到的token。parameter.put("format", "JSON"); // 返回值的款式。请指定为JSON或者XML,保举应用JSON,缺省值为XML

parameter.put("call_id", "1.0"); // 哀求行列步队号parameter.put("method", "users.getInfo"); // 你要造访那个接口,我们肯定调用获取用户的信息的接口咯,该接口支持批量获取。

注:AccessToken的值我们之前有在本地存储的,获取的要领如下:

mAuthTokenManager = new AuthTokenManager(this);

String accessToken = mAuthTokenManager.getAccessToken();LogUtil.e("EveryoneActivity", "accessToken = " + accessToken);

2、发送异步收集哀求

AsyncBaseRequest asyncHttpsPost = new AsyncHttpsPost(url, parameter, new ParseCallback() {

@Override

public Object parse(String json) throws JSONException {LogUtil.i("EveryoneActivity", "json = " + json);

return null;

}}, new ResultCallback() {

@Override

public void onSuccess(Object obj) {

}

@Overridepublic void onFail(int errorCode) {

// TODO Auto-generated method stub}

});

mDefaultThreadPool.execute(asyncHttpsPost);mAsyncRequests.add(asyncHttpsPost);

3、收集哀求返回的JSON

[

{"uid":461345584,

"tinyurl":"http://hdn.xnimg.cn/photos/hdn521/20130319/1930/h_tiny_zggz_8829000002e6113e.jpg","vip":1,

"sex":1,"name":"逐鹿。。。",

"star":1,"headurl":"http://hdn.xnimg.cn/photos/hdn521/20130319/1930/h_head_jbdD_8829000002e6113e.jpg",

"zidou":0}

]

4、对JSON进行解析(这里我们要自己一个字段一个字段的去解吗?不用,我给大年夜家保举应用谷歌GSON去进行解析。不知道的可以到网上搜下GSON,下载一个jar包导入工程就可以了。)

new ParseCallback() {

@Override

public Object parse(String json) throws JSONException {LogUtil.i("EveryoneActivity", "json = " + json);

Gson gson = new Gson();

java.lang.reflect.Type type = new TypeToken>(){}.getType();LinkedList userList = gson.fromJson(json, type);

return userList.get(0);}

}

5、将解析后的结果返回

new ResultCallback() {

@Override

public void onSuccess(Object obj) {UserBasicInfo userBasic = (UserBasicInfo)obj;

LogUtil.i("EveryoneActivity", "userBasic = " + userBasic.toString());

}

@Overridepublic void onFail(int errorCode) {

// TODO Auto-generated method stub}

}

三、发送收集哀求,并解析返回结果的完备代码如下:

mAuthTokenManager = new AuthTokenManager(this);

String accessToken = mAuthTokenManager.getAccessToken();LogUtil.e("EveryoneActivity", "accessToken = " + accessToken);

// 获取用户信息所需的参数

String url = "https://api.renren.com/restserver.do";Map parameter = new HashMap();

parameter.put("v", "1.0"); // API的版本号,固定值为1.0parameter.put("access_token", accessToken); // OAuth2.0验证授权后得到的token。

parameter.put("format", "JSON"); // 返回值的款式。请指定为JSON或者XML,保举应用JSON,缺省值为XMLparameter.put("call_id", "1.0"); // 哀求行列步队号

parameter.put("method", "users.getInfo"); // 你要造访那个接口,我们肯定调用用获取用户的信息的接口咯,该接口支持批量获取。

AsyncBaseRequest asyncHttpsPost = new AsyncHttpsPost(url, parameter, new ParseCallback() {

@Overridepublic Object parse(String json) throws JSONException {

LogUtil.i("EveryoneActivity", "json = " + json);

Gson gson = new Gson();java.lang.reflect.Type type = new TypeToken>() {

}.getType();LinkedList userList = gson.fromJson(json, type);

return userList.get(0);}

}, new ResultCallback() {

@Overridepublic void onSuccess(Object obj) {

UserBasicInfo userBasic = (UserBasicInfo) obj;LogUtil.i("EveryoneActivity", "userBasic = " + userBasic.toString());

}

@Override

public void onFail(int errorCode) {// TODO Auto-generated method stub

}});

mDefaultThreadPool.execute(asyncHttpsPost);

mAsyncRequests.add(asyncHttpsPost);

附带:用户基础信息实体类

package com.everyone.android.entity;

/**

* 功能描述:用户基础信息实体类* @author android_ls

**/

public class UserBasicInfo {

private int uid; // 用户id

private String tinyurl; // 头像链接 50*50大年夜小

private int sex; // 性别,值1表示男性;值0表示女性

private String name; // 用户名

private int star; // 是否为星级用户,值“1”表示“是”;值“0”表示“不是”

private String headurl; // 头像链接 100*100大年夜小

private int zidou; // 是否为vip用户,值1表示是;值0表示不是

private int vip; // 是否为vip用户等级,条件是zidou节点必须为1

public int getUid() {return uid;

}

public void setUid(int uid) {this.uid = uid;

}

public String getTinyurl() {return tinyurl;

}

public void setTinyurl(String tinyurl) {this.tinyurl = tinyurl;

}

public int getSex() {return sex;

}

public void setSex(int sex) {this.sex = sex;

}

public String getName() {return name;

}

public void setName(String name) {this.name = name;

}

public int getStar() {return star;

}

public void setStar(int star) {this.star = star;

}

public String getHeadurl() {return headurl;

}

public void setHeadurl(String headurl) {this.headurl = headurl;

}

public int getZidou() {return zidou;

}

public void setZidou(int zidou) {this.zidou = zidou;

}

public int getVip() {return vip;

}

public void setVip(int vip) {this.vip = vip;

}

@Overridepublic String toString() {

return "UserBasicInfo [uid=" + uid + ", tinyurl=" + tinyurl + ", sex=" + sex + ", name=" + name + ", star=" + star + ", headurl="+ headurl + ", zidou=" + zidou + ", vip=" + vip + "]";

}

}

这篇就先聊到这里,上面获取的用户信息里,有用户图像的URL,也便是说关联到了图片在本地的存储与处置惩罚。下一篇我们聊一聊图片的双缓存处置惩罚,敬请等候。。。

转自:http://blog.csdn.net/android_ls/article/details/8770537

您可能还会对下面的文章感兴趣: