Android uses okhttp to parse JSON data

For code reuse, we first encapsulate a class. This class is httputil

HttpUtil.java

Then, according to the old rule, write an entity class containing the get / set method, which is named app.java for convenience

App.java

Finally, write mainactivity.java

At the same time, it can be written in this way for the sake of code optimization and no fault in UI thread operation.

MainActivity.java

Does it feel comfortable to drive like this? This way of writing avoids the blocking of Android program network requests affecting the main thread to the greatest extent. Of course, you can directly pull over and copy this code.

In Java 1.8, lambda expressions seem to be added, so what can we do with expressions?

The simplest is to simplify the writing operation. For example, the thread free method under showresponse in mainactivity.java can be written like this:

In this way, it is much simpler. In addition, click events are also very suitable for lambda expressions. Using expressions can simplify click events of a button in this way:

Or write it like this:

That's all for now. I just switched from Java to Android. The code can only be like this. There must be better ones in the future.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>