在上一节介绍的OAuth认证过程中我们可以看到我们需要不断地和腾讯微博开放平台进行数据的交互,因此我们需要编写一个类用来发送Http请求,并且能处理平台返回过来的数据。学习Html的朋友应该知道Get和Post两种方式提交数据,在这里我们同样也需要编写Post和Get两个方法模拟Post和Get请求。在发送微博时我们还可以上传照片,所以我们还应编写一个方法用于上传图片,但是在这里暂时还不编写上传数据的方法。另外在模拟Http请求时我们需要传递参数,因此我们需创建一个Parameter类,表示参数对象。在腾讯微博开放平台中关于oauth_signature参数值的产生过程介绍中有这样一幅描述图(http://open.t.qq.com/resource.php?i=1,2#tag0 )
![2.腾讯微博Android客户端开发——Parameter类和SyncHttp类编写_第1张图片](http://img.e-com-net.com/image/info5/5a97e44d18f74998bfe1586f87e75d81.jpg)
我们可以看到参数对象是需要进行排序的,这里的排序是参数按name进行字典升序排列,详细介绍我们可以参考这篇介绍:http://wiki.opensns.qq.com/wiki/%E3%80%90QQ%E7%99%BB%E5%BD%95%E3%80%91%E7%AD%BE%E5%90%8D%E5%8F%82%E6%95%B0oauth_signature%E7%9A%84%E8%AF%B4%E6%98%8E。因此我们需让我们的Parameter类实现 Comparable<T>接口,重写里面的方法,最终Parameter类代码如下:
view plain copy to clipboard print ?
- package com.szy.weibo.model;
- import java.io.Serializable;
-
-
-
-
-
- public class Parameter implements Serializable, Comparable<Parameter>
- {
- private static final long serialVersionUID = 2721340807561333705L;
-
- private String name;
- private String value;
-
- public Parameter()
- {
- super();
- }
-
- public Parameter(String name, String value)
- {
- super();
- this.name = name;
- this.value = value;
- }
-
- public String getName()
- {
- return name;
- }
-
- public void setName(String name)
- {
- this.name = name;
- }
-
- public String getValue()
- {
- return value;
- }
-
- public void setValue(String value)
- {
- this.value = value;
- }
-
- @Override
- public String toString()
- {
- return "Parameter [name=" + name + ", value=" + value + "]";
- }
-
- @Override
- public boolean equals(Object arg0)
- {
- if (null == arg0)
- {
- return false;
- }
- if (this == arg0)
- {
- return true;
- }
- if (arg0 instanceof Parameter)
- {
- Parameter param = (Parameter) arg0;
-
- return this.getName().equals(param.getName()) && this.getValue().equals(param.getValue());
- }
- return false;
- }
-
- @Override
- public int compareTo(Parameter param)
- {
- int compared;
- compared = name.compareTo(param.getName());
- if (0 == compared)
- {
- compared = value.compareTo(param.getValue());
- }
- return compared;
- }
- }
模拟发送Http请求我们可以使用HttpURLConnection类进行操作,但是Android平台集成了功能强大且编写更容易的commons-httpclient.jar,因此在这里介绍如何通过commons-httpclient进行Http请求。发送Http请求可以有两种方式:一种是同步,一种是异步。由于我对异步不是很熟悉,所以这里先提供同步方式发送Http请求:
view plain copy to clipboard print ?
- package com.szy.weibo.service;
-
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.List;
-
- import org.apache.commons.httpclient.HttpClient;
- import org.apache.commons.httpclient.HttpStatus;
- import org.apache.commons.httpclient.NameValuePair;
- import org.apache.commons.httpclient.methods.GetMethod;
- import org.apache.commons.httpclient.methods.PostMethod;
- import org.apache.commons.httpclient.params.HttpMethodParams;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
-
- import com.szy.weibo.model.Parameter;
-
-
-
-
-
-
-
-
-
-
- public class SyncHttp
- {
- private static final Log LOG = LogFactory.getLog(SyncHttp.class);
-
- private static final int CONNECTION_TIMEOUT = 1000 * 5;
-
-
-
-
-
-
-
-
- public String httpGet(String url, String params) throws Exception
- {
- String response = null;
- if (null!=params&&!params.equals(""))
- {
- url += "?" + params;
- }
-
- HttpClient httpClient = new HttpClient();
-
- GetMethod httpGet = new GetMethod(url);
-
- httpGet.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, new Integer(CONNECTION_TIMEOUT));
- try
- {
- int statusCode = httpClient.executeMethod(httpGet);
- if (statusCode == HttpStatus.SC_OK)
- {
- InputStream inputStream = httpGet.getResponseBodyAsStream();
- response = getData(inputStream);
- }
- else
- {
- LOG.debug("Get Method Statuscode : "+statusCode);
- }
- } catch (Exception e)
- {
- throw new Exception(e);
- } finally
- {
- httpGet.releaseConnection();
- httpClient = null;
- }
- return response;
- }
-
-
-
-
-
-
-
-
- public String httpPost(String url, List<Parameter> params) throws Exception
- {
- String response = null;
- HttpClient httpClient = new HttpClient();
- PostMethod httpPost = new PostMethod(url);
-
- httpPost.addParameter("Connection", "Keep-Alive");
- httpPost.addParameter("Charset", "UTF-8");
- httpPost.addParameter("Content-Type", "application/x-www-form-urlencoded");
- httpPost.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, new Integer(CONNECTION_TIMEOUT));
- if (null!=params&¶ms.size()!=0)
- {
-
- httpPost.setRequestBody(buildNameValuePair(params));
- }
- try
- {
- int statusCode = httpClient.executeMethod(httpPost);
- if (statusCode == HttpStatus.SC_OK)
- {
- InputStream inputStream = httpPost.getResponseBodyAsStream();
- response = getData(inputStream);
- }
- else
- {
- LOG.debug("Post Method Statuscode : "+statusCode);
- }
- } catch (Exception e)
- {
- throw new Exception(e);
- } finally
- {
- httpPost.releaseConnection();
- httpClient = null;
- }
- return response;
- }
-
-
-
-
-
-
- private NameValuePair[] buildNameValuePair(List<Parameter> params)
- {
- int size = params.size();
- NameValuePair[] pair = new NameValuePair[size];
- for(int i = 0 ;i<size;i++)
- {
- Parameter param = params.get(i);
- pair[i] = new NameValuePair(param.getName(),param.getValue());
- }
- return pair;
- }
-
-
-
-
-
-
- private String getData(InputStream inputStream) throws Exception
- {
- String data = "";
-
- ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
- int len = -1;
- byte[] buff = new byte[1024];
- try
- {
- while((len=inputStream.read(buff))!=-1)
- {
- outputStream.write(buff, 0, len);
- }
- byte[] bytes = outputStream.toByteArray();
- data = new String(bytes);
- } catch (IOException e)
- {
- throw new Exception(e.getMessage(),e);
- }
- finally
- {
- outputStream.close();
- }
- return data;
- }
- }
本节课程下载地址:http://u.115.com/file/aq8oydql
本节课程文档下载:http://www.docin.com/p-213889278.html