diff --git a/app/src/main/java/org/ntlab/developrx/AddressHttpConnection.java b/app/src/main/java/org/ntlab/developrx/AddressHttpConnection.java new file mode 100644 index 0000000..76519a2 --- /dev/null +++ b/app/src/main/java/org/ntlab/developrx/AddressHttpConnection.java @@ -0,0 +1,11 @@ +package org.ntlab.developrx; + +/** + * Created by matsumoto_k on 2017/11/17. + */ + +public class AddressHttpConnection extends ObservableHttpConnection { + public AddressHttpConnection() { + super("http://zipcloud.ibsnet.co.jp"); + } +} diff --git a/app/src/main/java/org/ntlab/developrx/JavaMainActivity.java b/app/src/main/java/org/ntlab/developrx/JavaMainActivity.java index b5445ec..2895bfb 100644 --- a/app/src/main/java/org/ntlab/developrx/JavaMainActivity.java +++ b/app/src/main/java/org/ntlab/developrx/JavaMainActivity.java @@ -6,11 +6,15 @@ import java.util.concurrent.TimeUnit; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.schedulers.Schedulers; + /** * Created by matsumoto_k on 2017/11/11. */ public class JavaMainActivity extends AppCompatActivity { JavaRxProcess rxProcess = new JavaRxProcess(); + AddressHttpConnection addressHttpConnection = new AddressHttpConnection(); @Override protected void onCreate(Bundle savedInstanceState) { @@ -30,6 +34,7 @@ findViewById(R.id.flatmap_1_btn).setOnClickListener(view -> rxProcess.flatmap1()); findViewById(R.id.flatmap_2_btn).setOnClickListener(view -> rxProcess.flatmap2()); findViewById(R.id.meeting_btn).setOnClickListener(view -> rxProcess.meetingTest()); + SeekBar seekBar = findViewById(R.id.seek_bar); seekBar.setMax(1000); seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @@ -52,5 +57,18 @@ rxProcess.getPublishSubject() .throttleLast(500, TimeUnit.MILLISECONDS) .subscribe(progress -> System.out.println(progress)); + + findViewById(R.id.address_api_btn).setOnClickListener(view -> { + addressHttpConnection.addPathParam("api"); + addressHttpConnection.addPathParam("search"); + addressHttpConnection.addQueryParam("zipcode", "6731102"); + addressHttpConnection.doGet() + .subscribeOn(Schedulers.computation()) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe( + response -> System.out.println(response), + error -> error.printStackTrace(), + () -> System.out.println("onComplete")); + }); } } diff --git a/app/src/main/java/org/ntlab/developrx/ObservableHttpConnection.java b/app/src/main/java/org/ntlab/developrx/ObservableHttpConnection.java new file mode 100644 index 0000000..33abddb --- /dev/null +++ b/app/src/main/java/org/ntlab/developrx/ObservableHttpConnection.java @@ -0,0 +1,126 @@ +package org.ntlab.developrx; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.Reader; +import java.io.StringWriter; +import java.io.Writer; +import java.net.HttpURLConnection; +import java.net.URL; + +import io.reactivex.Observable; + +/** + * Created by matsumoto_k on 2017/11/16. + */ + +abstract public class ObservableHttpConnection { + private HttpURLConnection httpURLConnection = null; + private String baseUrl = ""; + private String pathParams = ""; + private String formParams = ""; + private String queryParams = ""; + private String method = ""; + + public Observable getResponse() { + return Observable.just(baseUrl) + .flatMap(url -> Observable.just(getHttp(new URL(baseUrl)))); + } + + public ObservableHttpConnection(String baseUrl) { + this.baseUrl = baseUrl; + } + + public String getHttp(URL url) { + try { + if (queryParams.isEmpty() || queryParams.isEmpty()) { + httpURLConnection = (HttpURLConnection) new URL(baseUrl + pathParams).openConnection(); + } else { + httpURLConnection = (HttpURLConnection) new URL(baseUrl + pathParams + "?" + queryParams).openConnection(); + } + httpURLConnection.setReadTimeout(10000); + httpURLConnection.setConnectTimeout(15000); + httpURLConnection.setRequestMethod(method); + if (formParams.length() > 0) { + httpURLConnection.setDoOutput(true); + OutputStream out = httpURLConnection.getOutputStream(); + out.write(formParams.getBytes("UTF-8")); + out.flush(); + out.close(); + formParams = ""; + } + pathParams = ""; + queryParams = ""; + httpURLConnection.connect(); + return toString(httpURLConnection.getInputStream()); + + } catch (IOException e) { + e.printStackTrace(); + } + return ""; + } + + public String toString(InputStream inputStream) { + Reader reader = null; + Writer writer = new StringWriter(); + char[] buffer = new char[1024]; + try { + reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); + int n; + while ((n = reader.read(buffer)) != -1) { + writer.write(buffer, 0, n); + } + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + reader.close(); + writer.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return writer.toString(); + } + + public Observable doGet() { + method = "GET"; + return getResponse(); + } + + public Observable doPost() { + method = "POST"; + return getResponse(); + } + + public Observable doPut() { + method = "PUT"; + return getResponse(); + } + + public Observable doDelete() { + method = "DELETE"; + return getResponse(); + } + + public void addQueryParam(String key, String value) { + if (queryParams.length() > 0) { + queryParams += ("&"); + } + queryParams += (key + "=" + value); + } + + public void addFormParam(String key, String value) { + if (formParams.length() > 0) { + formParams += "&"; + } + formParams += (key + "=" + value); + } + + public void addPathParam(String param) { + pathParams += "/" + param; + } +} diff --git a/app/src/main/res/layout/activity_main_java.xml b/app/src/main/res/layout/activity_main_java.xml index c2da997..e00779e 100644 --- a/app/src/main/res/layout/activity_main_java.xml +++ b/app/src/main/res/layout/activity_main_java.xml @@ -121,6 +121,14 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/meeting_btn" /> +