OkHttp Library - NetworkOnMainThreadException on simple post

后端 未结 3 1770
北海茫月
北海茫月 2020-11-28 06:52

I want to use OkHttp library for networking in Android. I started with the simple post example as written in their website:

public static final MediaType JSO         


        
3条回答
  •  独厮守ぢ
    2020-11-28 07:37

    According to the OkHttp docs: It supports both synchronous blocking calls and async calls with callbacks. Your example is on main thread and Android since version 3.0 throws that exception if you try to do network calls on main thread

    Better option is to use it together with retrofit and Gson: http://square.github.io/retrofit/ https://code.google.com/p/google-gson/

    Here are the examples: http://engineering.meetme.com/2014/03/best-practices-for-consuming-apis-on-android/ http://heriman.net/?p=5

提交回复
热议问题