Skip to content

Commit

Permalink
使用rxjava+rxandroid重构部分代码
Browse files Browse the repository at this point in the history
  • Loading branch information
Z-P-J committed Feb 8, 2020
1 parent b79e3a3 commit a4b5714
Show file tree
Hide file tree
Showing 22 changed files with 1,591 additions and 208 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ dependencies {
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

implementation("com.squareup.okhttp3:okhttp:4.2.2")
implementation 'io.reactivex.rxjava2:rxjava:2.2.17'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
// implementation "io.reactivex.rxjava3:rxjava:3.0.0-RC9"

implementation project(':zhttp')
}
152 changes: 86 additions & 66 deletions app/src/main/java/com/zpj/http/demo/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package com.zpj.http.demo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.zpj.http.ZHttp;
import com.zpj.http.core.IHttp;

import java.io.IOException;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class MainActivity extends AppCompatActivity {

Expand All @@ -24,71 +22,93 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView contentText = findViewById(R.id.text_content);
new Thread(new Runnable() {
@Override
public void run() {
try {
// long time3 = System.currentTimeMillis();
// Log.d(TAG, "time3=" + time3);
// OkHttpClient client = new OkHttpClient();
// Request request = new Request.Builder()
// .url("https://www.baidu.com/")
// .build();
// try (Response response = client.newCall(request).execute()) {
// final String content = response.body().string();
// long time4 = System.currentTimeMillis();
// Log.d(TAG, "time4=" + time4);
// Log.d(TAG, "delta2=" + (time4 - time3));
//// contentText.post(new Runnable() {
//// @Override
//// public void run() {
//// contentText.setText(content);
//// }
//// });
// }





// long time1 = System.currentTimeMillis();
// Log.d(TAG, "time1=" + time1);
// final String body = ZHttp.get("https://www.baidu.com/").toStr();
// long time2 = System.currentTimeMillis();
// Log.d(TAG, "time2=" + time2);
// Log.d(TAG, "delta1=" + (time2 - time1));
// new Thread(new Runnable() {
// @Override
// public void run() {
// try {
//// long time3 = System.currentTimeMillis();
//// Log.d(TAG, "time3=" + time3);
//// OkHttpClient client = new OkHttpClient();
//// Request request = new Request.Builder()
//// .url("https://www.baidu.com/")
//// .build();
//// try (Response response = client.newCall(request).execute()) {
//// final String content = response.body().string();
//// long time4 = System.currentTimeMillis();
//// Log.d(TAG, "time4=" + time4);
//// Log.d(TAG, "delta2=" + (time4 - time3));
////// contentText.post(new Runnable() {
////// @Override
////// public void run() {
////// contentText.setText(content);
////// }
////// });
//// }
//
//
//
//
//
//// long time1 = System.currentTimeMillis();
//// Log.d(TAG, "time1=" + time1);
//// final String body = ZHttp.get("https://www.baidu.com/").toStr();
//// long time2 = System.currentTimeMillis();
//// Log.d(TAG, "time2=" + time2);
//// Log.d(TAG, "delta1=" + (time2 - time1));
//// contentText.post(new Runnable() {
//// @Override
//// public void run() {
//// contentText.setText(body);
//// }
//// });
//
// // https://api.heweather.com/x3/weather
//// "city", "beijing"
//// "key", "d17ce22ec5404ed883e1cfcaca0ecaa7"
// final String b = ZHttp.get("https://api.heweather.com/x3/weather")
// .data("city", "beijing")
// .data("key", "d17ce22ec5404ed883e1cfcaca0ecaa7")
// .onRedirect(new IHttp.OnRedirectListener() {
// @Override
// public boolean onRedirect(String redirectUrl) {
// Log.d("onRedirect", "redirectUrl=" + redirectUrl);
// return true;
// }
// })
// .toStr();
// contentText.post(new Runnable() {
// @Override
// public void run() {
// contentText.setText(body);
// contentText.setText(b);
// }
// });
//
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// }).start();

// https://api.heweather.com/x3/weather
// "city", "beijing"
// "key", "d17ce22ec5404ed883e1cfcaca0ecaa7"
final String b = ZHttp.get("https://api.heweather.com/x3/weather")
.data("city", "beijing")
.data("key", "d17ce22ec5404ed883e1cfcaca0ecaa7")
.onRedirect(new IHttp.OnRedirectListener() {
@Override
public boolean onRedirect(String redirectUrl) {
Log.d("onRedirect", "redirectUrl=" + redirectUrl);
return true;
}
})
.toStr();
contentText.post(new Runnable() {
@Override
public void run() {
contentText.setText(b);
}
});
ZHttp.get("https://api.heweather.com/x3/weather")
.data("city", "beijing")
.data("key", "d17ce22ec5404ed883e1cfcaca0ecaa7")
.onRedirect(new IHttp.OnRedirectListener() {
@Override
public boolean onRedirect(String redirectUrl) {
Log.d("onRedirect", "redirectUrl=" + redirectUrl);
return true;
}
})
.toStr()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.onSuccess(new IHttp.OnSuccessListener<String>() {
@Override
public void onSuccess(String data) {
contentText.setText(data);
}
})
.subscribe();

} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
}
4 changes: 4 additions & 0 deletions zhttp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

// implementation("com.squareup.okhttp3:okhttp:4.2.2")
implementation 'io.reactivex.rxjava2:rxjava:2.2.17'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
}
44 changes: 22 additions & 22 deletions zhttp/src/main/java/com/zpj/http/ZHttp.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,28 +196,28 @@ public static Document parseBodyFragment(String bodyHtml) {
return Parser.parseBodyFragment(bodyHtml, "");
}

/**
Fetch a URL, and parse it as HTML. Provided for compatibility; in most cases use {@link #connect(String)} instead.
<p>
The encoding character set is determined by the content-type header or http-equiv meta tag, or falls back to {@code UTF-8}.
@param url URL to fetch (with a GET). The protocol must be {@code http} or {@code https}.
@param timeoutMillis Connection and read timeout, in milliseconds. If exceeded, IOException is thrown.
@return The parsed HTML.
@throws java.net.MalformedURLException if the request URL is not a HTTP or HTTPS URL, or is otherwise malformed
@throws HttpStatusException if the response is not OK and HTTP response errors are not ignored
@throws UnsupportedMimeTypeException if the response mime type is not supported and those errors are not ignored
@throws java.net.SocketTimeoutException if the connection times out
@throws IOException if a connection or read error occurs
@see #connect(String)
*/
public static Document parse(URL url, int timeoutMillis) throws IOException {
Connection con = ConnectionFactory.createHttpConnection(url);
con.timeout(timeoutMillis);
return con.toHtml();
}
// /**
// Fetch a URL, and parse it as HTML. Provided for compatibility; in most cases use {@link #connect(String)} instead.
// <p>
// The encoding character set is determined by the content-type header or http-equiv meta tag, or falls back to {@code UTF-8}.
//
// @param url URL to fetch (with a GET). The protocol must be {@code http} or {@code https}.
// @param timeoutMillis Connection and read timeout, in milliseconds. If exceeded, IOException is thrown.
// @return The parsed HTML.
//
// @throws java.net.MalformedURLException if the request URL is not a HTTP or HTTPS URL, or is otherwise malformed
// @throws HttpStatusException if the response is not OK and HTTP response errors are not ignored
// @throws UnsupportedMimeTypeException if the response mime type is not supported and those errors are not ignored
// @throws java.net.SocketTimeoutException if the connection times out
// @throws IOException if a connection or read error occurs
//
// @see #connect(String)
// */
// public static Document parse(URL url, int timeoutMillis) throws IOException {
// Connection con = ConnectionFactory.createHttpConnection(url);
// con.timeout(timeoutMillis);
// return con.toHtml();
// }

/**
Get safe HTML from untrusted input HTML, by parsing input HTML and filtering it through a white-list of permitted
Expand Down
Loading

0 comments on commit a4b5714

Please sign in to comment.