Giter Site home page Giter Site logo

troyzhxu / okhttps Goto Github PK

View Code? Open in Web Editor NEW
478.0 11.0 75.0 1.77 MB

如艺术一般优雅,像 1、2、3 一样简单,前后端通用,轻量却强大的 HTTP 客户端(同时支持 WebSocket 与 Stomp 协议)

Home Page: https://ok.zhxu.cn

License: Apache License 2.0

Java 100.00%
okhttps-okhttp3 httputils okhttp android okhttp3 okhttputils okhttp3-util download-manager upload download

okhttps's Issues

Crash when throw JsonSyntaxException and doesn't callback through exceptionListener or setOnException.

Describe the bug
服务器正常应返回json,但是异常情况下返回html,exceptionListener 和 setOnException不会走直接崩溃

    com.ejlchina.okhttps.internal.HttpException: 转换失败
     Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $
     Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

To Reproduce
这样用的

http = HTTP.builder()
                .addMsgConvertor(new GsonMsgConvertor())
                .baseUrl(Constant.getApiBaseUrl())
                .exceptionListener((HttpTask<?> task, IOException error) -> {
                  error.printStackTrace();
                  return true;
                })
                .build();

try{
            http.async(Constant.LOGIN_SUFFIX)
                    .bodyType("json")
                    .setBodyPara(user)
                    .setOnResBean(new TypeRef<BaseResp<LoginResp>>() {}, result -> {
                        callback.onResponse(result);
                    })
                    .setOnException(ex ->{
                        callback.onFail(ex);
                    })
                    .post();
        }catch (Exception e){
            e.printStackTrace();
        }

Expected behavior
期望 json转换异常也会在onexception抛出.

Smartphone (please complete the following information):

  • Device: HongMi2s
  • OS: Android 7.1.2
  • Version OKHTTPS 3.0.0-Gson

经常会报The destination [" + destination + "] has already been subscribed!这个异常

每次都是在Stomp连接成功的回调里面开始订阅,订阅之前会执行一次取消订阅方法,但是有时候过了很久APP会奔溃报上述异常;
还有登录成功或者打开首页都会执行Stomp的连接方法导致同时可能发起多个连接请求;只通过stomp.isConnected()判断也不可靠,因为有时候真在连接中
//设置监听
stomp.setOnConnected(new OnCallback() {
@OverRide
public void on(Stomp data) {
// 服务器连接成功回调
Log.i("Stomp", "OnConnected");
//开始订阅消息
subscribe();
}
});

       //取消订阅
        stomp.unsubscribe(destination);
        //订阅
        stomp.subscribe(destination,null,(com.ejlchina.stomp.Message msg) -> {

      }

WebSocket 连接异常

E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
Process: com.dppdkai.luckypandemo, PID: 10154
com.ejlchina.okhttps.internal.HttpException: WebSockt 连接异常: ws://myphp.com
at com.ejlchina.okhttps.internal.WebSocketTask$MessageListener.onFailure(WebSocketTask.java:278)
at okhttp3.internal.ws.RealWebSocket.failWebSocket(RealWebSocket.java:570)
at okhttp3.internal.ws.RealWebSocket$1.onResponse(RealWebSocket.java:199)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:174)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:929)
Caused by: java.net.ProtocolException: Expected HTTP 101 response but was '200 OK'
at okhttp3.internal.ws.RealWebSocket.checkUpgradeSuccess(RealWebSocket.java:224)
at okhttp3.internal.ws.RealWebSocket$1.onResponse(RealWebSocket.java:195)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:174) 
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
at java.lang.Thread.run(Thread.java:929) 

okhttps-gson:3.0.0版本是不是有问题,HTTP这个类就没有

Describe the bug
A clear and concise description of what the bug is.

To Reproduce
Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: [e.g. iOS]
  • Browser [e.g. chrome, safari]
  • Version [e.g. 22]

Smartphone (please complete the following information):

  • Device: [e.g. iPhone6]
  • OS: [e.g. iOS8.1]
  • Browser [e.g. stock browser, safari]
  • Version [e.g. 22]

Additional context
Add any other context about the problem here.

OkHttps.sync

请问下 OkHttps.sync 该用法怎么配置proxy 代理地址和端口?

Stomp有办法销毁websocket吗

在activity界面初始化连接stomp activity destory调用的disconnect之后回到首页 断网之后还能收到setOnException里面的回调 disconnect没有销毁websocket吗

使用Stomp出现粘包问题

stomp.topic("/your-topic", (Message msg)
订阅收到数据后,我的Message是多条消息,请问这个该如何解析?

WebSocket 连接异常

10-25 12:36:18.967 7197-7215/? E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
Process: com.dppdkai.luckypandemo, PID: 7197
com.ejlchina.okhttps.internal.HttpException: WebSockt 连接异常: ws://127.0.0.1/aka/dserver
at com.ejlchina.okhttps.internal.WebSocketTask$MessageListener.onFailure(WebSocketTask.java:278)
at okhttp3.internal.ws.RealWebSocket.failWebSocket(RealWebSocket.java:570)
at okhttp3.internal.ws.RealWebSocket$1.onResponse(RealWebSocket.java:199)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:174)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.net.ProtocolException: Expected HTTP 101 response but was '403 Forbidden'
at okhttp3.internal.ws.RealWebSocket.checkUpgradeSuccess(RealWebSocket.java:224)
at okhttp3.internal.ws.RealWebSocket$1.onResponse(RealWebSocket.java:195)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:174) 
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
at java.lang.Thread.run(Thread.java:818) 

没有匹配[json]类型的转换器!

Describe the bug

Exception in thread "main" com.ejlchina.okhttps.internal.HttpException: 没有匹配[json]类型的转换器!
	at com.ejlchina.okhttps.internal.TaskExecutor.doMsgConvert(TaskExecutor.java:145)
	at com.ejlchina.okhttps.HttpTask.toRequestBody(HttpTask.java:699)
	at com.ejlchina.okhttps.HttpTask.buildRequestBody(HttpTask.java:689)
	at com.ejlchina.okhttps.HttpTask.prepareRequest(HttpTask.java:596)
	at com.ejlchina.okhttps.HttpTask.prepareCall(HttpTask.java:585)
	at com.ejlchina.okhttps.internal.AsyncHttpTask.lambda$request$0(AsyncHttpTask.java:263)
	at com.ejlchina.okhttps.internal.HttpClient.preprocess(HttpClient.java:184)
	at com.ejlchina.okhttps.internal.AsyncHttpTask.request(AsyncHttpTask.java:255)
	at com.ejlchina.okhttps.internal.AsyncHttpTask.post(AsyncHttpTask.java:217)
	at com.hellosouth.file_transfer.utils.OkHttpUtils.main(OkHttpUtils.java:231)

To Reproduce

  Map<String, Object> map = new HashMap<>();
        map.put("uploadId", "uploadId");
//        map.put("base64",base64);
        map.put("total", 10);
        map.put("current", 2);
        HTTP http = HTTP.builder().bodyType(OkHttps.JSON).build();
        http.async(UPLOAD_URL)
                .bodyType(OkHttps.JSON)
                .addBodyPara(map)
//                .setBodyPara(map)
                .setOnException((IOException e) -> {
                    // 异常回调
                    e.printStackTrace();
                })
                .setOnResponse((HttpResult res) -> {

                    // 响应回调
                    int status = res.getStatus();       // 状态码
                    Headers headers = res.getHeaders(); // 响应头
                    HttpResult.Body body = res.getBody();          // 报文体
                    System.out.println(body);

                })
                .post();
  <!-- okhttps -->
        <dependency>
            <groupId>com.ejlchina</groupId>
            <artifactId>okhttps</artifactId>
            <version>2.4.2</version>
        </dependency>

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: [e.g. iOS]
  • Browser [e.g. chrome, safari]
  • Version [e.g. 22]

Smartphone (please complete the following information):

  • Device: [e.g. iPhone6]
  • OS: [e.g. iOS8.1]
  • Browser [e.g. stock browser, safari]
  • Version [e.g. 22]

Additional context
Add any other context about the problem here.

Stomp 使用的是自签的https 需要配置sslSocketFactory和hostnameVerifier

WebSocketTask webSocketTask = OkHttps.webSocket(UrlConfig.WS_STOMP);
stomp = Stomp.over(webSocketTask);
这么没有api可以获取到OkHttps中的http来设置sslSocketFactory

public static WebSocketTask webSocket(String url) {
return getHttp().webSocket(url);
}

static synchronized HTTP getHttp() {
if (http != null) {
return http;
}
HTTP.Builder builder = HTTP.builder();
ConvertProvider.inject(builder);
Config.config(builder);
http = builder.build();
return http;
}
我需要Https中的http对象
http .config(b -> {
b.sslSocketFactory(mySSLSocketFactory, myTrustManager);
b.hostnameVerifier(myHostnameVerifier);
})

STOMP心跳与服务器断开连接后没有任何操作和处理

com.ejlchina.okhttps.internal.WebSocketTask

	private void schedulePing() {
		if (!isConnected()) {
			return;
		}
		int delay = (int) (pingSeconds + lastPingSecs - nowSeconds());
		httpClient.executor.requireScheduler().schedule(() -> {
			if (!isConnected()) {
				return;
			}
			WebSocket ws = webSocket;
			if (nowSeconds() - lastPingSecs >= pingSeconds && ws != null) {
				ByteString ping = pingSupplier != null ? pingSupplier.getPing() : ByteString.EMPTY;
				ws.send(ping);
				lastPingSecs = nowSeconds();
			}
			schedulePing();
		}, delay, TimeUnit.SECONDS);
	}

	/**
	 * 检测服务器的心跳响应
	 */
	private void schedulePong() {
		if (!isConnected()) {
			return;
		}
		int delay = (int) (pongSeconds + lastPongSecs - nowSeconds());
		httpClient.executor.requireScheduler().schedule(() -> {
			if (!isConnected()) {
				return;
			}
			long noPongSeconds = nowSeconds() - lastPongSecs;
			if (noPongSeconds > 3L * pongSeconds) {
				WebSocketImpl ws = webSocket;
				if (ws != null) {
					Exception e = new SocketTimeoutException("Server didn't pong heart-beat on time. Last received at " + noPongSeconds + " seconds ago.");
					((RealWebSocket) ws.webSocket).failWebSocket(e, null);
				}
			} else {
				schedulePong();
			}
		}, delay, TimeUnit.SECONDS);
	}

如代码中所示,两个心跳检测中,判断与服务器连接断开!isConnected()后,直接return,此处是否应该抛出异常或触发onDisconnected回调。

在接入Stomp后,如果app网络断开,App直接奔溃,加try-catch也还是奔溃

com.ejlchina.okhttps.internal.HttpException: WebSockt 连接异常: ws://192.168.101.233:10002/api/ws/websocket
at com.ejlchina.okhttps.internal.WebSocketTask$MessageListener.onFailure(WebSocketTask.java:252)
at okhttp3.internal.ws.RealWebSocket.failWebSocket(RealWebSocket.java:570)
at okhttp3.internal.ws.RealWebSocket$1.onFailure(RealWebSocket.java:216)
at okhttp3.RealCall$AsyncCall.execute(RealCall.java:180)
at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:776)
Caused by: java.net.ConnectException: Failed to connect to /192.168.101.233:10002

WHttpTask onFailure 非能重置webSocket连接中状态

Describe the bug
STOMP 重连时, 服务器关闭状态下在次调用stomp.connect()时, WebSocket status 任然是 STATUS_CONNECTING.
主要原因在WHttpTask.onFailure中未能重置WebSocket状态

To Reproduce
Steps to reproduce the behavior:

  1. Go to '...'
  2. Click on '....'
  3. Scroll down to '....'
  4. See error

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: [e.g. iOS]
  • Browser [e.g. chrome, safari]
  • Version [e.g. 22]

Smartphone (please complete the following information):

  • Device: [e.g. iPhone6]
  • OS: [e.g. iOS8.1]
  • Browser [e.g. stock browser, safari]
  • Version [e.g. 22]

Additional context
Add any other context about the problem here.

没有添加报文体参数时,POST 请求都会以 form 表单的形式提交

形如以下代码:

OkHttps.sync("http://xxxxx")
    .bodyType(OkHttps.JSON)
    .post();

期望发出的 Content-Typeapplication/json,但实际发出的却是 application/x-www-form-urlencoded

即:
所有应该有报文体,但没有添加 bodyPara 的请求,都会默认以空表单的形式提交。

当前的可行的避免方案有:

OkHttps.sync("http://xxxxx")
    .bodyType(OkHttps.JSON)
    .addBodyPara(null)    // 添加一个 空 报文体参数
    .post();

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.