com.dss886.nForumSDK.service.SearchService.java Source code

Java tutorial

Introduction

Here is the source code for com.dss886.nForumSDK.service.SearchService.java

Source

/*
 * 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.service;

import java.io.IOException;
import java.net.URLEncoder;

import com.dss886.nForumSDK.util.ParamOption;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;

import com.dss886.nForumSDK.http.GetMethod;
import com.dss886.nForumSDK.http.NForumException;
import com.dss886.nForumSDK.model.Search;

/**
 * ???
 * ?https://github.com/xw2423/nForum/wiki/nForum-API
 * @author dss886
 * @since 2014-9-7
 */
public class SearchService {

    private DefaultHttpClient httpClient;
    private String host;
    private String returnFormat;
    private String appkey;
    private String auth;

    public SearchService(DefaultHttpClient httpClient, String host, String returnFormat, String appkey,
            String auth) {
        this.httpClient = httpClient;
        this.host = host;
        this.returnFormat = returnFormat;
        this.appkey = appkey;
        this.auth = auth;
    }

    /** 
     * ??
     * ????utf-8
     * @param keyword ????
     * @return ?
     * @throws JSONException
     * @throws NForumException
     * @throws IOException
     */
    public Search searchBoard(String keyword) throws JSONException, NForumException, IOException {
        String board = URLEncoder.encode(keyword, "GBK");
        ParamOption params = new ParamOption().addParams("board", board);
        String url = host + "search/board" + returnFormat + appkey + params;
        GetMethod getMethod = new GetMethod(httpClient, auth, url);
        return Search.parse(getMethod.getJSON());
    }

    /**
     * ???
     * ??o?1???
     * /search/threads???/search/threads
      * ??title1, title2, titlen, author, day, m, a, o, count, page
     * @param board ???
     * @return ?
     * @throws JSONException
     * @throws NForumException
     * @throws IOException
     */
    public Search searchArticle(String board, ParamOption params)
            throws JSONException, NForumException, IOException {
        params.addParams("board", board);
        String url = host + "search/article" + returnFormat + appkey + params;
        GetMethod getMethod = new GetMethod(httpClient, auth, url);
        return Search.parse(getMethod.getJSON());
    }

    /**
    * ???
    * ???
    * ????
     * ??title1, title2, titlen, author, day, m, a, count, page
    * @param board ???
    * @return ?
    * @throws JSONException
    * @throws NForumException
    * @throws IOException
    */
    public Search searchThreads(String board, ParamOption params)
            throws JSONException, NForumException, IOException {
        params.addParams("board", board);
        String url = host + "search/threads" + returnFormat + appkey + params;
        GetMethod getMethod = new GetMethod(httpClient, auth, url);
        return Search.parse(getMethod.getJSON());
    }

}