com.zjut.material_wecenter.Client.java Source code

Java tutorial

Introduction

Here is the source code for com.zjut.material_wecenter.Client.java

Source

package com.zjut.material_wecenter;

import android.support.annotation.NonNull;
import android.util.Log;

import com.google.gson.Gson;
import com.zjut.material_wecenter.models.Action;
import com.zjut.material_wecenter.models.AnswerComment;
import com.zjut.material_wecenter.models.AnswerDetail;
import com.zjut.material_wecenter.models.Dynamic;
import com.zjut.material_wecenter.models.LoginProcess;
import com.zjut.material_wecenter.models.PublishAnswer;
import com.zjut.material_wecenter.models.PublishQuestion;
import com.zjut.material_wecenter.models.Question;
import com.zjut.material_wecenter.models.QuestionDetail;
import com.zjut.material_wecenter.models.Result;
import com.zjut.material_wecenter.models.UserInfo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Copyright (C) 2016 Jinghong Union of ZJUT
 *
 * 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.
 */
public class Client {

    public static int PUBLISH = 101;
    public static int ANSWER = 201;
    private static Client client;
    private String cooike;

    /**
     * 
     * @return Client
     */
    public static Client getInstance() {
        if (client == null)
            client = new Client();
        return client;
    }

    /**
     * loginProcess 
     * @param user_name ??
     * @param password ?
     * @return ?LoginProcessResult
     */
    public Result loginProcess(String user_name, String password) {
        Map<String, String> params = new HashMap<>();
        params.put("user_name", user_name);
        params.put("password", password);
        String json = doPost(Config.LOGIN_PROCESS, params);
        return getResult(json, LoginProcess.class);
    }

    /**
     * getUserInfo ??
     * @param uid ID
     * @return ?UserInfoResult
     */
    public Result getUserInfo(String uid) {
        String json = doGet(Config.GET_USERINFO + "?uid=" + uid);
        return getResult(json, UserInfo.class);
    }

    /**
     * getUserActions???
     * @param uid ID
     * @param actions 101-??? 201-?
     * @return ?ActionResult
     */
    public Result getUserActions(String uid, int actions, int page) {
        Log.d("page", String.valueOf(page));
        String json = doGet(Config.GET_USER_ACTIONS + "?uid=" + uid + "&actions=" + String.valueOf(actions)
                + "&page=" + String.valueOf(page));
        Log.d("JSON", json);
        return getResults(json, Action.class);
    }

    /**
     * explore ??
     * @param page 
     * @return Result
     */
    public Result explore(int page) {
        String url = Config.EXPLORE + "?page=" + String.valueOf(page) + "&per_page="
                + String.valueOf(Config.PER_PAGE);
        String json = doGet(url);
        return getResults(json, Question.class);
    }

    /**
     * getQuestion ?
     * @param questionID ?
     * @return Result
     */

    public Result getQuestion(int questionID) {
        String url = Config.QUESTION + questionID;
        String json = doGet(url);
        return getResult(json, QuestionDetail.class);
    }

    /**
     * getAnswer ?
     * @param answerID ?
     * @return Result
     */

    public Result getAnswer(int answerID) {
        String url = Config.ANSWER + answerID;
        String json = doGet(url);
        return getResult(json, AnswerDetail.class);
    }

    /**
     * getAnswer ?
     * @param answerID ?
     * @return Result
     */

    public Result getAnswerComments(int answerID) {
        String url = Config.ANSWER_COMMENT + answerID;
        String json = doGet(url);
        return getResultArray(json, AnswerComment.class);
    }

    /**
     * getDynamic ?home?
     * @return Result
     */
    public Result getDynamic(int page) {
        String url = Config.HOME_DYNAMIC + "?page=" + String.valueOf(page) + "&per_page="
                + String.valueOf(Config.PER_PAGE);
        String json = doGet(url);
        return getResults(json, Dynamic.class);
    }

    /**
     * publishQuestion ?
     * @param content 
     * @param detail 
     * @param topics ?
     * @return ?PublishQuestionResult
     */
    public Result publishQuestion(String content, String detail, ArrayList<String> topics) {
        Map<String, String> params = new HashMap<>();
        params.put("question_content", content);
        params.put("question_detail", detail);
        // ??
        StringBuilder topic = new StringBuilder();
        if (!topics.isEmpty()) {
            topic.append(topics.get(0));
            for (int i = 1; i < topics.size(); i++)
                topic.append(',').append(topics.get(i));
        }
        params.put("topics", topics.toString());
        String json = doPost(Config.PUSHLISH_QUESTION, params);
        return getResult(json, PublishQuestion.class);
    }

    /**
     * publishAnswer 
     * @param questionID ?
     * @param content 
     * @return ?PublishAnswerResult
     */

    public Result publishAnswer(int questionID, String content) {
        Map<String, String> params = new HashMap<>();
        params.put("question_id", questionID + "");
        params.put("answer_content", content);
        String json = doPost(Config.PUSHLISH_ANSWER, params);
        return getResult(json, PublishAnswer.class);
    }

    /**
     * postAction ?
     * @param type 
     * @param classType 
     * @param strs ?
     * @return ResultNULL
     */

    public Result postAction(Config.ActionType type, @NonNull Class<? extends Object> classType,
            ArrayList<String> strs) {

        Map<String, String> params = new HashMap<>();
        String json;
        if (type == Config.ActionType.QUESTION_FOCUS) {
            params.put("question_id", strs.get(0));
            json = doPost(Config.QUESTION_FOCUS, params);
            return getResult(json, classType);
        } else if (type == Config.ActionType.QUESTION_THANKS) {
            params.put("question_id", strs.get(0));
            json = doPost(Config.QUESTION_THANKS, params);
            return getResult(json, classType);
        } else if (type == Config.ActionType.PUSHLISH_ANSWER_COMMENT) {
            params.put("answer_id", strs.get(0));
            params.put("message", strs.get(1));
            json = doPost(Config.PUSHLISH_ANSWER_COMMENT, params);
            return getResult(json, classType);
        } else if (type == Config.ActionType.ANSWER_VOTE) {
            params.put("answer_id", strs.get(0));
            params.put("value", strs.get(1));
            json = doPost(Config.ANSWER_VOTE, params);
            return getResult(json, classType);
        } else if (type == Config.ActionType.ANSWER_RATE) {
            params.put("type", strs.get(0));
            params.put("answer_id", strs.get(1));
            json = doPost(Config.ANSWER_RATE, params);
            return getResult(json, classType);
        }
        return null;
    }

    /**
     * getResult JSON?Result
     * @param json JSON
     * @param classType 
     * @return ResultNULL
     */
    private Result getResult(String json, @NonNull Class<? extends Object> classType) {
        try {
            JSONObject jsonObject = new JSONObject(json);
            Result resualt = new Result();
            resualt.setErr(jsonObject.getString("err"));
            resualt.setErrno(jsonObject.getInt("errno"));
            if (resualt.getErrno() == 1) {
                Gson gson = new Gson();
                resualt.setRsm(gson.fromJson(jsonObject.getJSONObject("rsm").toString(), classType));
            } else
                resualt.setRsm(null);
            return resualt;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * getResultArray JSON?Result
     * @param json JSON
     * @param classType 
     * @return Result
     */

    private Result getResultArray(String json, @NonNull Class<? extends Object> classType) {
        try {

            JSONObject jsonObject = new JSONObject(json);
            Result result = new Result();
            result.setErr(jsonObject.getString("err"));
            result.setErrno(jsonObject.getInt("errno"));
            if (result.getErrno() == 1) {
                ArrayList<Object> list = new ArrayList<>();
                JSONArray array = jsonObject.getJSONArray("rsm");
                int size = array.length();
                Gson gson = new Gson();
                for (int i = 0; i < size; i++) {
                    JSONObject item = array.getJSONObject(i);
                    list.add(gson.fromJson(item.toString(), classType));
                }
                result.setRsm(list);
            } else
                result.setRsm(null);
            return result;
        } catch (JSONException e) {
            return null;
        }
    }

    /**
     * getResults JSON?Result
     * @param json JSON
     * @param classType 
     * @return Result
     */
    private Result getResults(String json, @NonNull Class<? extends Object> classType) {
        try {
            JSONObject jsonObject = new JSONObject(json);
            Result result = new Result();
            result.setErr(jsonObject.getString("err"));
            result.setErrno(jsonObject.getInt("errno"));
            if (result.getErrno() == 1) {
                ArrayList<Object> list = new ArrayList<>();
                JSONObject rsm = jsonObject.getJSONObject("rsm");
                JSONArray array = rsm.getJSONArray("rows");
                int total_rows = rsm.getInt("total_rows");
                Gson gson = new Gson();
                for (int i = 0; i < total_rows; i++) {
                    JSONObject item = array.getJSONObject(i);
                    list.add(gson.fromJson(item.toString(), classType));
                }
                result.setRsm(list);
            } else
                result.setRsm(null);
            return result;
        } catch (JSONException e) {
            return null;
        }
    }

    /**
     * doPost ??POST
     * @param URL URL
     * @param params ?
     * @return ?NULL
     */
    private String doPost(String URL, Map<String, String> params) {
        // 
        StringBuilder builder = new StringBuilder();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            builder.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue())).append("&");
        }
        builder.deleteCharAt(builder.length() - 1);
        byte[] data = builder.toString().getBytes();
        // ?
        try {
            URL url = new URL(URL);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setConnectTimeout(Config.TIME_OUT);
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            connection.setUseCaches(false);
            // Cookie
            connection.setRequestProperty("Cookie", cooike);
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.setRequestProperty("Content-Length", String.valueOf(data.length));
            // ??
            OutputStream output = connection.getOutputStream();
            output.write(data);
            // ?
            int response = connection.getResponseCode();
            if (response == HttpURLConnection.HTTP_OK) {
                // ?Cookie
                Map<String, List<String>> header = connection.getHeaderFields();
                List<String> cookies = header.get("Set-Cookie");
                if (cookies.size() == 3)
                    cooike = cookies.get(2);
                // ??
                InputStream input = connection.getInputStream();
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                byte[] buffer = new byte[Config.MAX_LINE_BUFFER];
                int len = 0;
                while ((len = input.read(buffer)) != -1)
                    byteArrayOutputStream.write(buffer, 0, len);
                return new String(byteArrayOutputStream.toByteArray());
            }
        } catch (IOException e) {
            return null;
        }
        return null;
    }

    /**
     * doGet ??GET
     * @param URL URL?
     * @return ?NULL
     */
    private String doGet(String URL) {
        try {
            URL url = new URL(URL);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            // Cookie
            connection.setRequestProperty("Cookie", cooike);
            InputStreamReader input = new InputStreamReader(connection.getInputStream());
            BufferedReader reader = new BufferedReader(input);
            StringBuilder builder = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null)
                builder.append(line);
            return builder.toString();
        } catch (IOException e) {
            return null;
        }
    }
}