Skip to content

Instantly share code, notes, and snippets.

View xxhdpi's full-sized avatar

Jetfly xxhdpi

View GitHub Profile
@xxhdpi
xxhdpi / CoroutineTimer.kt
Created April 11, 2021 11:16 — forked from gmk57/CoroutineTimer.kt
Coroutine-based solution for delayed and periodic work
/**
* Coroutine-based solution for delayed and periodic work. May fire once (if [interval] omitted)
* or periodically ([startDelay] defaults to [interval] in this case), replacing both
* `Observable.timer()` & `Observable.interval()` from RxJava.
*
* In contrast to RxJava, intervals are calculated since previous run completion; this is more
* convenient for potentially long work (prevents overlapping) and does not suffer from queueing
* multiple invocations in Doze mode on Android.
*
* Dispatcher is inherited from scope, may be overridden via [context] parameter.
/**
* ambil data web dengan OkHttp
*/
private void ambilDataWeb(String url) {
// create request
Request request = new Request.Builder().url(url).build();
// create async request call
httpClient.newCall(request).enqueue(new Callback() {
@Override public void onFailure(Call call, IOException e) {
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private static final String URL_SERVICES = "https://httpbin.org/get";
private Button btnAmbilData;
private TextView tvHasil;
// declare http client
private OkHttpClient httpClient;
/**
* ambil data dari internet menggunakan StringRequest dari volley
*/
private void getDataFromURL(String url) {
StringRequest stringRequest =
new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override public void onResponse(String response) {
// logging
Log.d(TAG, "onResponse: " + response);
tvHasil.setText(response);
public class MainActivity extends AppCompatActivity {
private static final String DATA_FROM_NET = "https://httpbin.org/get";
private static final String TAG = "MainActivity";
private Button btnGetData;
private TextView tvHasil;
private RequestQueue requestQueue;
@Override protected void onCreate(Bundle savedInstanceState) {
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "blogspot.xhdpi.contohvolley"
minSdkVersion 17
targetSdkVersion 25
versionCode 1
public class MainActivity extends AppCompatActivity implements AmbilDataTask.Callback {
private static final String TAG = "main";
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new AmbilDataTask(this).execute("https://httpbin.org/get");
}
public class AmbilDataTask extends AsyncTask<String, Void, String> {
public interface Callback {
void sendResult(String result);
}
private static final String TAG = "AmbilDataTask";
private Callback callback;
/**
* bca text dari input stream, dan convert menjadi string object
*
* @throws IOException
*/
private String readStream(InputStream stream, int maxLength) throws IOException {
String result = null;
// Read InputStream using the UTF-8 charset.
InputStreamReader reader = new InputStreamReader(stream, "UTF-8");
// Create temporary buffer to hold Stream data with specified max length.
/**
* baca data dari http connection
*/
private String ambilDataFromURL(URL url) throws IOException {
// define inputStream
InputStream inputStream = null;
// define HttpURLConnection
HttpURLConnection httpURLConnection = null;
String hasil = null;
try {