当前位置:操作系统 > 安卓/Android >>

Android仿人人客户端(v5.7.1)——通过HTTPS协议的POST方式获取用户的基本信息

 
一、扩展之前的网络模块

      基于Android仿人人客户端(v5.7.1)——网络模块处理的架构这篇进行扩展,添加通过HTTPS协议的POST方式访问网络的处理。自定义类(AsyncHttpsPost)让其继承AsyncBaseRequest类,代码如下:

[java]
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<String, String> parameter, 
            ParseCallback handler, ResultCallback requestCallback) { 
        super(url, parameter, handler, requestCallback); 
    } 
 
    @Override 
    protected InputStream getRequestResult() throws IOException { 
        List<NameValuePair> paramPairs = new ArrayList<NameValuePair>(); 
        if(parameter != null && !parameter.isEmpty()){ 
            for(Map.Entry<String, String> 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; 
    } 
 

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<String, String> parameter,
   ParseCallback handler, ResultCallback requestCallback) {
  super(url, parameter, handler, requestCallback);
 }

 @Override
 protected InputStream getRequestResult() throws IOException {
     List<NameValuePair> paramPairs = new ArrayList<NameValuePair>();
        if(parameter != null && !parameter.isEmpty()){
            for(Map.Entry<String, String> 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认证。

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

[java]
String url = "https://api.renren.com/restserver.do"; 
Map<String, String> parameter = new HashMap<String, String>(); 
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"); // 你要访问那个接口,我们肯定调用获取用户的信息的接口咯,该接口支持批量获取。 

        String url = "https://api.renren.com/re

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,