This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| apply plugin: 'com.android.application' | |
| android { | |
| compileSdkVersion 25 | |
| buildToolsVersion "25.0.2" | |
| defaultConfig { | |
| applicationId "blogspot.xhdpi.contohvolley" | |
| minSdkVersion 17 | |
| targetSdkVersion 25 | |
| versionCode 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class AmbilDataTask extends AsyncTask<String, Void, String> { | |
| public interface Callback { | |
| void sendResult(String result); | |
| } | |
| private static final String TAG = "AmbilDataTask"; | |
| private Callback callback; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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 { |
NewerOlder