Java tutorial
/* * Copyright (C) 2010-2014 dss886 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.dss886.nForumSDK; import org.apache.commons.codec.binary.Base64; import org.apache.http.impl.client.DefaultHttpClient; import com.dss886.nForumSDK.service.ArticleService; import com.dss886.nForumSDK.service.AttachmentService; import com.dss886.nForumSDK.service.BlacklistService; import com.dss886.nForumSDK.service.BoardService; import com.dss886.nForumSDK.service.FavouriteService; import com.dss886.nForumSDK.service.MailService; import com.dss886.nForumSDK.service.ReferService; import com.dss886.nForumSDK.service.SearchService; import com.dss886.nForumSDK.service.SectionService; import com.dss886.nForumSDK.service.UserService; import com.dss886.nForumSDK.service.VoteService; import com.dss886.nForumSDK.service.WidgetService; import com.dss886.nForumSDK.util.Host; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; /** * SDK?? * ? * @author dss886 * @since 2014-9-7 */ public class NForumSDK { private DefaultHttpClient httpClient; private String host; private String appkey; private String auth; private String returnFormat = Host.RETURN_FORMAT_JSON; private int timeout = 10000; /** * ?appkey * @param host ????http://api.byr.cn/ * Host.HOST_* ? * @param appkey ?????"?appkey=""&" * @param username ?? * @param password ? */ public NForumSDK(String host, String appkey, String username, String password) { httpClient = new DefaultHttpClient(); HttpParams param = httpClient.getParams(); HttpConnectionParams.setConnectionTimeout(param, timeout); HttpConnectionParams.setSoTimeout(param, timeout); httpClient = new DefaultHttpClient(param); auth = new String(Base64.encodeBase64((username + ":" + password).getBytes())); this.host = host; this.returnFormat = returnFormat + "?"; this.appkey = "&appkey=" + appkey; } /** * ??appkey * @param host ????http://api.byr.cn/ * HOST.HOST_* ? * @param username ?? * @param password ? */ public NForumSDK(String host, String username, String password) { httpClient = new DefaultHttpClient(); HttpParams param = httpClient.getParams(); HttpConnectionParams.setConnectionTimeout(param, timeout); HttpConnectionParams.setSoTimeout(param, timeout); httpClient = new DefaultHttpClient(param); auth = new String(Base64.encodeBase64((username + ":" + password).getBytes())); this.host = host; this.returnFormat = returnFormat + "?"; this.appkey = ""; } public int getTimeout() { return timeout; } /** * @param timeout */ public void setTimeout(int timeout) { this.timeout = timeout; } /** * @return ?? */ public UserService getUserService() { return new UserService(httpClient, host, returnFormat, appkey, auth); } /** * @return ?? */ public SectionService getSectionService() { return new SectionService(httpClient, host, returnFormat, appkey, auth); } /** * @return ??? */ public BoardService getBoardService() { return new BoardService(httpClient, host, returnFormat, appkey, auth); } /** * @return ?? */ public ArticleService getArticleService() { return new ArticleService(httpClient, host, returnFormat, appkey, auth); } /** * @return ?? */ public AttachmentService getAttachmentService() { return new AttachmentService(httpClient, host, returnFormat, appkey, auth); } /** * @return ?? */ public MailService getMailService() { return new MailService(httpClient, host, returnFormat, appkey, auth); } /** * @return ??? */ public FavouriteService getFavouriteService() { return new FavouriteService(httpClient, host, returnFormat, appkey, auth); } /** * @return ???? */ public ReferService getReferService() { return new ReferService(httpClient, host, returnFormat, appkey, auth); } /** * @return ??? */ public SearchService getSearchService() { return new SearchService(httpClient, host, returnFormat, appkey, auth); } /** * @return Widget?? */ public WidgetService getWidgetService() { return new WidgetService(httpClient, host, returnFormat, appkey, auth); } /** * @return ????? */ public BlacklistService getBlacklistService() { return new BlacklistService(httpClient, host, returnFormat, appkey, auth); } /** * @return Widget?? */ public VoteService getVoteService() { return new VoteService(httpClient, host, returnFormat, appkey, auth); } }