In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what is okhttp and okio in the analysis of Android web request framework". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "Android network request framework parsing what is okhttp and okio" it!
Android web request
Let's take a look at today's outline.
Import okhttp and okio dependencies
Disable the inspection of plaintext traffic requests
Add access permission
Layout and code implementation
Running result
Here are the concrete steps
Import okhttp and okio dependencies
1. Open File-Project Structure-Dependencies
two。 Select your own program file, click the plus sign, and select Library Dependency
3. Search for okhttp, select Com.squareup.okhttp3, and click the ok button, which may take a long time.
4.okio ditto
5. Apply, confirm
6. At this point we can see that Gradle Scripts-build.gradle (Module: My_Application.app) has two more dependencies.
Module: My_Application.app is its own corresponding app
II. Disable the inspection of plaintext traffic requests
1. Create a new xml folder under the res directory and a new nettools.xml under the xml folder
Nettools.xml
two。 Add the nettools.xml you just created to the manifests-AndroidManifest.xml
Android:networkSecurityConfig= "@ xml/nettools"
Third, add network request permissions
Add in manifests-AndroidManifest.xml
`
Fourth, code implementation 1. The realization of main code
MainActivity.java
Import androidx.annotation.UiThread;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.TextView;import java.io.IOException;import okhttp3.OkHttpClient;import okhttp3.Request;import okhttp3.Response;public class MainActivity extends AppCompatActivity {private Button btn; private TextView txt; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main) InitView ();} private void initView () {btn = findViewById (R.id.btn); txt = findViewById (R.id.txt); btn.setOnClickListener (new View.OnClickListener () {@ Override public void onClick (View view) {request ();}}) } protected void request () {new Thread (new Runnable () {@ Override public void run () {OkHttpClient client = new OkHttpClient (); Request request = new Request.Builder () .url ("https://www.baidu.com") .build ()) Response response = null; String string = null; try {response = client.newCall (request). Execute (); string = response.body (). String ();} catch (IOException e) {e.printStackTrace () } String finalString = string; runOnUiThread (new Runnable () {@ Override public void run () {txt.setText (finalString);}});}}) .start ();}} 2. Realization of main layout
Activity_main.xml
5. Operation results
If the operation fails, it may be a problem with the simulator. It is recommended to change the simulator or use the real machine directly.
At this point, I believe you have a deeper understanding of what is okhttp and okio in the parsing of the Android network request framework, so you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.