http://www.eoeandroid.com/thread-53701-1-1.html

package mobson.weiboku.view;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
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.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
 
import mobson.weiboku.R;
import mobson.weiboku.model.User;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.widget.TextView;
/**
* 测试新浪微博API
* @author syn
* @date 2010/12/22
*/
public class TestActivity extends Activity
{
private static final String BASE_URL = "http://api.t.sina.com.cn/"; //API接口
private static final String CONSUMER_KEY = "270793661"; //你申请的Key
private static final String HEADER_AUTHO = "Authorization"; //Authorization
private static final String HEADER_BASIC = "Basic "; //Basic
private static final String ERROR = "MyError"; //错误
 
List<myTest> myTestList;
/**
* 测试用的类,用于解析JSON,因为只是测试,所以乱写一下
*/
public class myTest
{
private Date created_at; //返回微博发布的时间
private String text; //微博内容
private User user; //微博用户信息
public myTest(JSONObject object) throws JSONException //解析JSON文件
{
text=""; user=null;
created_at=new Date(object.getString("created_at"));
text=object.getString("text");
user=new User(object.getJSONObject("user"));
}
}
@Override
public void onCreate(Bundle savedInstanceState)
{
String tailUrl="statuses/public_timeline.json"; //我要获得的是最新的公共微博
String response=getResponse(tailUrl, MainActivity.loginUser);//调用提交函数,此函数是核心部分
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.test);
TextView textView01=(TextView)findViewById(R.id.test);
if(response.startsWith(ERROR))
textView01.setText("error");
else
{
JSONArray array;
try
{
array = new JSONArray(response);
for (int i = 0; i < array.length(); i++)
{
myTest mytest= new myTest(array.getJSONObject(i));
//myTestList.add(mytest);
String content="Content:"+mytest.text+" Author:"+mytest.user.getNike()+" Date:"+mytest.created_at;
textView01.setText(content);
}
}
catch (JSONException e)
{
e.printStackTrace();
}
}
}
/**
* 此函数提交URL,返回访问结果
* @param tailUrl json或者xml的url
* @param user 用户的一个对象
* @return 提交结果
*/
private static String getResponse(String tailUrl,User user)
{
String httpUrl=BASE_URL+tailUrl;
ArrayList<NameValuePair> postParams=new ArrayList<NameValuePair>();
postParams.add(new BasicNameValuePair("source",CONSUMER_KEY)); //封装入APP Key
try
{
 
HttpPost httpRequest = new HttpPost(httpUrl);
httpRequest.setEntity(new UrlEncodedFormEntity(postParams,HTTP.UTF_8)); //把参数放入Entity
httpRequest.addHeader(HEADER_AUTHO, HEADER_BASIC+user.encodeBase64NamePass()); //这里就是给用户的用户名和密码加密,然后放入http头
httpRequest.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE,false);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse httpResponse = httpclient.execute(httpRequest); //提交
int statusCode=httpResponse.getStatusLine().getStatusCode(); //获得结果码200是正确
if ( statusCode== HttpStatus.SC_OK)
{
String strResult = EntityUtils.toString(httpResponse.getEntity());
Log.e("WeiboKu", "strResult :"+strResult);
return strResult;
}
else
{
Log.e("WeiboKu", "strResult Error:"+statusCode);
return ERROR+String.valueOf(statusCode);
}
}
catch (Exception e)
{
Log.e("WeiboKu", "getResponse Exception:"+e.getMessage());
return ERROR+e.getMessage().toString();
}
}
}

新浪微博(t.sina)简单授权代码及API测试的更多相关文章

  1. 我的权限系统设计实现MVC4 + WebAPI + EasyUI + Knockout(四)授权代码维护

    一.前言 权限系统设计中,授权代码是用来控制数据访问权限的.授权代码说白了只是一树型结构的数据,没有什么其它的业务意义.那么这个页面的功能也就非常简单授权代码维护:新增.修改.删除授权代码数据. 二. ...

  2. Security » Authorization » 简单授权

    Simple Authorization¶ 简单授权 82 of 86 people found this helpful Authorization in MVC is controlled thr ...

  3. python网页请求urllib2模块简单封装代码

    这篇文章主要分享一个python网页请求模块urllib2模块的简单封装代码. 原文转自:http://www.jbxue.com/article/16585.html 对python网页请求模块ur ...

  4. 原生js简单调用百度翻译API实现的翻译工具

    先来个在线demo: js翻译工具 或者百度搜索js简单调用百度翻译API工具(不过有个小小的界面显示bug,我想细心的人应该会发现) 或者直接前往该网址:js翻译工具 或者前往我的github:gi ...

  5. 手写DotNet Core 认证授权代码

    在普通的MVC项目中 我们普遍的使用Cookie来作为认证授权方式,使用简单.登录成功后将用户信息写入Cookie:但当我们做WebApi的时候显然Cookie这种方式就有点不适用了. 在dotnet ...

  6. 使用go, gin, gorm编写一个简单的curd的api接口

    go 是一门非常灵活的语言,既具有静态语言的高性能,又有动态语言的开发速度快的优点,语法也比较简单,下面是通过简单的代码实现了一个简单的增删改查 api 接口 hello world 常规版 新建 d ...

  7. 无需编写代码,API业务流程测试,零代码实现

    引言 除了测试单个接口,我们常常需要对多个有数据或者逻辑关联的接口进行业务流程测试,例如获取验证码-注册-登录.传统测试业务流程需要编写一系列测试代码,现在通过eoLinker全UI界面,无需编写任何 ...

  8. 让 API 测试变的简单。

    做开发已经四年有余了,之前在接口测试的时候最开始用的自己写的测试类进行测试,后来接触到了 postman 和 swagger ,虽然用起来比自己写的强太多了,但是总觉得差点事儿. 一方面是 postm ...

  9. Lucene.net站内搜索—3、最简单搜索引擎代码

    目录 Lucene.net站内搜索—1.SEO优化 Lucene.net站内搜索—2.Lucene.Net简介和分词Lucene.net站内搜索—3.最简单搜索引擎代码Lucene.net站内搜索—4 ...

随机推荐

  1. 通过编译函数库来学习GCC【转】

    转自:http://blog.csdn.net/u012365926/article/details/51446295 基本概念 什么是库 在windows平台和linux平台下都大量存在着库. 本质 ...

  2. 基于ARM 构架(带MMU)的copy_from_user与copy_to_user详细分析

      [转自:http://blog.chinaunix.net/uid-20543672-id-3195249.html]   在学习Linux内核驱动的时候,一开始就会碰到copy_from_use ...

  3. memcached和redis区别

    Memcached:是高性能分布式内存缓存服务器,本质是一个内存 key-value 数据库,但不支持数据持久化,服务器关闭后,数据全丢失.只支持 key-value 结构. Redis:将大部分数据 ...

  4. 【bzoj4896】补退选

    傻逼题. 每个点维护下vector,然后随便做. #include<bits/stdc++.h> ; using namespace std; typedef long long ll; ...

  5. tcpcopy 流量复制

    依赖 libpcap 线上服务器安装 https://github.com/session-replay-tools/tcpcopy.git 下载后解压 #--pcap-capture 默认raw s ...

  6. mybatis源码阅读(动态代理)

    这一篇文章主要是记录Mybatis的动态代理学习成果,如果对源码感兴趣,可以看一下上篇文章  https://www.cnblogs.com/ChoviWu/p/10118051.html 阅读本篇的 ...

  7. [hadoop][基本原理]zookeeper简单使用

    代码:https://github.com/xufeng79x/ZkClientTest 1.简介 zookeeper的基本原理和使用场景描述可参考:[hadoop][基本原理]zookeeper基本 ...

  8. linux命令(4):vmstat命令

    CPU监控如下: vmstat 2 10  //表示每隔2秒运行10次 内存监控如下: vmstat –s 监控进程及CPU.内存状态 如下: top

  9. 常用模块二(hashlib、configparser、logging)

    阅读目录 常用模块二 hashlib模块 configparse模块 logging模块   常用模块二 返回顶部 hashlib模块 Python的hashlib提供了常见的摘要算法,如MD5,SH ...

  10. python webpy 框架环境架设

    前几年使用过 webpy做个些小东西,今天有个东西从拾webpy.但是基本上都忘记了,还是那句古话“好记性不如烂笔头”.这里把相应的步骤梳理下. 前提: 操作系统 windows 一.webpy 方面 ...