123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- package com.yyrh.networking.urlhttp;
- import android.text.TextUtils;
- import com.yyrh.behavior.ReportLogUtil;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.UnsupportedEncodingException;
- import java.net.HttpURLConnection;
- import java.net.URLEncoder;
- import java.util.List;
- import java.util.Map;
- import static com.yyrh.constant.URLConstants.ACTION_LOG_URL;
- /**
- * Created by fighting on 2017/4/24.
- */
- class RequestUtil{
- private Thread mThread;
- /**
- * 一般的get请求或post请求
- */
- RequestUtil(String method,String url, Map<String, String> paramsMap, Map<String, String> headerMap, CallBackUtil callBack) {
- switch (method){
- case "GET":
- urlHttpGet(url,paramsMap,headerMap,callBack);
- break;
- case "POST":
- urlHttpPost(url,paramsMap,null,headerMap,callBack);
- break;
- }
- }
- /**
- * post请求,传递json格式数据。
- */
- RequestUtil(String url, String jsonStr, Map<String, String> headerMap, CallBackUtil callBack) {
- urlHttpPost(url,null,jsonStr,headerMap,callBack);
- }
- /**
- * 上传文件
- */
- RequestUtil(String url, File file ,List<File> fileList,Map<String,File> fileMap,String fileKey,String fileType, Map<String,String> paramsMap, Map<String, String> headerMap,CallBackUtil callBack) {
- urlHttpUploadFile(url,file,fileList,fileMap,fileKey,fileType,paramsMap,headerMap,callBack);
- }
- /**
- * get请求
- */
- private void urlHttpGet(final String url, final Map<String, String> paramsMap, final Map<String, String> headerMap, final CallBackUtil callBack) {
- mThread = new Thread(new Runnable() {
- @Override
- public void run() {
- RealResponse response = new RealRequest().getData(getUrl(url,paramsMap),headerMap);
- if(response.code == HttpURLConnection.HTTP_OK){
- callBack.onSeccess(response);
- }else {
- callBack.onError(response);
- }
- }
- });
- }
- /**
- * post请求
- */
- private void urlHttpPost(final String url, final Map<String, String> paramsMap, final String jsonStr, final Map<String, String> headerMap, final CallBackUtil callBack) {
- mThread = new Thread(new Runnable() {
- @Override
- public void run() {
- RealResponse response = new RealRequest().postData(url, getPostBody(paramsMap,jsonStr),getPostBodyType(paramsMap,jsonStr),headerMap);
- if(response.code == HttpURLConnection.HTTP_OK){
- callBack.onSeccess(response);
- }else {
- callBack.onError(response);
- if (!url.startsWith(ACTION_LOG_URL)){
- resportErrorLog(url,response);
- }
- }
- }
- });
- }
- /**
- * 上报接口错误日志
- */
- private void resportErrorLog(String url,RealResponse response){
- String errorMessage = "";
- if(response.inputStream != null){
- errorMessage = getRetString(response.inputStream);
- }else if(response.errorStream != null) {
- errorMessage = getRetString(response.errorStream);
- }else if(response.exception != null) {
- errorMessage = response.exception.getMessage();
- }else {
- errorMessage = "";
- }
- String tag =url.split("/")[url.split("/").length-1];
- JSONObject json = new JSONObject();
- try {
- json.put("errorMessage",errorMessage);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- ReportLogUtil.reportErrorLog(tag,tag,json.toString());
- }
- /**
- * 上传文件
- */
- private void urlHttpUploadFile(final String url, final File file , final List<File> fileList, final Map<String,File> fileMap, final String fileKey, final String fileType, final Map<String,String> paramsMap, final Map<String, String> headerMap, final CallBackUtil callBack) {
- mThread = new Thread(new Runnable() {
- @Override
- public void run() {
- RealResponse response = null;
- response = new RealRequest().uploadFile(url, file,fileList,fileMap,fileKey,fileType,paramsMap,headerMap,callBack);
- if(response.code == HttpURLConnection.HTTP_OK){
- callBack.onSeccess(response);
- }else {
- callBack.onError(response);
- }
- }
- });
- }
- /**
- * get请求,将键值对凭接到url上
- */
- private String getUrl(String path,Map<String, String> paramsMap) {
- if(paramsMap != null){
- path = path+"?";
- for (String key: paramsMap.keySet()){
- path = path + key+"="+paramsMap.get(key)+"&";
- }
- path = path.substring(0,path.length()-1);
- }
- return path;
- }
- /**
- * 得到post请求的body
- */
- private String getPostBody(Map<String, String> params,String jsonStr) {//throws UnsupportedEncodingException {
- if(params != null){
- return getPostBodyFormParameMap(params);
- }else if(!TextUtils.isEmpty(jsonStr)){
- return jsonStr;
- }
- return null;
- }
- /**
- * 根据键值对参数得到body
- */
- private String getPostBodyFormParameMap(Map<String, String> params) {//throws UnsupportedEncodingException {
- StringBuilder result = new StringBuilder();
- boolean first = true;
- try {
- for (Map.Entry<String, String> entry : params.entrySet()) {
- if (first)
- first = false;
- else
- result.append("&");
- result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
- result.append("=");
- result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
- }
- return result.toString();
- } catch (UnsupportedEncodingException e) {
- return null;
- }
- }
- /**
- * 得到bodyType
- */
- private String getPostBodyType(Map<String, String> paramsMap, String jsonStr) {
- if(paramsMap != null){
- //return "text/plain";不知为什么这儿总是报错。目前暂不设置(20170424)
- return null;
- }else if(!TextUtils.isEmpty(jsonStr)){
- return "application/json;charset=utf-8";
- }
- return null;
- }
- /**
- * 开启子线程,调用run方法
- */
- void execute(){
- if(mThread != null){
- mThread.start();
- }
- }
- private static String getRetString(InputStream is) {
- String buf;
- try {
- BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"));
- StringBuilder sb = new StringBuilder();
- String line = "";
- while ((line = reader.readLine()) != null) {
- sb.append(line + "\n");
- }
- is.close();
- buf = sb.toString();
- return buf;
- } catch (Exception e) {
- return null;
- }
- }
- }
|