Android

项目主页: https://github.com/shopex/prism-android/

用途

实现shopex Prism 的Android版SDK供第三方使用

功能

  • 提供oauth认证
  • 提供get,post请求

要求

Android 1.6+ JDK6以上版本

提供oauth认证

  • 创建PrismOauth实例对象,并且将上下文Context,clientId和redirectUrl传入
String redirectUrl = "http://buwb2lii.com/oauth-adapter?action=callback";
String clientId = "buwb2lii";
PrismOauth oauth = new PrismOauth(context,clientId,redirectUrl);
  • 进行认证
			oauth.authorize(new PrismOauthListener() {
					
					@Override
					public void onSuccess(OAuth data) {
						System.out.println("登录成功:"+data.getAccess_token());
						UIUtils.showAlert(MainActivity.this, "成功","登录成功:"+data.getAccess_token());
						//用户需将OAuth对象保存本地,以后再次发送请求的时候将access_token带入头部
					}
					
					@Override
					public void onFaliure(int code, String result) {
						UIUtils.showAlert(MainActivity.this, "失败",result+":"+code);
						
					}
					
					@Override
					public void onException(PrismException exception) {
						// TODO Auto-generated method stub
						
					}
					
					@Override
					public void onCancel() {
						UIUtils.showAlert(MainActivity.this, "失败","用户取消");
						
					}
				});

提供get请求

  • 创建NetWorkClient实例对象,并且将上下文Context,OAuth传入
			//context 为上下文环境 OAuth 为提供OAuth认证后返回的数据,将OAuth妥善保管在本地,如果没有OAuth 请传入null
			NetworkClient networkClient = new NetworkClient(context,oAuth);

			//等到获得是在setOAuth即可
			networkClient.setOAuth(oAuth);

  • get请求 ,get有重载,可以根据不同的条件调用不同方法
			networkClient.get(clientId, clientSecret, url,new ShopExAsynchResponseHandler(){

					@Override
					public void onSuccess(int status, Header[] headers,
							byte[] body) {
						// 返回正确结果
						super.onSuccess(status, headers, body);
						String json = new String(body);

					}

					@Override
					public void onFailure(int status, Header[] headers,
							byte[] body, Throwable e) {
						// 返回错误信息
						super.onFailure(status, headers, body, e);
					}
					
				});
				
  • post请求 ,post有重载,可以根据不同的条件调用不同方法
			/*
			* 参数设置
			*
			/
			public class TestReq extends AbstractCommonReq{
				public TestReq(String t1,String t2){
					add("t1",t1);
					add("t2",t2);
				}
			}
			networkClient.post(clientId, clientSecret, url,new TestReq(),new ShopExAsynchResponseHandler(){

					@Override
					public void onSuccess(int status, Header[] headers,
							byte[] body) {
						// 返回正确结果
						super.onSuccess(status, headers, body);
						String json = new String(body);

					}

					@Override
					public void onFailure(int status, Header[] headers,
							byte[] body, Throwable e) {
						// 返回错误信息
						super.onFailure(status, headers, body, e);
					}
					
				});