okHttp的请求超时以后,连续发送5次请求后就不能再发送请求了

首先给大家推荐一下我老师大神的人工智能教学网站。教学不仅零基础,通俗易懂,而且非常风趣幽默,还时不时有内涵黄段子!点这里可以跳转到网站

项目之中遇到了OkHttp 请求超时以后就不能发送请求了,

解决办法

在onFail的里面再添加一下HttpClient.mOkHttpClient = new OkHttpClient(); 

public void onFailure(Call call, IOException e) {
                HttpClient.mOkHttpClient = new OkHttpClient();
                callback.onFailure(call,e);
            }

package com.wbm.app.business.net; import android.util.Log; import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import com.alibaba.fastjson.TypeReference;import com.wbm.app.commons.Result;import com.wbm.app.commons.ResultList;import com.wbm.app.utils.DateUtil; import java.io.IOException;import java.math.BigDecimal;import java.util.Date;import java.util.List;import java.util.Map; import okhttp3.Call;import okhttp3.Callback;import okhttp3.FormBody;import okhttp3.OkHttpClient;import okhttp3.Request;import okhttp3.Response; /** *  */public class BopHttpClient {      /**     * 调用java后端服务     * @param domain     * @param method     * @param map     * @param callback     */    public static void callHttpMethod(String domain,String method,String session, Map<String,String> map, final Callback callback){         FormBody.Builder bodyBuilder = new FormBody.Builder()                .add("method", method).add("session",session)                .add("timestamp", DateUtil.formateDate(new Date())).add("app_key",APP_KEY)                .add("format","json");         if(map!=null && map.size()>0){            for(Map.Entry<String,String> entry:map.entrySet()){                bodyBuilder.add(entry.getKey(),entry.getValue());            }        }         FormBody requestBody = bodyBuilder.build();         //创建一个Request        final Request request = new Request.Builder()                .url("https://"+domain+":8080/gateway/api").post(requestBody)                .build();         Call call = HttpClient.mOkHttpClient.newCall(request);         call.enqueue(new Callback() {            @Override            public void onFailure(Call call, IOException e) {                //TODO                HttpClient.mOkHttpClient = new OkHttpClient();                callback.onFailure(call,e);            }             @Override            public void onResponse(Call call, Response response) throws IOException {                //TODO                callback.onResponse(call,response);             }        });    }     public static <K,T extends Result<K>> void callHttpMethod4Result(String domain,String method,String session, Map<String,String> map, final WrapCallback<T> callback,final K k){        callHttpMethod(domain,method, session,map,new Callback() {            @Override            public void onFailure(Call call, IOException e) {                HttpClient.mOkHttpClient = new OkHttpClient();                 callback.onFailure(call,e);            }            @Override            public void onResponse(Call call, Response response) throws IOException {                try {                    String responseStr = response.body().string();                    if (response.code() >= 200 && response.code() < 300) {                        Result<K> responseObject = JSON.parseObject(responseStr, new TypeReference<Result<K>>() {                        });                        if (responseObject.getData() != null) {                            K valueObject = null;                            Class<?> targetType= k.getClass();                            if (targetType == String.class) {                                valueObject =  (K)responseObject.getData().toString();                            }                            else if ((targetType == Integer.class) || (targetType == Integer.TYPE))                                valueObject =  (K)Integer.valueOf(responseObject.getData().toString());                            else if ((targetType == Byte.class) || (targetType == Byte.TYPE))                                valueObject =  (K)Byte.valueOf(responseObject.getData().toString());                            else if ((targetType == Double.class) || (targetType == Double.TYPE))                                valueObject =  (K)Double.valueOf(responseObject.getData().toString());                            else if ((targetType == Float.class) || (targetType == Float.TYPE))                                valueObject =  (K)Float.valueOf(responseObject.getData().toString());                            else if ((targetType == Long.class) || (targetType == Long.TYPE))                                valueObject =  (K)Long.valueOf(responseObject.getData().toString());                            else if ((targetType == Short.class) || (targetType == Short.TYPE))                                valueObject =  (K)Short.valueOf(responseObject.getData().toString());                            else if (targetType == BigDecimal.class )                                valueObject =  (K)new BigDecimal(responseObject.getData().toString());                            else{                                valueObject = (K) JSONObject.toJavaObject((JSONObject) responseObject.getData(), k.getClass());                            }                            responseObject.setData(valueObject);                        }                        callback.onResponse(call, (T) responseObject);                    } else {//                        Result<K> responseObject = new Result<K>();//                        responseObject.setError(ErrorCodeConstants.SYSTEMERROR_999999.getErrorCode(), ErrorCodeConstants.SYSTEMERROR_999999.getErrorName(), ErrorCodeConstants.SYSTEMERROR_999999.getErrorMessage());//                        callback.onResponse(call, (T) responseObject);                        onFailure(call,new IOException());                    }                }catch (Throwable e){//                    throw e;                    onFailure(call,new IOException(e));                }             }        });    }     public static <K,T extends ResultList<K>> void callHttpMethod4ResultList(String domain,String method,String session, Map<String,String> map, final WrapCallback<T> callback,final K k){        callHttpMethod(domain,method, session,map,new Callback() {            @Override            public void onFailure(Call call, IOException e) {              <strong><span style="color:#33cc00;"> </span><span style="color:#ff6666;"> HttpClient.mOkHttpClient = new OkHttpClient();</span></strong>                 callback.onFailure(call,e);            }            @Override            public void onResponse(Call call, Response response) throws IOException {                try {                    String responseStr = response.body().string();                    if (response.code() >= 200 && response.code() < 300) {                        ResultList<K> responseObject = JSON.parseObject(responseStr, new TypeReference<ResultList<K>>() {                        });                        if (responseObject.getDatalist() != null) {                            String valueString = JSON.toJSONString(responseObject.getDatalist());                            List<K> values = (List<K>) JSON.parseArray(valueString, k.getClass());                            responseObject.setDatalist(values);                        }                        callback.onResponse(call, (T) responseObject);                    } else {                        onFailure(call,new IOException());                    }                }catch (Throwable e){                    onFailure(call,new IOException(e));                }            }        });    }  }

点这里可以跳转到人工智能网站

发表评论