Use the stetho interceptor with Chrome to debug network requests.

Source: Internet
Author: User

Use the stetho interceptor with Chrome to debug network requests.
First, integration.Stetho Development Kit and introductionRxJava framework

Apply plugin: 'com. android. application 'android {compileSdkVersion 26 defaultConfig {applicationId "com. example. nc039.fanldemo "minSdkVersion 19 targetSdkVersion 26 versionCode 1 versionName" 1.0 "testInstrumentationRunner" android. support. test. runner. androidJUnitRunner "} buildTypes {release {minifyEnabled false proguardFiles getdefadefaproguardfile('proguard-android.txt '), 'proguard-rules. pro' }}dependencies {implementation fileTree (dir: 'libs', include :['*. jar ']) implementation 'com. android. support: appcompat-v7: 26.1.0 'implementation' com. android. support. constraint: constraint-layout: 1.0.2 'implementation' com. amitshekhar. android: android-networking: 1.0.1 'testimplementation' junit: 4.12 'androidtestimplementation' com. android. support. test: runner: 1.0.1 'androidtestimplementation' com. android. support. test: rules: 1.0.1 'androidtestimplementation' com. android. support. test. espresso: espresso-core: 3.0.1 'implementation' com. android. support. test. espresso: espresso-intents: 3.0.1 'implementation' com. amitshekhar. android: jackson-android-networking: 1.0.1 '// -- Stetho Development Kit start -- implementation 'com. facebook. stetho: 1.3.1 'implementation' com. facebook. stetho: stetho-okhttp3: 1.3.1 '// -- Stetho Development Kit end -- // -- RxJava2.0 start -- implementation' io. reactivex. rxjava2: rxandroid: 2.0.1 'implementation' io. reactivex. rxjava2: rxjava: 2.1.0 '// -- RxJava2.0 end --}
Initialize Stetho interceptor in onCreate of Activity
 Stetho.initializeWithDefaults(this);
Use the OkHttp3.0 framework of the current API for network requests and add interceptors
 private OkHttpClient okHttpClient=new OkHttpClient.Builder() .addNetworkInterceptor(new StethoInterceptor()) .build();
Test Cases

To facilitate the comparison, use the unredirected https://www.baidu.com and the redirected https://www.baidu.com to test, use the ChromeInspect viewer observe test process

Set button click event, useInspect viewer captures network request process:
 @Override public void onClick(View v) { switch(v.getId()){ case R.id.btnStart1: url="https://www.baidu.com"; Request request = new Request.Builder() .url(url) .build(); final Call call1 = okHttpClient.newCall(request); updateUI(call1); break; case R.id.btnStart2: url="https://www.baidu.com"; request = new Request.Builder() .url(url) .build(); final Call call2 = okHttpClient.newCall(request); updateUI(call2); } }
The request results are synchronously displayed using RxJava:
 private void updateUI(final Call call){ Observable.create(new ObservableOnSubscribe  (){ @Override public void subscribe(@NonNull ObservableEmitter  e) throws Exception { Response response = call.execute(); e.onNext(response.body().string()); e.onComplete(); } }).subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer  () { @Override public void onSubscribe(@NonNull Disposable d) { } @Override public void onNext(@NonNull String ret) { tvRet.setText(ret); } @Override public void onError(@NonNull Throwable e) { e.printStackTrace(); } @Override public void onComplete() { } }); }   
Test Results

Returned results of unredirected addresses

The app returns html page information:

Chrome viewer results:

The return code is 200. It is normal. It is a GET request and the User-Agent is okhttp/3.9.1.

Returned results of the redirected address

Redirection rules returned by the app:

Chrome viewer results:

The return code is 302, and the address is redirected to the URL corresponding to Location in Response Headers.

Purpose

It can be used to debug Network Interface data during project development. With the above debugging methods, you can further determine whether the problem lies in the server or client, network request interface debugging is a almost transparent process.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.